-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (52 loc) · 1.25 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
package main
import (
"fmt"
"os"
"github.com/gin-gonic/gin"
"github.com/jessevdk/go-flags"
)
var version = "master"
// Options is a server options
type Options struct {
Version bool `long:"version" description:"Show version"`
Address string `short:"a" long:"address" description:"Server address" default:":3000"`
Verbose []bool `short:"v" long:"verbose" description:"Verbose output"`
}
var options Options
var parser = flags.NewParser(&options, flags.Default)
// ModuleFunc is module callback function
type ModuleFunc func(*gin.Engine)
var modules []ModuleFunc
// RegisterModule register new module
func RegisterModule(desc string, data interface{}, mod ModuleFunc) {
parser.AddGroup(desc, "", data)
modules = append(modules, mod)
}
func main() {
_, err := parser.Parse()
if err != nil {
ferr, ok := err.(*flags.Error)
if ok && ferr.Type == flags.ErrHelp {
os.Exit(0)
} else {
os.Exit(1)
}
}
if options.Version {
fmt.Println(version)
os.Exit(0)
}
gin.SetMode(gin.ReleaseMode)
if len(options.Verbose) >= 2 {
gin.SetMode(gin.DebugMode)
}
engine := gin.New()
if len(options.Verbose) >= 1 {
engine.Use(gin.Logger())
}
engine.Use(gin.Recovery())
for _, mod := range modules {
mod(engine)
}
engine.Run(options.Address)
}