-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
75 lines (61 loc) · 1.69 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
package main
import (
"fmt"
"net/http"
"os"
"github.com/gin-gonic/gin"
dotEnv "github.com/joho/godotenv"
adminService "github.com/shariarfaisal/order-ms/pkg/admin/service"
brandService "github.com/shariarfaisal/order-ms/pkg/brand/service"
hubService "github.com/shariarfaisal/order-ms/pkg/hub/service"
marketService "github.com/shariarfaisal/order-ms/pkg/market/service"
orderService "github.com/shariarfaisal/order-ms/pkg/order/service"
riderService "github.com/shariarfaisal/order-ms/pkg/rider/service"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var db *gorm.DB
func JSONMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Content-Type", "application/json")
c.Next()
}
}
func initEnv() {
err := dotEnv.Load(".env")
if err != nil {
panic("Error loading .env file")
}
}
func main() {
initEnv()
env := os.Getenv("ENV")
host := os.Getenv("DB_HOST")
user := os.Getenv("DB_USER")
password := os.Getenv("DB_PASSWORD")
dbName := os.Getenv("DB_NAME")
port := os.Getenv("DB_PORT")
sslMode := "disable"
if env == "production" {
sslMode = ""
}
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s", host, user, password, dbName, port, sslMode)
dbRes, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
db = dbRes
r := gin.Default()
r.SetTrustedProxies([]string{"localhost"})
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
adminService.Init(db, r)
hubService.Init(db, r)
orderService.Init(db, r)
riderService.Init(db, r)
brandService.Init(db, r)
marketService.Init(db, r)
r.Use(JSONMiddleware())
r.Run(":5000") // listen and serve on
}