-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (116 loc) · 3 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
130
131
132
133
134
135
package main
import (
"flag"
"fmt"
"net/http"
"time"
"github.com/CactusDev/Xerophi/command"
"github.com/CactusDev/Xerophi/quote"
"github.com/CactusDev/Xerophi/rethink"
"github.com/CactusDev/Xerophi/types"
"github.com/gin-gonic/gin"
debugFname "github.com/onrik/logrus/filename"
log "github.com/sirupsen/logrus"
)
var port int
var config Config
func init() {
var debug, verbose bool
flag.BoolVar(&debug, "debug", false, "Run the API in debug mode")
flag.BoolVar(&debug, "d", false, "Run the API in debug mode")
flag.BoolVar(&verbose, "verbose", false, "Run the API in verbose mode")
flag.BoolVar(&verbose, "v", false, "Run the API in verbose mode")
flag.IntVar(&port, "port", 8000, "Specify which port the API will run on")
flag.Parse()
if debug {
log.Warn("Starting API in debug mode!")
gin.SetMode(gin.DebugMode)
} else if verbose {
log.Warn("Starting API in verbose mode!")
} else {
gin.SetMode(gin.ReleaseMode)
}
if debug || verbose {
log.SetLevel(log.DebugLevel)
log.AddHook(debugFname.NewHook())
}
// Load the config
config = LoadConfig()
}
func generateRoutes(h types.Handler, g *gin.RouterGroup) {
for _, r := range h.Routes() {
if !r.Enabled {
// Route currently disabled
continue
}
switch r.Verb {
case "GET":
g.GET(r.Path, r.Handler)
case "PATCH":
g.PATCH(r.Path, r.Handler)
case "POST":
g.POST(r.Path, r.Handler)
case "DELETE":
g.DELETE(r.Path, r.Handler)
}
}
}
// TODO: Look into using this in future to remove duplicate error catching code
// func catchPanic(ctx *gin.Context) {
// defer func(ctx *gin.Context) {
// if rec := recover(); rec != nil {
// err, ok := rec.(types.ServerError)
// if !ok {
// // We have an actual panic
// log.Warn("Recovered from actual panic!")
// log.Warn(errors.New(rec))
// return
// }
// // We panic-d only purpose within the function, nicely handle that
// util.NiceError(ctx, err.Error, err.Code)
// return
// }
// }(ctx)
// ctx.Next()
// }
func main() {
rdbConn := rethink.Connection{
DB: config.Rethink.DB,
Opts: config.Rethink.Connection,
}
err := rdbConn.Connect()
if err != nil {
log.Fatal("RethinkDB Connection Failed! - ", err)
}
handlers := map[string]types.Handler{
"/user/:token/command": &command.Command{
Conn: &rdbConn,
Table: "commands",
},
"/user/:token/quote": "e.Quote{
Conn: &rdbConn,
Table: "quotes",
},
}
router := gin.Default()
api := router.Group("/api/v2")
// Intialize the monitoring/status system
monitor := rethink.Status{
Tables: map[string]struct{}{
"commands": {},
},
DBs: map[string]struct{}{
"cactus": {},
},
LastUpdated: time.Now(),
}
monitor.Monitor(&rdbConn)
api.GET("/status", monitor.APIStatusHandler)
for baseRoute, handler := range handlers {
group := api.Group(baseRoute)
generateRoutes(handler, group)
}
router.Run(fmt.Sprintf(":%d", config.Server.Port))
log.Warnf("API starting on :%d - %s", port, router.BasePath)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}