presumably working basic routing + db queries
This commit is contained in:
23
config/config.go
Normal file
23
config/config.go
Normal file
@@ -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"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
12
controllers/user/router.go
Normal file
12
controllers/user/router.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package handlers
|
package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -6,8 +6,8 @@ import (
|
|||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
"golang.org/x/crypto/bcrypt"
|
|
||||||
"github.com/yourusername/go-sqlc-jwt/db"
|
"github.com/yourusername/go-sqlc-jwt/db"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AuthHandler struct {
|
type AuthHandler struct {
|
||||||
@@ -100,3 +100,4 @@ func (h *AuthHandler) Register(c *gin.Context) {
|
|||||||
"username": user.Username,
|
"username": user.Username,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
66
main.go
66
main.go
@@ -1,72 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
"github.com/yourusername/go-sqlc-jwt/db"
|
r "github.com/yourusername/go-sqlc-jwt/routes"
|
||||||
"github.com/yourusername/go-sqlc-jwt/internal/handlers"
|
|
||||||
"github.com/yourusername/go-sqlc-jwt/internal/middleware"
|
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
|
||||||
DB struct {
|
|
||||||
DSN string
|
|
||||||
}
|
|
||||||
JWT struct {
|
|
||||||
Secret string
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cfg := Config{
|
router := r.MainRouter()
|
||||||
DB: struct{ DSN string }{
|
log.Fatal(router.Run(":8080"))
|
||||||
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"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
58
routes/routes.go
Normal file
58
routes/routes.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user