-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
129 lines (113 loc) · 3.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/BranceLee/Support/config"
"github.com/BranceLee/Support/coreservice"
"github.com/getsentry/sentry-go"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"go.uber.org/zap"
)
type server struct {
env string
db *gorm.DB
conf *config.Config
sentryDSN string
logger *zap.Logger
}
func (s *server) connectToDB() {
s.logger.Info("Connecting to database", zap.String("event", "connectToDB_connect"))
dbConfig := config.DefaultPostgresConfig()
db, err := gorm.Open(dbConfig.Dialect(), dbConfig.ConnectionInfo())
if err != nil {
sentry.CaptureException(err)
s.logger.Fatal("Failed to connect to the db", zap.String("event", "connectToDB_fail"), zap.Error(err))
}
db.LogMode(s.env == "TEST")
s.db = db
s.logger.Info("Connected to database", zap.String("event", "connectToDB_success"))
}
func (s *server) initSentry() {
sentryDSN := config.GetSentryDSN()
err := sentry.Init(sentry.ClientOptions{
Dsn: sentryDSN,
})
if err != nil {
s.logger.Fatal("Failed to initialize sentry", zap.String("event", "init sentry failed"), zap.Error(err))
}
s.sentryDSN = sentryDSN
s.logger.Info("LoadSentry success", zap.String("event", "initSentry_success"))
}
func (s *server) initConf() {
switch s.env {
case config.TEST:
conf, _ := config.LoadTestConfig()
s.conf = conf
case config.DEV:
// TODO: LoadProdconfig
case config.PROD:
// TODO: LoadProdConfig
}
}
func main() {
exampaleLogger := zap.NewExample()
env, ok := os.LookupEnv("ENV")
if !ok {
exampaleLogger.Fatal("ENV is not found")
}
serv := &server{
logger: exampaleLogger,
env: env,
}
serv.initConf()
serv.initSentry()
serv.connectToDB()
defer serv.db.Close()
undo := zap.RedirectStdLog(serv.logger)
defer serv.logger.Sync()
defer undo()
handler, err := coreservice.NewHandler(serv.db)
if err != nil {
log.Fatal(err)
return
}
r := mux.NewRouter()
r.HandleFunc("/api/category/new", handler.CreateCategory).Methods("POST")
r.HandleFunc("/api/category", handler.GetCategory).Methods("GET")
r.HandleFunc("/api/category/blog", handler.GetAllBlogs).Methods("GET")
r.HandleFunc("/api/category/blog/new", handler.CreateBlog).Methods("POST")
r.HandleFunc("/api/user/new", handler.CreateUser).Methods("POST")
server := http.Server{
Addr: ":8000",
Handler: r,
ReadTimeout: time.Second * 60,
WriteTimeout: time.Second * 60,
IdleTimeout: time.Second * 60,
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
idleConnsClosed := make(chan struct{})
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
signal.Notify(sigint, syscall.SIGTERM)
<-sigint
if err := server.Shutdown(ctx); err != nil {
serv.logger.Info("HTTP server shutdown", zap.Error(err))
}
close(idleConnsClosed)
}()
serv.logger.Info("> Server runs on 8000")
err = server.ListenAndServe()
if err != nil {
serv.logger.Info("HTTP server error", zap.Error(err))
}
<-idleConnsClosed
}