-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpistol.go
162 lines (134 loc) · 4.12 KB
/
pistol.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package Sex
import (
"net/http"
"strings"
"github.com/Showmax/go-fqdn"
)
// Pistol is the Sex HTTP handler, who are used to setup http server
// Example:
// router := Sex.NewPistol().
// Add("/", func(Sex.Request) string {
// return "Hello World"
// }).
// Run()
type Pistol struct {
*http.ServeMux
rootPath string
config Config
routes Dict
plugins []Plugin
err error
}
// Plugin is a extension to Sex Pistol
type Plugin interface {
Name() string
Init(*Pistol) (*Pistol, error)
Root(http.ResponseWriter, *http.Request) (http.ResponseWriter, *http.Request)
}
// GetPath provides Pistol root path
func (pistol *Pistol) GetPath() string { return pistol.rootPath }
// GetRoutes provides Pistol route list
func (pistol *Pistol) GetRoutes() Dict { return pistol.routes }
// Error provides Pistol last error
func (pistol *Pistol) Error() error { return pistol.err }
// SetErr sets Pistol last error
func (pistol *Pistol) SetErr(err error) { pistol.err = err }
// NewPistol create new Sex.Pistol and default configurations
// Example:
// router := Sex.NewPistol()
func NewPistol() *Pistol {
pistol := new(Pistol)
for _, plugin := range pistol.plugins {
p, err := plugin.Init(pistol)
if err == nil {
pistol = p
}
}
pistol.config = make(Config)
pistol.routes = make(Dict)
pistol.ServeMux = http.NewServeMux()
pistol.HandleFunc("/", pistol.root)
return pistol
}
// Add adds endpoint to the Sex.Pistol Server
// path are the endpoint location
// route is a void interface thats need to be on next format list:
// - func (http.ResponseWriter, *http.Request)
// - func (Sex.Request) Sex.Response
// (res, status)
// - func (Sex.Request) string // Or (string, int)
// - func (Sex.Request) []byte // Or ([]byte, int)
// - func (Sex.Request) Sex.Json // Or (Sex.Json, int)
// methods are a list of accepted HTTP methods to endpoint
// Example:
// router.Add("/", func(Sex.Request) string {
// return "Hello World"
// }, "POST")
// router.Add("/ok", func(Sex.Request) Sex.Json, int {
// return map[stirng]bool{
// "ok": true,
// }, 404
// })
func (pistol *Pistol) Add(path string, route interface{}, methods ...string) *Pistol {
for i, method := range methods {
methods[i] = strings.ToUpper(method)
}
path = fixPath(path)
root_path := fixPath(pistol.rootPath)
if path != root_path {
path = fixPath(root_path + path)
}
path_pattern := GetPathPattern(path)
conf := Prop{
"path-template": []string{path},
"methods-allowed": methods,
}
pistol.config[path_pattern] = conf
pistol.routes[path_pattern] = route
return pistol
}
// AddPlugin adds plugins to Sex Pistol
func (pistol *Pistol) AddPlugin(plugin Plugin) *Pistol {
pistol.plugins = append(pistol.plugins, plugin)
return pistol
}
// Run set-up Sex.Pistol server
// Example:
// pistol.Run(5000) // Will run server on port 5000
// pistol.Run("/joao") // will run server on path "/joao"
// pistol.Run("/joao", 80) // will run server on path "/joao" and port 80
// pistol.Run(80, "/joao") // will run server on path "/joao" and port 80
//
// If you run a Sex Pistol server with $SEX_DEBUG setted as "true" thats function will to log list all Sex endpoints of router
func (pistol *Pistol) Run(a ...interface{}) error {
port := 8000
path := "/"
if a != nil {
for _, v := range a {
if v, ok := v.(string); ok {
path = v
}
if v, ok := v.(int); ok {
port = v
}
}
}
pistol.rootPath = path
host, err := fqdn.FqdnHostname()
if err != nil {
host = "localhost"
}
msg := Fmt("Running Sex Pistol server at %s:%d%s", host, port, path)
RawLog(LogLevelInfo, false, msg)
if GetEnv("SEX_DEBUG", "false") != "false" {
for path := range pistol.routes {
methods_str := "ALL METHODS"
if methods := pistol.config[path].Values("methods-allowed"); len(methods) > 0 {
methods_str = strings.Join(methods, ", ")
}
msg := Fmt("%s <- %s", pistol.config[path].Get("path-template"), methods_str)
RawLog(LogLevelInfo, false, msg)
}
}
return http.ListenAndServe(Fmt(":%d", port), pistol)
}