-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
52 lines (42 loc) · 926 Bytes
/
main.go
File metadata and controls
52 lines (42 loc) · 926 Bytes
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
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/commy08/golang_sandbox.git/routes"
"github.com/joho/godotenv"
"github.com/labstack/echo"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var DB *gorm.DB
func init() {
loc, _ := time.LoadLocation("Asia/Bangkok")
time.Local = loc
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
return
}
connectDB()
}
func main() {
e := echo.New()
routes.Routes(e.Group(""))
e.Logger.Fatal(e.Start(":" + os.Getenv("PORT")))
}
func connectDB() {
dsn := fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true&loc=%s",
os.Getenv("DB_USERNAME"), os.Getenv("DB_PASSWORD"), os.Getenv("DB_HOST"), os.Getenv("DB_PORT"), os.Getenv("DB_NAME"),
"Asia%2FBangkok",
)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
fmt.Println(err)
}
// Migrate the schema
// db.AutoMigrate(&models.User{})
DB = db
}