added some govno to postgres
This commit is contained in:
+40
-4
@@ -12,8 +12,11 @@ import (
|
||||
docs "gitea.d3m0k1d.ru/HellreigN/Control-plane/docs"
|
||||
"gitea.d3m0k1d.ru/HellreigN/Control-plane/internal/auth"
|
||||
"gitea.d3m0k1d.ru/HellreigN/Control-plane/internal/config"
|
||||
"gitea.d3m0k1d.ru/HellreigN/Control-plane/internal/org"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/pressly/goose/v3"
|
||||
"github.com/swaggo/files"
|
||||
"github.com/swaggo/gin-swagger"
|
||||
)
|
||||
@@ -48,16 +51,38 @@ func main() {
|
||||
}
|
||||
log.Println("connected to postgres")
|
||||
|
||||
repo := auth.NewRepository(pool)
|
||||
db := stdlib.OpenDBFromPool(pool)
|
||||
defer db.Close()
|
||||
|
||||
if err := repo.Migrate(ctx); err != nil {
|
||||
if err := goose.Up(db, "migrations"); err != nil {
|
||||
log.Fatalf("failed to run migrations: %v", err)
|
||||
}
|
||||
log.Println("migrations applied")
|
||||
|
||||
repo := auth.NewRepository(pool)
|
||||
orgRepo := org.NewRepository(pool)
|
||||
|
||||
svc := auth.NewService(repo, cfg.JWTSecret, cfg.JWTExpiration, cfg.JWTRefreshExpiration)
|
||||
handler := auth.NewHandler(svc)
|
||||
|
||||
orgSvc := org.NewService(orgRepo)
|
||||
orgHandler := org.NewHandler(orgSvc)
|
||||
|
||||
loginLimiter := auth.NewRateLimiter(10, time.Minute)
|
||||
authMW := auth.AuthMiddleware([]byte(cfg.JWTSecret))
|
||||
|
||||
go func() {
|
||||
ticker := time.NewTicker(30 * time.Minute)
|
||||
defer ticker.Stop()
|
||||
for range ticker.C {
|
||||
cleanupCtx, cleanupCancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
if err := repo.DeleteExpiredRefreshTokens(cleanupCtx); err != nil {
|
||||
log.Printf("failed to cleanup expired tokens: %v", err)
|
||||
}
|
||||
cleanupCancel()
|
||||
}
|
||||
}()
|
||||
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
r := gin.New()
|
||||
r.Use(gin.Logger(), gin.Recovery())
|
||||
@@ -74,10 +99,21 @@ func main() {
|
||||
api := r.Group("/api/auth")
|
||||
{
|
||||
api.POST("/register", handler.Register)
|
||||
api.POST("/login", handler.Login)
|
||||
api.POST("/login", loginLimiter.Middleware(), handler.Login)
|
||||
api.POST("/refresh", handler.Refresh)
|
||||
api.POST("/logout", handler.Logout)
|
||||
api.GET("/me", auth.AuthMiddleware([]byte(cfg.JWTSecret)), handler.Me)
|
||||
api.GET("/me", authMW, handler.Me)
|
||||
api.PUT("/me", authMW, handler.UpdateProfile)
|
||||
api.PUT("/password", authMW, handler.ChangePassword)
|
||||
}
|
||||
|
||||
orgs := r.Group("/api/organizations", authMW)
|
||||
{
|
||||
orgs.POST("", orgHandler.Create)
|
||||
orgs.GET("", orgHandler.List)
|
||||
orgs.GET("/:id", orgHandler.GetByID)
|
||||
orgs.PUT("/:id", orgHandler.Update)
|
||||
orgs.DELETE("/:id", orgHandler.Delete)
|
||||
}
|
||||
|
||||
srv := &http.Server{
|
||||
|
||||
Reference in New Issue
Block a user