11package api
22
33import (
4- "auth/config"
5- "auth/service"
6- "auth/utils"
4+ "fmt"
75 "net/http"
86 "strings"
97 "time"
108
9+ "github.com/gaucho-racing/mapache/auth/config"
10+ "github.com/gaucho-racing/mapache/auth/pkg/logger"
11+ "github.com/gaucho-racing/mapache/auth/service"
1112 "github.com/gin-contrib/cors"
1213 "github.com/gin-gonic/gin"
1314)
1415
15- func SetupRouter () * gin.Engine {
16- if config .Env == "PROD" {
16+ func Run () {
17+ api := InitializeRouter ()
18+ InitializeRoutes (api )
19+ err := api .Run (":" + config .Port )
20+ if err != nil {
21+ logger .SugarLogger .Fatalf ("Failed to start server: %v" , err )
22+ }
23+ }
24+
25+ func InitializeRouter () * gin.Engine {
26+ if config .IsProduction () {
1727 gin .SetMode (gin .ReleaseMode )
1828 }
1929 r := gin .Default ()
@@ -30,7 +40,7 @@ func SetupRouter() *gin.Engine {
3040}
3141
3242func InitializeRoutes (router * gin.Engine ) {
33- router .GET ("/auth /ping" , Ping )
43+ router .GET (fmt . Sprintf ( "/%s /ping", config . Service . Name ) , Ping )
3444 router .POST ("/auth/login" , Login )
3545 router .GET ("/users" , GetAllUsers )
3646 router .GET ("/users/@me" , GetCurrentUser )
@@ -39,19 +49,27 @@ func InitializeRoutes(router *gin.Engine) {
3949
4050func AuthChecker () gin.HandlerFunc {
4151 return func (c * gin.Context ) {
52+ if config .SkipAuthCheck {
53+ c .Set ("Auth-Token" , "mock-token" )
54+ c .Set ("Auth-UserID" , "mock-user" )
55+ c .Set ("Auth-Audience" , "mock-audience" )
56+ c .Set ("Auth-Scope" , "openid profile email" )
57+ c .Next ()
58+ return
59+ }
4260 if c .GetHeader ("Authorization" ) != "" {
4361 authHeader := c .GetHeader ("Authorization" )
4462 if strings .HasPrefix (authHeader , "Bearer " ) {
4563 claims , err := service .ValidateJWT (strings .Split (c .GetHeader ("Authorization" ), "Bearer " )[1 ])
4664 if err != nil {
47- utils .SugarLogger .Errorln ("Failed to validate token: " + err .Error ())
65+ logger .SugarLogger .Errorln ("Failed to validate token: " + err .Error ())
4866 c .AbortWithStatusJSON (401 , gin.H {"message" : err .Error ()})
4967 } else {
50- utils .SugarLogger .Infof ("Decoded token: %s" , claims .Subject )
51- utils .SugarLogger .Infof ("↳ Client ID: %s" , claims .Audience [0 ])
52- utils .SugarLogger .Infof ("↳ Scope: %s" , claims .Scope )
53- utils .SugarLogger .Infof ("↳ Issued at: %s" , claims .IssuedAt .String ())
54- utils .SugarLogger .Infof ("↳ Expires at: %s" , claims .ExpiresAt .String ())
68+ logger .SugarLogger .Infof ("Decoded token: %s" , claims .Subject )
69+ logger .SugarLogger .Infof ("↳ Client ID: %s" , claims .Audience [0 ])
70+ logger .SugarLogger .Infof ("↳ Scope: %s" , claims .Scope )
71+ logger .SugarLogger .Infof ("↳ Issued at: %s" , claims .IssuedAt .String ())
72+ logger .SugarLogger .Infof ("↳ Expires at: %s" , claims .ExpiresAt .String ())
5573 c .Set ("Auth-Token" , strings .Split (c .GetHeader ("Authorization" ), "Bearer " )[1 ])
5674 c .Set ("Auth-UserID" , claims .Subject )
5775 c .Set ("Auth-Audience" , claims .Audience [0 ])
@@ -70,8 +88,7 @@ func UnauthorizedPanicHandler() gin.HandlerFunc {
7088 if err == "Unauthorized" {
7189 c .AbortWithStatusJSON (http .StatusUnauthorized , gin.H {"message" : "you are not authorized to access this resource" })
7290 } else {
73- // Handle other panics
74- utils .SugarLogger .Errorf ("Unexpected panic: %v" , err )
91+ logger .SugarLogger .Errorf ("Unexpected panic: %v" , err )
7592 c .AbortWithStatusJSON (http .StatusInternalServerError , gin.H {"message" : err .(string )})
7693 }
7794 }
@@ -80,14 +97,12 @@ func UnauthorizedPanicHandler() gin.HandlerFunc {
8097 }
8198}
8299
83- // Require checks if a condition is true, otherwise aborts the request
84100func Require (c * gin.Context , condition bool ) {
85101 if ! condition {
86102 panic ("Unauthorized" )
87103 }
88104}
89105
90- // Any checks if any condition is true, otherwise returns false
91106func Any (conditions ... bool ) bool {
92107 for _ , condition := range conditions {
93108 if condition {
@@ -97,7 +112,6 @@ func Any(conditions ...bool) bool {
97112 return false
98113}
99114
100- // All checks if all conditions are true, otherwise returns false
101115func All (conditions ... bool ) bool {
102116 for _ , condition := range conditions {
103117 if ! condition {
0 commit comments