Skip to content

Commit 6f85612

Browse files
committed
feat: added router logger
1 parent 152f2c4 commit 6f85612

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

http/route/logger.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package route
2+
3+
import (
4+
gologger "github.com/ralvarezdev/go-logger"
5+
gologgerstatus "github.com/ralvarezdev/go-logger/status"
6+
)
7+
8+
// Logger is the logger for router
9+
type Logger struct {
10+
logger gologger.Logger
11+
}
12+
13+
// NewLogger is the logger for the router
14+
func NewLogger(logger gologger.Logger) (*Logger, error) {
15+
// Check if the logger is nil
16+
if logger == nil {
17+
return nil, gologger.ErrNilLogger
18+
}
19+
20+
return &Logger{logger: logger}, nil
21+
}
22+
23+
// RegisterRouteGroup registers a route group
24+
func (l *Logger) RegisterRouteGroup(routerPath string, routerGroupPath string) {
25+
l.logger.LogMessage(
26+
gologger.NewLogMessage(
27+
"Registering route group",
28+
gologgerstatus.StatusDebug,
29+
nil,
30+
"router path: "+routerPath,
31+
"router group path: "+routerGroupPath,
32+
),
33+
)
34+
}
35+
36+
// RegisterRoute registers a route
37+
func (l *Logger) RegisterRoute(routerPath string, routePath string) {
38+
l.logger.LogMessage(
39+
gologger.NewLogMessage(
40+
"Registering route",
41+
gologgerstatus.StatusDebug,
42+
nil,
43+
"router path: "+routerPath,
44+
"route path: "+routePath,
45+
),
46+
)
47+
}

http/route/router.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package route
22

33
import (
4+
goflagsmode "github.com/ralvarezdev/go-flags/mode"
45
"net/http"
56
)
67

@@ -16,13 +17,26 @@ type (
1617

1718
// Router is the route group struct
1819
Router struct {
19-
mux *http.ServeMux
20+
mux *http.ServeMux
21+
path string
22+
mode *goflagsmode.Flag
23+
logger *Logger
2024
}
2125
)
2226

2327
// NewRouter creates a new router
24-
func NewRouter() *Router {
25-
return &Router{mux: http.NewServeMux()}
28+
func NewRouter(path string, mode *goflagsmode.Flag, logger *Logger) *Router {
29+
// Check if the path is empty
30+
if path == "" {
31+
path = "/"
32+
}
33+
34+
return &Router{
35+
mux: http.NewServeMux(),
36+
logger: logger,
37+
path: path,
38+
mode: mode,
39+
}
2640
}
2741

2842
// NewRouterGroup creates a new route group
@@ -32,8 +46,19 @@ func NewRouterGroup(baseRoute *Router, path string) (*Router, error) {
3246
return nil, ErrNilRouter
3347
}
3448

49+
// Check the base route path
50+
routerPath := path
51+
if baseRoute.path != "/" {
52+
routerPath = baseRoute.path + path
53+
}
54+
3555
// Create a new router
36-
instance := &Router{mux: http.NewServeMux()}
56+
instance := &Router{
57+
mux: http.NewServeMux(),
58+
logger: baseRoute.logger,
59+
path: routerPath,
60+
mode: baseRoute.mode,
61+
}
3762

3863
// Register the route group
3964
baseRoute.RegisterRouteGroup(path, instance)
@@ -48,7 +73,12 @@ func (r *Router) Handler() *http.ServeMux {
4873

4974
// HandleFunc registers a new route with a path and a handler function
5075
func (r *Router) HandleFunc(path string, handler http.HandlerFunc) {
76+
// Register the route
5177
r.mux.HandleFunc(path, handler)
78+
79+
if r.logger != nil && r.mode != nil && !r.mode.IsProd() {
80+
r.logger.RegisterRoute(r.path, path)
81+
}
5282
}
5383

5484
// RegisterRoute registers a new route with a path and a handler function
@@ -63,7 +93,12 @@ func (r *Router) RegisterHandler(path string, handler http.Handler) {
6393
path = path[:len(path)-1]
6494
}
6595

96+
// Register the route group
6697
r.mux.Handle(path+"/", http.StripPrefix(path, handler))
98+
99+
if r.logger != nil && r.mode != nil && !r.mode.IsProd() {
100+
r.logger.RegisterRouteGroup(r.path, path)
101+
}
67102
}
68103

69104
// RegisterRouteGroup registers a new route group with a path and a router

0 commit comments

Comments
 (0)