-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (73 loc) · 2.08 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
81
82
83
84
85
86
87
88
package main
import (
"fmt"
"github.com/joho/godotenv"
"github.com/serhatmorkoc/go-realworld-example/handler/api"
as "github.com/serhatmorkoc/go-realworld-example/store/article"
cs "github.com/serhatmorkoc/go-realworld-example/store/comment"
"github.com/serhatmorkoc/go-realworld-example/store/shared/db"
"github.com/serhatmorkoc/go-realworld-example/store/shared/db/seed"
us "github.com/serhatmorkoc/go-realworld-example/store/user"
"github.com/sirupsen/logrus"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
)
func main() {
if err := godotenv.Load(); err != nil {
panic(err)
}
fmt.Println(os.Getenv("CONSOLE_L"))
driver := os.Getenv("DB_DRIVER")
host := os.Getenv("DB_HOST")
database := os.Getenv("DB_DATABASE")
port, _ := strconv.Atoi(os.Getenv("DB_PORT"))
user := os.Getenv("DB_USERNAME")
password := os.Getenv("DB_PASSWORD")
maxOpenConnections, _ := strconv.Atoi(os.Getenv("DB_MAX_OPEN_CONNECTIONS"))
db, err := db.Connection(driver, host, database, user, password, port, maxOpenConnections)
if err != nil {
log.Fatal(err)
}
articleStore := as.NewArticleStore(db)
userStore := us.NewUserStore(db)
commentStore := cs.NewCommentStore(db)
r := api.New(userStore, commentStore, articleStore)
h := r.Handler()
sd, _ := strconv.ParseBool(os.Getenv("DB_SEED"))
if sd {
if err = seed.Seed(userStore); err != nil {
logrus.Fatalf("error occurred on database seed: %s", err.Error())
}
}
logrus.Info("application starting")
log.Println("application starting")
go func() {
s := http.Server{
Addr: ":8080",
Handler: h,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20, //1mb
}
err := s.ListenAndServe()
if err != nil {
log.Println("application failed to start")
panic(err)
}
}()
log.Println("application started")
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
<-quit
logrus.Info("application shutting down")
log.Println("database closing")
if err := db.Close(); err != nil {
panic(err)
}
log.Println("database closed")
}