-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (78 loc) · 2.1 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
package main
import (
"fmt"
"flag"
"net/http"
"github.com/vlamug/scfg/config"
"github.com/vlamug/scfg/api"
"github.com/vlamug/scfg/storage"
"github.com/vlamug/scfg/loader"
"github.com/vlamug/scfg/cache"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/jinzhu/gorm"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
// DEFAULT_LISTEN_ADDR is default api address
DEFAULT_LISTEN_ADDR = ":9002"
)
// App is main struct of api service
type App struct {
db *gorm.DB
loaderService *loader.Loader
router *mux.Router
}
// initDbConnection initializes database connections
func (app *App) initDbConnection(configPath string) error {
cfg, err := config.LoadDatabaseConfig(configPath)
if err != nil {
return err
}
dsn := fmt.Sprintf(
"host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
cfg.Host,
cfg.Port,
cfg.User,
cfg.Password,
cfg.DbName,
)
app.db, err = gorm.Open("postgres", dsn)
if err != nil {
panic(err)
}
return nil
}
// initRouter initializes api endpoints
func (app *App) initRouter() {
app.router = mux.NewRouter()
app.router.HandleFunc("/get", api.GetHandler(app.loaderService)).Methods("POST")
app.router.Handle("/metrics", promhttp.Handler())
}
// initLoaderService initializes service of loading config
func (app *App) initLoaderService() {
app.loaderService = loader.NewLoader(storage.NewPostgresStorage(app.db), cache.NewMem())
}
// listenAndServe listens and serve requests
func (app *App) listenAndServe(listenAddr string) {
server := http.Server{Addr:listenAddr, Handler: app.router}
panic(server.ListenAndServe())
}
// close closes database connection
func (app *App) close() {
app.db.Close()
}
func main() {
listenAddr := flag.String("api.listen-addr", DEFAULT_LISTEN_ADDR, "the address of api")
dbConfigPath := flag.String("database.config-path", "etc/database.json", "the path to the database config")
flag.Parse()
app := App{}
err := app.initDbConnection(*dbConfigPath)
if err != nil {
panic(err)
}
app.initLoaderService()
app.initRouter()
app.listenAndServe(*listenAddr)
defer app.close()
}