-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (70 loc) · 1.56 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
package main
import (
"flag"
"fmt"
"os"
"github.com/gookit/color"
"github.com/gookit/gcli/v2"
"github.com/gookit/rux"
"github.com/gookit/rux/handlers"
"github.com/gookit/view"
"github.com/inherelab/httprr/app"
"github.com/inherelab/httprr/route"
)
var (
port uint
debug = os.Getenv("HTTPRR_DEBUG") == "true"
)
func bindFlags() {
binName := os.Args[0]
defDebug := os.Getenv("HTTPRR_DEBUG") == "true"
gf := gcli.NewFlags()
gf.FSet().Usage = func() {
color.Infoln("Usage:", binName, "[--OPTIONS] ...\n")
color.Yellow.Println("Options:")
gf.PrintHelpPanel()
}
gf.BoolOpt(&debug, "debug", "", defDebug, "open debug mode")
gf.UintOpt(&port, "port", "p", 18081, "the http server port")
err := gf.Parse(os.Args[1:])
if err != nil {
if err == flag.ErrHelp {
os.Exit(0)
}
panic(err)
}
app.HttpAddr = fmt.Sprintf("127.0.0.1:%d", port)
}
func init() {
bindFlags()
// view templates
view.Initialize(func(r *view.Renderer) {
r.ViewsDir = "resource/views"
})
// open debug
rux.Debug(debug)
}
// start: go run main.go
// access: http://127.0.0.1:18080
func main() {
r := rux.New()
r.StaticDir("/static", "static")
// register routes
r.Use(handlers.PanicsHandler())
if debug {
r.Use(handlers.RequestLogger())
}
// handle error
r.OnError = func(c *rux.Context) {
if err := c.FirstError(); err != nil {
fmt.Println(err)
c.HTTPError(err.Error(), 400)
return
}
}
route.LoadRoutes(r)
// quick start
r.Listen(app.HttpAddr)
// apply global pre-handlers
// http.ListenAndServe(":18080", handlers.HTTPMethodOverrideHandler(r))
}