-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (59 loc) · 1.55 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"fmt"
"log"
"os"
"github.com/gin-gonic/gin"
"github.com/hoyirul/go-starter-api/commands"
"github.com/hoyirul/go-starter-api/config"
"github.com/hoyirul/go-starter-api/middlewares"
"github.com/hoyirul/go-starter-api/routes"
"github.com/joho/godotenv"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go [function]")
return
}
functionToRun := os.Args[1]
switch functionToRun {
case "key:generate":
commands.GenerateSecretKey()
case "db:seed":
commands.SeedDB()
case "migrate":
commands.MigrateDB()
case "serve":
config.ConnectDB()
if key := os.Getenv("SECRET_KEY"); key == "" {
fmt.Println("Your key is null, please run `go run main.go key:generate`")
} else {
fmt.Println("Starting Go API Server...")
// Baca variabel lingkungan dari file .env
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
// Periksa apakah variabel GIN_MODE ada di lingkungan
ginMode := os.Getenv("GIN_MODE")
if ginMode == "development" {
ginMode = gin.DebugMode
} else if ginMode == "release" {
gin.SetMode(gin.ReleaseMode)
} else {
log.Fatalf("Invalid GIN_MODE value: %s. Should be 'release' or empty.", ginMode)
}
// Inisialisasi router Gin
// router := gin.Default()
router := gin.New()
// Untuk CORS
router.Use(middlewares.CorsMiddleware())
// Setup rute untuk pengguna (user) dan produk (product)
routes.SetupRoutes(router)
// Jalankan server
router.Run(":8080")
}
default:
fmt.Println("Function not found.")
}
}