110 lines
3.2 KiB
Go
110 lines
3.2 KiB
Go
// @title GoGin API
|
||
// @version 1.0
|
||
// @description API documentation for GoGin
|
||
// @host localhost:8080
|
||
// @BasePath /
|
||
// @securityDefinitions.apikey BearerAuth
|
||
// @in header
|
||
// @name Authorization
|
||
// @description Enter your JWT token (include "Bearer " prefix, e.g., "Bearer <token>")
|
||
|
||
package main
|
||
|
||
import (
|
||
"fmt"
|
||
database "goGin/app/database/config"
|
||
migrasyon "goGin/app/database/migrate"
|
||
"goGin/app/database/seed"
|
||
"goGin/app/routes"
|
||
config "goGin/config"
|
||
utils "goGin/pkg/utis"
|
||
"log"
|
||
"os"
|
||
|
||
// Swagger
|
||
_ "goGin/docs"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
swaggerFiles "github.com/swaggo/files"
|
||
ginSwagger "github.com/swaggo/gin-swagger"
|
||
)
|
||
|
||
const customSwaggerIndexHTML = `<!doctype html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>GoGin Swagger</title>
|
||
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css">
|
||
</head>
|
||
<body>
|
||
<div id="swagger-ui"></div>
|
||
<script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js"></script>
|
||
<script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-standalone-preset.js"></script>
|
||
<script>
|
||
window.ui = SwaggerUIBundle({
|
||
url: '/swagger-docs/doc.json',
|
||
dom_id: '#swagger-ui',
|
||
presets: [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset],
|
||
layout: 'BaseLayout',
|
||
requestInterceptor: function (req) {
|
||
req.headers = req.headers || {};
|
||
var auth = req.headers.Authorization || req.headers.authorization;
|
||
if (auth && !/^Bearer\\s+/i.test(auth)) {
|
||
req.headers.Authorization = 'Bearer ' + auth;
|
||
delete req.headers.authorization;
|
||
}
|
||
return req;
|
||
}
|
||
});
|
||
</script>
|
||
</body>
|
||
</html>
|
||
`
|
||
|
||
func main() {
|
||
// 1. Load Config
|
||
config.LoadConfig()
|
||
|
||
// 2. Setup Database
|
||
database.ConnectDB()
|
||
database.ConnectRedis()
|
||
migrasyon.Migrate()
|
||
|
||
// 3. Seed komutu (opsiyonel)
|
||
if len(os.Args) > 1 && os.Args[1] == "seed-admin" {
|
||
fmt.Println("Seeding default admin user...")
|
||
seed.SeedDefaultAdmin()
|
||
seed.SeedDefaultSettings()
|
||
fmt.Println("Admin seeding completed.")
|
||
return
|
||
}
|
||
|
||
// 4. Banner ve başlangıç logu
|
||
fmt.Println(`
|
||
___ __ __ ___ ___ ___ _ __ ___ _ _ ___
|
||
| _ )| | / \| \ | _ ) / \| |/ / | __|| \| || \
|
||
| _ \| |_| () | |) || _ \| - | ' < | _| | . || |) |
|
||
|___/|____\__/|___/ |___/|_| |_|_|\_\ |___||_|\_||___/
|
||
|
||
`)
|
||
fmt.Println(" Go Backend | v1.0.0 | " + utils.ColorGreen + "Running" + utils.ColorReset)
|
||
fmt.Println()
|
||
|
||
// 5. Router'ı başlat ve server'ı çalıştır
|
||
r := routes.SetupRouter()
|
||
// Custom swagger page: token'ı otomatik Bearer formatına çevirir
|
||
r.GET("/swagger", func(c *gin.Context) {
|
||
c.Redirect(302, "/swagger/index.html")
|
||
})
|
||
r.GET("/swagger/index.html", func(c *gin.Context) {
|
||
c.Data(200, "text/html; charset=utf-8", []byte(customSwaggerIndexHTML))
|
||
})
|
||
// Swagger JSON ve diğer endpointler (farklı prefix, route çakışması yok)
|
||
r.GET("/swagger-docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||
addr := ":" + config.AppConfig.Port
|
||
if err := r.Run(addr); err != nil {
|
||
log.Fatalf("failed to run server: %v", err)
|
||
}
|
||
}
|