forked from tryor/trygo
/
ssss.go
138 lines (119 loc) · 3.12 KB
/
ssss.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
package ssss
import (
"fmt"
"log"
"net"
"net/http"
"net/http/fcgi"
"os"
"time"
)
const VERSION = "0.0.1"
var (
SSSSApp *App
AppPath string
)
func init() {
SSSSApp = NewApp(nil)
AppPath, _ = os.Getwd()
}
type App struct {
Handlers *ControllerRegistor
config *Config
StaticDirs map[string]string
TemplateRegistor *TemplateRegistor
}
func NewApp(config *Config) *App {
cr := NewControllerRegistor()
app := &App{Handlers: cr,
config: config,
StaticDirs: make(map[string]string),
TemplateRegistor: NewTemplateRegistor()}
cr.app = app
return app
}
//method-http method, GET,POST,PUT,HEAD,DELETE,PATCH,OPTIONS,*
//path-URL path
//name - method on the container
func (app *App) Register(method string, path string, c IController, name string, params ...string) *App {
ps := ""
if len(params) > 0 {
ps = params[0]
}
app.Handlers.Add(method, path, c, name, ps)
return app
}
//采用正则路由
func (app *App) RegisterPattern(method string, pattern string, c IController, name string, params ...string) *App {
ps := ""
if len(params) > 0 {
ps = params[0]
}
app.Handlers.Add(method, pattern, c, name, ps, true)
return app
}
func Register(method string, path string, c IController, name string, params ...string) *App {
SSSSApp.Register(method, path, c, name, params...)
return SSSSApp
}
func RegisterPattern(method string, path string, c IController, name string, params ...string) *App {
SSSSApp.RegisterPattern(method, path, c, name, params...)
return SSSSApp
}
func (app *App) SetStaticPath(url string, path string) *App {
app.StaticDirs[url] = path
return app
}
func SetStaticPath(url string, path string) *App {
SSSSApp.StaticDirs[url] = path
return SSSSApp
}
func AddTemplateExt(ext string) {
SSSSApp.TemplateRegistor.AddTemplateExt(ext)
}
func AddFuncMap(key string, funname interface{}) error {
return SSSSApp.TemplateRegistor.AddFuncMap(key, funname)
}
func (app *App) buildTemplate() {
if app.config.TemplatePath != "" {
app.TemplateRegistor.buildTemplate(app.config.TemplatePath)
}
}
func (app *App) Run() {
app.buildTemplate()
if app.config.HttpAddr == "" {
app.config.HttpAddr = "0.0.0.0"
}
addr := fmt.Sprintf("%s:%d", app.config.HttpAddr, app.config.HttpPort)
var err error
for {
if app.config.UseFcgi {
l, e := net.Listen("tcp", addr)
if e != nil {
log.Print("Listen: ", e)
}
//log.Print("UseFcgi, fcgi.Serve")
err = fcgi.Serve(l, app.Handlers)
} else {
//log.Print("http.ListenAndServe")
//err = http.ListenAndServe(addr, app.Handlers)
err = httpListenAndServe(addr, app.Handlers, app.config.ReadTimeout, app.config.WriteTimeout)
}
if err != nil {
log.Print("ListenAndServe: ", err)
//panic(err)
}
time.Sleep(time.Second * 2)
}
}
func httpListenAndServe(addr string, handler http.Handler, readTimeout time.Duration, writeTimeout time.Duration) error {
if readTimeout == 0 {
readTimeout = time.Second * 5
}
server := &http.Server{Addr: addr, Handler: handler, ReadTimeout: readTimeout, WriteTimeout: writeTimeout}
return server.ListenAndServe()
}
func Run(config *Config) {
SSSSApp.config = config
SSSSApp.Run()
}