first commit
This commit is contained in:
83
main.go
Normal file
83
main.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"ginimageApi/app/middleware"
|
||||
"ginimageApi/configs"
|
||||
_ "ginimageApi/docs"
|
||||
"ginimageApi/routers"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
// @title Gin Image API
|
||||
// @version 1.0
|
||||
// @description GinImage API dokumantasyonu. Legacy access token formatlari desteklenmez.
|
||||
// @host mcp.beyhano.net.tr
|
||||
// @schemes https
|
||||
// @securityDefinitions.apikey BearerAuth
|
||||
// @in header
|
||||
// @name Authorization
|
||||
|
||||
func ensureRequiredEnv() error {
|
||||
if strings.TrimSpace(os.Getenv("JWT_SECRET")) == "" {
|
||||
return fmt.Errorf("JWT_SECRET zorunludur")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TIP <p>To run your code, right-click the code and select <b>Run</b>.</p> <p>Alternatively, click
|
||||
// the <icon src="AllIcons.Actions.Execute"/> icon in the gutter and select the <b>Run</b> menu item from here.</p>
|
||||
func main() {
|
||||
if err := godotenv.Load(); err != nil {
|
||||
log.Println("no .env file found, relying on environment variables")
|
||||
}
|
||||
|
||||
if err := ensureRequiredEnv(); err != nil {
|
||||
log.Fatalf("config: %v", err)
|
||||
}
|
||||
|
||||
if err := configs.ConnectDB(); err != nil {
|
||||
log.Fatalf("database: %v", err)
|
||||
}
|
||||
|
||||
if err := configs.RunAutoMigrate(); err != nil {
|
||||
log.Fatalf("migration: %v", err)
|
||||
}
|
||||
|
||||
if err := configs.SeedSecurityDefaults(); err != nil {
|
||||
log.Fatalf("seed security defaults: %v", err)
|
||||
}
|
||||
|
||||
if err := configs.ConnectRedis(); err != nil {
|
||||
log.Fatalf("redis: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
if err := configs.CloseRedis(); err != nil {
|
||||
log.Printf("redis kapatılırken hata: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
r := gin.Default()
|
||||
r.Use(middleware.DynamicCORS())
|
||||
r.Use(middleware.DynamicRateLimit())
|
||||
routers.Setup(r)
|
||||
|
||||
if err := r.SetTrustedProxies([]string{"127.0.0.1"}); err != nil {
|
||||
log.Fatalf("failed to set trusted proxies: %v", err)
|
||||
}
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
if err := r.Run(":" + port); err != nil {
|
||||
log.Fatalf("server: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user