-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.go
186 lines (159 loc) · 3.98 KB
/
app.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package hime
import (
"context"
"crypto/tls"
"html/template"
"net"
"net/http"
"sync"
"github.com/moonrhythm/parapet"
)
// App is the hime app
type App struct {
srv *parapet.Server
handler http.Handler
routes Routes
globals sync.Map
onceServeHTTP sync.Once
serveHandler http.Handler
template map[string]*tmpl
component map[string]*tmpl
cachedComponent sync.Map
parent *template.Template
ETag bool
}
type ctxKeyApp struct{}
// New creates new app
func New() *App {
app := &App{}
app.SetServer(¶pet.Server{})
app.setupParent()
return app
}
// Clone clones app
func (app *App) Clone() *App {
x := &App{
srv: ¶pet.Server{
Addr: app.srv.Addr,
ReadTimeout: app.srv.ReadTimeout,
ReadHeaderTimeout: app.srv.ReadHeaderTimeout,
WriteTimeout: app.srv.WriteTimeout,
IdleTimeout: app.srv.IdleTimeout,
MaxHeaderBytes: app.srv.MaxHeaderBytes,
TCPKeepAlivePeriod: app.srv.TCPKeepAlivePeriod,
GraceTimeout: app.srv.GraceTimeout,
WaitBeforeShutdown: app.srv.WaitBeforeShutdown,
ErrorLog: app.srv.ErrorLog,
TrustProxy: app.srv.TrustProxy,
H2C: app.srv.H2C,
ReusePort: app.srv.ReusePort,
ConnState: app.srv.ConnState,
TLSConfig: app.srv.TLSConfig.Clone(),
BaseContext: app.srv.BaseContext,
},
handler: app.handler,
routes: cloneRoutes(app.routes),
globals: cloneMap(&app.globals),
template: cloneTmpl(app.template),
parent: template.Must(app.parent.Clone()),
ETag: app.ETag,
}
x.srv.Handler = x
x.setupParent()
return x
}
// Address sets server address
func (app *App) Address(addr string) {
app.srv.Addr = addr
}
// Handler sets the handler
func (app *App) Handler(h http.Handler) {
app.handler = h
}
func (app *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
app.onceServeHTTP.Do(func() {
app.serveHandler = app.handler
if app.serveHandler == nil {
app.serveHandler = http.DefaultServeMux
}
app.serveHandler = app.ServeHandler(app.serveHandler)
})
app.serveHandler.ServeHTTP(w, r)
}
func (app *App) ServeHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = context.WithValue(ctx, ctxKeyApp{}, app)
r = r.WithContext(ctx)
h.ServeHTTP(w, r)
})
}
// Server returns server inside app
func (app *App) Server() *parapet.Server {
return app.srv
}
func (app *App) SetServer(srv *parapet.Server) {
if srv == nil {
panic("nil server")
}
app.srv = srv
srv.Handler = app
}
// Shutdown shutdowns server
func (app *App) Shutdown() error {
return app.srv.Shutdown()
}
// ListenAndServe starts web server
func (app *App) ListenAndServe() error {
return app.srv.ListenAndServe()
}
// Serve serves listener
func (app *App) Serve(l net.Listener) error {
return app.srv.Serve(l)
}
func (app *App) ensureTLSConfig() {
if app.srv.TLSConfig == nil {
app.srv.TLSConfig = &tls.Config{}
}
}
// TLS sets cert and key file
func (app *App) TLS(certFile, keyFile string) {
app.ensureTLSConfig()
err := loadTLSCertKey(app.srv.TLSConfig, certFile, keyFile)
if err != nil {
panicf("load key pair; %v", err)
}
}
// SelfSign generates self sign cert
func (app *App) SelfSign(s SelfSign) {
app.ensureTLSConfig()
err := s.config(app.srv.TLSConfig)
if err != nil {
panicf("generate self sign; %v", err)
}
}
func (app *App) setupParent() {
if app.parent == nil {
app.parent = template.New("")
}
if app.template == nil {
app.template = make(map[string]*tmpl)
}
if app.component == nil {
app.component = make(map[string]*tmpl)
}
app.parent.Funcs(template.FuncMap{
"param": tfParam,
"templateName": func() string { return "" },
"component": app.renderComponent,
"route": app.Route,
"global": app.Global,
})
}
func getApp(ctx context.Context) *App {
app, ok := ctx.Value(ctxKeyApp{}).(*App)
if !ok {
panic(ErrAppNotFound)
}
return app
}