-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
59 lines (49 loc) · 1.02 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
package main
import (
"context"
"net/http"
"os"
"os/signal"
"time"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/200lab-training-1/models"
"github.com/200lab-training-1/routes"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
)
func main() {
db, err := gorm.Open("mysql", "root:123456@/200labnotes?charset=utf8&parseTime=True&loc=Local")
if err != nil {
panic(err)
}
defer db.Close()
db.LogMode(true)
db.AutoMigrate(&models.Note{}, &models.User{})
fileWrite, err := os.Create("access.log")
if err != nil {
panic(err)
}
gin.SetMode(gin.DebugMode)
gin.DefaultWriter = fileWrite
r := gin.Default()
routes.InitRoutes(r, db)
port := os.Getenv("HTTP_PORT")
if port == "" {
port = "8081"
}
srv := &http.Server{
Addr: ":" + port,
Handler: r,
}
go func() {
err := srv.ListenAndServe()
if err != nil {
panic(err)
}
}()
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
srv.Shutdown(ctx)
}