-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (54 loc) · 1.66 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
package main
import (
"github.com/SojebSikder/goframe/app/middleware"
"github.com/SojebSikder/goframe/config"
"github.com/SojebSikder/goframe/routes"
orm "github.com/SojebSikder/goframe/system/core/ORM"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"context"
"log"
"github.com/SojebSikder/goframe/ent"
_ "github.com/go-sql-driver/mysql"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
ctg, _ := config.GetConfig()
// Initialize the database connection
hostname := ctg.Database.Hostname
database := ctg.Database.Database
user := ctg.Database.User
password := ctg.Database.Password
var databaseConnection string
if password == "" {
databaseConnection = user + "@tcp(" + hostname + ":3306)/" + database + "?parseTime=True"
} else {
databaseConnection = user + ":" + password + "@tcp(" + hostname + ":3306)/" + database + "?parseTime=True"
}
// Initialize database connection
client, err := ent.Open("mysql", databaseConnection)
// client, err := ent.Open("mysql", "root:pass@tcp(localhost:3306)/go-example?parseTime=True")
if err != nil {
log.Fatalf("failed opening connection to mysql: %v", err)
}
defer client.Close()
ctx := context.Background()
// Run the auto migration tool.
if err := client.Schema.Create(context.Background()); err != nil {
log.Fatalf("failed creating schema resources: %v", err)
}
//
// Initialize ORM
orm.Init(ctx, client)
// Initialize the application
r := gin.Default()
r.Static("/static", "./"+config.StaticDir)
r.LoadHTMLGlob(config.TemplateDir + "/*")
// Custom middleware call here
r.Use(middleware.Hello())
routes.Routes(r)
r.Run(":" + ctg.App.Port)
}