-
Notifications
You must be signed in to change notification settings - Fork 41
/
main.go
74 lines (58 loc) · 1.5 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
71
72
73
74
package main
import (
"os"
helmet "github.com/danielkov/gin-helmet"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"github.com/restuwahyu13/gin-rest-api/models"
"github.com/restuwahyu13/gin-rest-api/pkg"
"github.com/restuwahyu13/gin-rest-api/routes"
)
func main() {
app := SetupRouter()
logrus.Fatal(app.Run(":" + pkg.GodotEnv("GO_PORT")))
}
func SetupRouter() *gin.Engine {
db := SetupDatabase()
app := gin.Default()
if pkg.GodotEnv("GO_ENV") != "production" && pkg.GodotEnv("GO_ENV") != "test" {
gin.SetMode(gin.DebugMode)
} else if pkg.GodotEnv("GO_ENV") == "test" {
gin.SetMode(gin.TestMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
app.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"*"},
AllowHeaders: []string{"*"},
AllowWildcard: true,
}))
app.Use(helmet.Default())
app.Use(gzip.Gzip(gzip.BestCompression))
routes.InitAuthRoutes(db, app)
routes.InitStudentRoutes(db, app)
return app
}
func SetupDatabase() *gorm.DB {
db, err := gorm.Open(postgres.Open(pkg.GodotEnv("DATABASE_URI")), &gorm.Config{})
if err != nil {
defer logrus.Info("Connect into Database Failed")
logrus.Fatal(err.Error())
}
if os.Getenv("GO_ENV") != "production" {
logrus.Info("Connect into Database Successfully")
}
err = db.AutoMigrate(
&models.ModelAuth{},
&models.ModelStudent{},
)
if err != nil {
logrus.Fatal(err.Error())
}
return db
}