-
Notifications
You must be signed in to change notification settings - Fork 0
/
fipple.go
84 lines (67 loc) · 1.45 KB
/
fipple.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
package fipple
import "text/template"
import (
"github.com/fipress/fiplog"
"net/http"
)
var (
defaultService *Service
templates *template.Template
compress = true
logger fiplog.Logger
//todo: other config
)
type RouterType int
const (
Regex RouterType = iota
Trie
)
type Router interface {
addRoute(route *route)
dispatch(url string, ctx *Context)
}
func DefaultService() *Service {
if defaultService == nil {
defaultService = NewService()
}
return defaultService
}
func Start(port string) error {
return DefaultService().Start(port)
}
func AddRoutes(route ...*route) {
DefaultService().AddRoutes(route...)
}
func AddRoute(route *route) {
DefaultService().AddRoute(route)
}
func Get(path string, action action) {
DefaultService().Get(path, action)
}
func Post(path string, action action) {
DefaultService().Post(path, action)
}
func Delete(path string, action action) {
getLogger().Debug("delete, path:", path)
DefaultService().Delete(path, action)
}
func ServeHTTP(rw http.ResponseWriter, req *http.Request) {
DefaultService().ServeHTTP(rw, req)
}
func AddTemplGlob(pattern string) {
var err error
templates, err = template.ParseGlob(pattern)
if err != nil {
getLogger().Error("parse templates failed,", err)
}
//templates.ParseFiles("/temps/cover.html")
}
func SetLogger(log fiplog.Logger) {
logger = log
}
func getLogger() fiplog.Logger {
if logger == nil {
logger = fiplog.GetLogger()
}
return logger
}