Feat(ktm-booking): Initial commit
Some checks failed
ktm-booking-bot/ktm-booking-bot/pipeline/head Something is wrong with the build of this commit
Some checks failed
ktm-booking-bot/ktm-booking-bot/pipeline/head Something is wrong with the build of this commit
This commit is contained in:
50
backend/internal/user/main.go
Normal file
50
backend/internal/user/main.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Env struct {
|
||||
DB *gorm.DB
|
||||
CookieString string
|
||||
}
|
||||
|
||||
func UserRoutes(db *gorm.DB) chi.Router {
|
||||
var env Env
|
||||
env.DB = db
|
||||
env.CookieString = os.Getenv("COOKIE_STRING")
|
||||
if env.CookieString == "" {
|
||||
env.CookieString = "cookie_string"
|
||||
}
|
||||
|
||||
r := chi.NewRouter()
|
||||
allowRegistration := os.Getenv("ALLOW_REGISTRATION")
|
||||
if strings.ToUpper(allowRegistration) == "TRUE" || allowRegistration == "1" {
|
||||
log.Println("Registration enabled.")
|
||||
r.Post("/register", env.registerRouteHandler)
|
||||
}
|
||||
r.Post("/login", env.loginRouteHandler)
|
||||
r.Post("/logout", env.logoutRouteHandler)
|
||||
|
||||
checkLoggedInUserGroup := r.Group(nil)
|
||||
checkLoggedInUserGroup.Use(env.CheckUserMiddleware)
|
||||
checkLoggedInUserGroup.Get("/me", env.meRouteHandler)
|
||||
checkLoggedInUserGroup.Put("/profile", env.setProfileRouteHandler)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func NewUserEnv(db *gorm.DB) *Env {
|
||||
var env Env
|
||||
env.DB = db
|
||||
env.CookieString = os.Getenv("COOKIE_STRING")
|
||||
if env.CookieString == "" {
|
||||
env.CookieString = "cooking_string"
|
||||
}
|
||||
return &env
|
||||
}
|
||||
Reference in New Issue
Block a user