Backend/cmd/main.go
2025-09-25 09:01:00 +04:00

85 lines
2.6 KiB
Go

package main
// @title 58team blog backend
// @version 1.0
// @description 58team blog's backend
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /api/v1
// @securityDefinitions.basic BasicAuth
import (
docs "58team_blog/docs"
"58team_blog/internal/application/services"
"58team_blog/internal/infrastructure"
"58team_blog/internal/infrastructure/db"
"58team_blog/internal/infrastructure/db/repo"
"58team_blog/internal/interfaces"
"58team_blog/internal/utils"
"log"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
const secret = "58secret"
func main() {
router := gin.Default()
// Setup cookie container
router.Use(sessions.Sessions("session", cookie.NewStore([]byte(secret))))
// Register custom validators
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
v.RegisterValidation("password", utils.PasswordValidator)
}
// Swagger setup
docs.SwaggerInfo.Title = "58team blog"
docs.SwaggerInfo.Description = "This is blog 58team"
docs.SwaggerInfo.Version = "1.0"
docs.SwaggerInfo.Host = "localhost:8080"
docs.SwaggerInfo.BasePath = "/api/v1/"
docs.SwaggerInfo.Schemes = []string{"http", "https"}
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.URL("http://localhost:8080/swag/doc/doc.json")))
router.StaticFile("/swag/doc/doc.json", "docs/swagger.json")
// Routes setup
g := router.Group("/api/v1")
config, err := infrastructure.LoadConfig()
if err != nil {
log.Fatal("Load config error: ", err)
}
d, err := db.DatabaseInit(*config)
if err != nil {
log.Fatal("Database error: ", err)
}
postRepository := repo.CreatePostRepository(d)
userRepository := repo.CreateUserRepository(d)
imagesRepository := repo.CreateImagesRepository(d)
postService := services.CreatePostService(&postRepository)
userService := services.CreateUserService(&userRepository)
imagesService := services.CreateImagesService(&imagesRepository)
interfaces.BindPostAdmin(&postService, &userService, g)
interfaces.BindUser(config.AdminName, config.AdminPassword, &userService, g)
interfaces.BindImages(config.ImagesPath, &imagesService, g)
router.Run(":8080")
}