From 5ed47a796f1583a6ee1c823b7ef421152c8adaeb Mon Sep 17 00:00:00 2001 From: Dmitry Chumak Date: Wed, 28 May 2025 20:43:37 +0500 Subject: [PATCH] presumably working basic routing + db queries --- config/config.go | 23 +++++++ controllers/user/router.go | 12 ++++ .../auth.go => controllers/user/user.go | 17 ++--- main.go | 66 ++----------------- routes/routes.go | 58 ++++++++++++++++ 5 files changed, 106 insertions(+), 70 deletions(-) create mode 100644 config/config.go create mode 100644 controllers/user/router.go rename internal/handlers/auth.go => controllers/user/user.go (93%) create mode 100644 routes/routes.go diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..168a08f --- /dev/null +++ b/config/config.go @@ -0,0 +1,23 @@ +package config + +import "os" + +type Config struct { + DB struct { + DSN string + } + JWT struct { + Secret string + } +} + +func GetCfg() Config { + return Config{ + DB: struct{ DSN string }{ + DSN: os.Getenv("DB_DSN"), + }, + JWT: struct{ Secret string }{ + Secret: os.Getenv("JWT_SECRET"), + }, + } +} diff --git a/controllers/user/router.go b/controllers/user/router.go new file mode 100644 index 0000000..5c01841 --- /dev/null +++ b/controllers/user/router.go @@ -0,0 +1,12 @@ +package user + +import ( + "github.com/gin-gonic/gin" +) + +func UserRouter(r *gin.Engine) *gin.Engine { + r.POST("/login", Login) + r.POST("/register", Register) + + return r +} diff --git a/internal/handlers/auth.go b/controllers/user/user.go similarity index 93% rename from internal/handlers/auth.go rename to controllers/user/user.go index c09e82a..0d6fae6 100644 --- a/internal/handlers/auth.go +++ b/controllers/user/user.go @@ -1,4 +1,4 @@ -package handlers +package user import ( "net/http" @@ -6,18 +6,18 @@ import ( "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt/v5" - "golang.org/x/crypto/bcrypt" "github.com/yourusername/go-sqlc-jwt/db" + "golang.org/x/crypto/bcrypt" ) type AuthHandler struct { - Queries *db.Queries + Queries *db.Queries JWTSecret string } func NewAuthHandler(q *db.Queries, secret string) *AuthHandler { return &AuthHandler{ - Queries: q, + Queries: q, JWTSecret: secret, } } @@ -46,9 +46,9 @@ func (h *AuthHandler) Login(c *gin.Context) { } token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ - "sub": user.ID, + "sub": user.ID, "username": user.Username, - "exp": time.Now().Add(time.Hour * 24).Unix(), + "exp": time.Now().Add(time.Hour * 24).Unix(), }) tokenString, err := token.SignedString([]byte(h.JWTSecret)) @@ -68,7 +68,7 @@ func (h *AuthHandler) Login(c *gin.Context) { } c.JSON(http.StatusOK, gin.H{ - "token": tokenString, + "token": tokenString, "expires_in": 3600 * 24, }) } @@ -99,4 +99,5 @@ func (h *AuthHandler) Register(c *gin.Context) { "id": user.ID, "username": user.Username, }) -} \ No newline at end of file +} + diff --git a/main.go b/main.go index 33c16d8..fc05fc4 100644 --- a/main.go +++ b/main.go @@ -1,72 +1,14 @@ package main import ( - "database/sql" "log" - "os" - "time" - "github.com/gin-gonic/gin" _ "github.com/lib/pq" - "github.com/yourusername/go-sqlc-jwt/db" - "github.com/yourusername/go-sqlc-jwt/internal/handlers" - "github.com/yourusername/go-sqlc-jwt/internal/middleware" - "net/http" + r "github.com/yourusername/go-sqlc-jwt/routes" ) -type Config struct { - DB struct { - DSN string - } - JWT struct { - Secret string - } +func main() { + router := r.MainRouter() + log.Fatal(router.Run(":8080")) } -func main() { - cfg := Config{ - DB: struct{ DSN string }{ - DSN: os.Getenv("DB_DSN"), - }, - JWT: struct{ Secret string }{ - Secret: os.Getenv("JWT_SECRET"), - }, - } - - dbConn, err := sql.Open("postgres", cfg.DB.DSN) - if err != nil { - log.Fatal("failed to connect to db:", err) - } - defer dbConn.Close() - - // Configure database connection pool - dbConn.SetMaxOpenConns(25) - dbConn.SetMaxIdleConns(25) - dbConn.SetConnMaxLifetime(5 * time.Minute) - queries := db.New(dbConn) - - r := gin.Default() - - authHandler := handlers.NewAuthHandler(queries, cfg.JWT.Secret) - authGroup := r.Group("/auth") - { - authGroup.POST("/register", authHandler.Register) - authGroup.POST("/login", authHandler.Login) - } - - // Protected routes - protected := r.Group("/api") - protected.Use(middleware.AuthMiddleware(cfg.JWT.Secret)) - { - protected.GET("/profile", func(c *gin.Context) { - userID := c.MustGet("userID").(float64) - username := c.MustGet("username").(string) - c.JSON(http.StatusOK, gin.H{ - "id": userID, - "username": username, - }) - }) - } - - log.Fatal(r.Run(":8080")) -} \ No newline at end of file diff --git a/routes/routes.go b/routes/routes.go new file mode 100644 index 0000000..b9030b3 --- /dev/null +++ b/routes/routes.go @@ -0,0 +1,58 @@ +package routes + +import ( + "database/sql" + "log" + "net/http" + "time" + + "github.com/gin-gonic/gin" + "github.com/yourusername/go-sqlc-jwt/config" + "github.com/yourusername/go-sqlc-jwt/controllers/user" + "github.com/yourusername/go-sqlc-jwt/db" + "github.com/yourusername/go-sqlc-jwt/internal/middleware" +) + +func MainRouter() *gin.Engine { + cfg := config.GetCfg() + + dbConn, err := sql.Open("postgres", cfg.DB.DSN) + if err != nil { + log.Fatal("failed to connect to db:", err) + } + defer dbConn.Close() + + // Configure database connection pool + dbConn.SetMaxOpenConns(25) + dbConn.SetMaxIdleConns(25) + dbConn.SetConnMaxLifetime(5 * time.Minute) + + queries := db.New(dbConn) + + r := gin.Default() + + authHandler := user.NewAuthHandler(queries, cfg.JWT.Secret) + authGroup := r.Group("/auth") + { + authGroup.POST("/register", authHandler.Register) + authGroup.POST("/login", authHandler.Login) + } + + // Protected routes + protected := r.Group("/api") + protected.Use(middleware.AuthMiddleware(cfg.JWT.Secret)) + { + protected.GET("/profile", func(c *gin.Context) { + userID := c.MustGet("userID").(float64) + username := c.MustGet("username").(string) + c.JSON(http.StatusOK, gin.H{ + "id": userID, + "username": username, + }) + }) + } + + user.UserRouter(r) + + return r +}