File tree Expand file tree Collapse file tree 4 files changed +31
-11
lines changed Expand file tree Collapse file tree 4 files changed +31
-11
lines changed Original file line number Diff line number Diff line change
1
+ package middlewares
2
+
3
+ import (
4
+ "fmt"
5
+ "net/http"
6
+ "time"
7
+
8
+ "github.com/sirupsen/logrus"
9
+ )
10
+
11
+ func (m * Middleware ) LogMiddleware (next http.Handler ) http.Handler {
12
+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
13
+ fmt .Println ("hiteed" )
14
+ start := time .Now ()
15
+
16
+ // Call the next handler
17
+ next .ServeHTTP (w , r )
18
+
19
+ // Log request details with Logrus
20
+ logrus .WithFields (logrus.Fields {
21
+ "method" : r .Method ,
22
+ "url" : r .URL .Path ,
23
+ "duration" : time .Since (start ).String (),
24
+ }).Info ("Request processed" )
25
+ })
26
+ }
Original file line number Diff line number Diff line change @@ -16,7 +16,8 @@ import (
16
16
"github.com/hammer-code/lms-be/domain"
17
17
"github.com/hammer-code/lms-be/utils"
18
18
"github.com/sirupsen/logrus"
19
- "github.com/swaggo/http-swagger"
19
+ httpSwagger "github.com/swaggo/http-swagger"
20
+
20
21
// _ "swagger-mux/docs"
21
22
"github.com/rs/cors"
22
23
"github.com/spf13/cobra"
@@ -37,14 +38,10 @@ var serveHttpCmd = &cobra.Command{
37
38
38
39
// route
39
40
router := registerHandler (app )
40
- // router.Use(cors.Default().Handler )
41
+ router .Use (app . Middleware . LogMiddleware )
41
42
42
43
// build cors
43
- muxCorsWithRouter := cors .New (cors.Options {
44
- AllowedOrigins : []string {"*" , "http://localhost:8000" },
45
- AllowedMethods : []string {"*" },
46
- AllowedHeaders : []string {"*" },
47
- }).Handler (router )
44
+ muxCorsWithRouter := cors .AllowAll ().Handler (router )
48
45
49
46
srv := & http.Server {
50
47
Addr : cfg .APP_PORT ,
Original file line number Diff line number Diff line change @@ -66,10 +66,6 @@ func GetConfig() Config {
66
66
methods = strings .Split (corsMethods , "," )
67
67
}
68
68
69
- logrus .Info ("cors_allow_origins :" , origins )
70
- logrus .Info ("cors_allow_headers :" , headers )
71
- logrus .Info ("cors_allow_methods :" , methods )
72
-
73
69
c = & Config {
74
70
APP_ENV : viper .GetString ("APP_ENV" ),
75
71
APP_NAME : viper .GetString ("APP_NAME" ),
Original file line number Diff line number Diff line change @@ -4,4 +4,5 @@ import "net/http"
4
4
5
5
type Middleware interface {
6
6
AuthMiddleware (next http.Handler ) http.Handler
7
+ LogMiddleware (next http.Handler ) http.Handler
7
8
}
You can’t perform that action at this time.
0 commit comments