-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
80 lines (65 loc) · 1.87 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
75
76
77
78
79
80
package main
import (
"log"
"net/http"
"os"
"github.com/ashraful88/flyway-postgres-golang/config"
"github.com/ashraful88/flyway-postgres-golang/flyway"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func main() {
dbHost, _ := os.LookupEnv("POSTGRES_HOST")
dbPort, _ := os.LookupEnv("POSTGRES_PORT")
dbName, _ := os.LookupEnv("POSTGRES_DB")
dbUser, _ := os.LookupEnv("POSTGRES_USER")
dbPass, _ := os.LookupEnv("POSTGRES_PASSWORD")
// load .env file
err := godotenv.Load(".env")
if err != nil {
log.Print("Error loading primary .env file")
}
// get env from .env file
env := os.Getenv("ENV")
if env == "" {
env = "local"
}
// load configs non based on env
config.Conf = config.GetConfigByEnv(env)
if env != "local" {
cs := flyway.CreateConnectionString(dbHost, dbPort, dbName, dbUser, dbPass, config.Conf.FlywayDBSchemas, config.Conf.FlywayLocations, config.Conf.FlywayConnRetry)
// wipe all database schema and data
/* res, err := flyway.Clean(cs)
if err != nil {
log.Println("Flyway DB Clean Failed", err)
}
log.Println("flyway Clean", res) */
// run migration
res, err := flyway.Migrate(cs)
if err != nil {
log.Println("Flyway DB Migration Failed", err)
}
log.Println("flyway Migrate", res)
}
r := gin.New()
r.Use(gin.Logger())
// Recovery middleware recovers from any panics and writes a 500 if there was one.
r.Use(gin.Recovery())
r.Use(func(context *gin.Context) {
context.Writer.Header().Add("Access-Control-Allow-Methods", "*")
context.Writer.Header().Add("Access-Control-Allow-Headers", "*")
context.Writer.Header().Set("Content-Type", "application/json; charset=latin-1")
context.Next()
})
_ = r.SetTrustedProxies(nil)
r.GET("/health", HealthCheck)
err = r.Run(":" + config.Conf.APIPort)
if err != nil {
panic(err)
}
}
func HealthCheck(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"status": "OK",
})
}