JWT proto with login & registration
This commit is contained in:
+89
-5
@@ -1,28 +1,112 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
docs "gitea.d3m0k1d.ru/HellreigN/Control-plane/docs"
|
||||
"gitea.d3m0k1d.ru/HellreigN/Control-plane/internal/auth"
|
||||
"gitea.d3m0k1d.ru/HellreigN/Control-plane/internal/config"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/swaggo/files"
|
||||
"github.com/swaggo/gin-swagger"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
// @title AegisGuard API
|
||||
// @version 1.0
|
||||
// @description API for AegisGuard control plane
|
||||
// @schemes http
|
||||
//
|
||||
// @securityDefinitions.apikey Bearer
|
||||
// @in header
|
||||
// @name Authorization
|
||||
// @description Type "Bearer" followed by a space and the JWT token.
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
log.Fatalf("failed to load config: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
client, err := mongo.Connect(options.Client().ApplyURI(cfg.MongoURI))
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create mongodb client: %v", err)
|
||||
}
|
||||
|
||||
if err := client.Ping(ctx, nil); err != nil {
|
||||
log.Fatalf("failed to ping mongodb: %v", err)
|
||||
}
|
||||
log.Println("connected to mongodb")
|
||||
|
||||
db := client.Database(cfg.MongoDB)
|
||||
|
||||
repo := auth.NewRepository(db)
|
||||
|
||||
if err := repo.EnsureIndexes(ctx); err != nil {
|
||||
log.Printf("warning: failed to ensure indexes: %v", err)
|
||||
}
|
||||
|
||||
svc := auth.NewService(repo, cfg.JWTSecret, cfg.JWTExpiration)
|
||||
handler := auth.NewHandler(svc)
|
||||
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
r := gin.New()
|
||||
r.Use(gin.Logger(), gin.Recovery())
|
||||
|
||||
docs.SwaggerInfo.Title = "AegisGuard API"
|
||||
docs.SwaggerInfo.Version = "1.0"
|
||||
docs.SwaggerInfo.Description = "API for AegisGuard"
|
||||
docs.SwaggerInfo.Schemes = []string{"http"}
|
||||
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
r.GET("/health", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"status": "ok",
|
||||
})
|
||||
c.JSON(200, gin.H{"status": "ok"})
|
||||
})
|
||||
r.Run(":8080")
|
||||
|
||||
api := r.Group("/api/auth")
|
||||
{
|
||||
api.POST("/register", handler.Register)
|
||||
api.POST("/login", handler.Login)
|
||||
api.GET("/me", auth.AuthMiddleware([]byte(cfg.JWTSecret)), handler.Me)
|
||||
}
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: ":" + cfg.ServerPort,
|
||||
Handler: r,
|
||||
}
|
||||
|
||||
go func() {
|
||||
log.Printf("server starting on :%s", cfg.ServerPort)
|
||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatalf("failed to start server: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-quit
|
||||
|
||||
log.Println("shutting down server...")
|
||||
|
||||
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer shutdownCancel()
|
||||
|
||||
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||||
log.Fatalf("server forced to shutdown: %v", err)
|
||||
}
|
||||
|
||||
if err := client.Disconnect(shutdownCtx); err != nil {
|
||||
log.Printf("failed to disconnect mongodb: %v", err)
|
||||
}
|
||||
|
||||
log.Println("server stopped")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user