feat: initial router on main function
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package internalhttp
|
||||
|
||||
import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/cors"
|
||||
"go.uber.org/fx"
|
||||
|
||||
chimware "github.com/go-chi/chi/v5/middleware"
|
||||
)
|
||||
|
||||
var Module = fx.Module("router",
|
||||
fx.Provide(initRouter),
|
||||
)
|
||||
|
||||
func initRouter() chi.Router {
|
||||
router := chi.NewRouter()
|
||||
localCors := cors.New(cors.Options{
|
||||
AllowedOrigins: []string{"*"},
|
||||
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"},
|
||||
AllowedHeaders: []string{"Accept", "Content-Type", "Authorization", "X-Device", "X-Client-Ip"},
|
||||
AllowCredentials: true,
|
||||
Debug: true,
|
||||
})
|
||||
|
||||
router.Use(
|
||||
localCors.Handler,
|
||||
chimware.RequestID,
|
||||
)
|
||||
|
||||
return router
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package pkgconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
func Router(apiRouter chi.Router) {
|
||||
mainRouter := chi.NewRouter()
|
||||
|
||||
mainRouter.Mount("/", apiRouter)
|
||||
mainCtx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
svr := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", APP_PORT),
|
||||
Handler: mainRouter,
|
||||
}
|
||||
|
||||
group, groupCtx := errgroup.WithContext(mainCtx)
|
||||
|
||||
group.Go(func() error {
|
||||
logrus.Infof("Listening to port %d", APP_PORT)
|
||||
return svr.ListenAndServe()
|
||||
})
|
||||
|
||||
group.Go(func() error {
|
||||
<-groupCtx.Done()
|
||||
|
||||
ctxTimeout, cancel := context.WithTimeout(mainCtx, GRACEFULL_TIMEOUT*time.Second)
|
||||
defer cancel()
|
||||
|
||||
svr.Shutdown(ctxTimeout)
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err := group.Wait(); err != nil {
|
||||
logrus.Errorf("system exit, reason: %v", err.Error())
|
||||
} else {
|
||||
logrus.Info("system exit normally")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package pkgconfig
|
||||
|
||||
const APP_PORT = 3000
|
||||
const GRACEFULL_TIMEOUT = 20
|
||||
@@ -1,6 +1,8 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
jwtclaimenum "github.com/ardeman/project-legalgo-go/internal/enums/jwt"
|
||||
timeutils "github.com/ardeman/project-legalgo-go/internal/utilities/time_utils"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
@@ -25,3 +27,13 @@ func GenerateToken(options ...ClaimOption) (string, error) {
|
||||
|
||||
return token.SignedString(jwtSecret)
|
||||
}
|
||||
|
||||
func GenerateToken2(username string) (string, error) {
|
||||
now := timeutils.Now()
|
||||
token := jwt.New(jwt.SigningMethodES256)
|
||||
claims := token.Claims.(jwt.MapClaims)
|
||||
claims["username"] = username
|
||||
claims["exp"] = now.Add(time.Hour).Unix()
|
||||
|
||||
return token.SignedString(jwtSecret)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func StoreToken(ctx context.Context, rdb *redis.Client, token, username string) error {
|
||||
// return rdb.Set(ctx context.Context, key string, value interface{}, expiration time.Duration)
|
||||
return rdb.Set(ctx, "token"+username, token, time.Hour).Err()
|
||||
}
|
||||
Reference in New Issue
Block a user