-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
flame.go
241 lines (205 loc) · 6.34 KB
/
flame.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2021 Flamego. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Package flamego is a fantastic modular Go web framework with a slim core but limitless extensibility.
package flamego
import (
gocontext "context"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/charmbracelet/log"
"github.com/flamego/flamego/inject"
"github.com/flamego/flamego/internal/route"
)
// Flame is the top-level web application instance, which manages global states
// of injected services and middleware.
type Flame struct {
inject.Injector
Router
urlPrefix string // The URL prefix to be trimmed for every request.
befores []BeforeHandler // The list of handlers to be called before matching route.
handlers []Handler // The list of middleware handlers.
action Handler // The last action handler to be executed.
logger *log.Logger // The default request logger.
stop chan struct{} // The signal to stop the HTTP server.
}
// NewWithLogger creates and returns a bare bones Flame instance. Use this
// function if you want to have full control over log destination and middleware
// that are used.
func NewWithLogger(w io.Writer) *Flame {
f := &Flame{
Injector: inject.New(),
logger: log.NewWithOptions(
w,
log.Options{
TimeFormat: "2006-01-02 15:04:05", // TODO(go1.20): Use time.DateTime
Level: log.DebugLevel,
ReportTimestamp: true,
},
),
stop: make(chan struct{}),
}
f.Router = newRouter(f.createContext)
f.NotFound(http.NotFound)
f.Map(f.logger)
f.Map(f.logger.StandardLog())
f.Map(defaultReturnHandler())
return f
}
// New creates and returns a bare bones Flame instance with default logger
// writing to os.Stdout. Use this function if you want to have full control over
// middleware that are used.
func New() *Flame {
return NewWithLogger(os.Stdout)
}
// Classic creates and returns a classic Flame instance with default middleware:
// `flamego.Logger`, `flamego.Recovery` and `flamego.Static`.
func Classic() *Flame {
f := New()
f.Use(
Logger(),
Recovery(),
Static(),
)
return f
}
func (f *Flame) createContext(w http.ResponseWriter, r *http.Request, params route.Params, handlers []Handler, urlPath urlPather) internalContext {
// Allocate a new slice to avoid mutating the original "handlers" and that could
// potentially cause data race.
hs := make([]Handler, 0, len(f.handlers)+len(handlers))
hs = append(hs, f.handlers...)
hs = append(hs, handlers...)
c := newContext(w, r, params, hs, urlPath)
c.SetParent(f)
if f.action != nil {
c.setAction(f.action)
}
return c
}
// Use adds handlers of middleware to the Flame instance, and panics if any of
// the handler is not a callable func. Middleware handlers are invoked in the
// same order as they are added.
func (f *Flame) Use(handlers ...Handler) {
validateAndWrapHandlers(handlers, nil)
f.handlers = append(f.handlers, handlers...)
}
// Handlers sets the entire middleware stack with the given Handlers. This will
// clear any current middleware handlers, and panics if any of the handlers is
// not a callable function.
func (f *Flame) Handlers(handlers ...Handler) {
f.handlers = make([]Handler, 0, len(handlers))
for _, handler := range handlers {
f.Use(handler)
}
}
// Action sets the final handler that will be called after all handlers have
// been invoked.
func (f *Flame) Action(h Handler) {
f.action = validateAndWrapHandler(h, nil)
}
// BeforeHandler is a handler executes at beginning of every request. Flame
// instance stops further process when it returns true.
type BeforeHandler func(rw http.ResponseWriter, req *http.Request) bool
// Before allows for a handler to be called before matching any route. Multiple
// calls to this method will queue up handlers, and handlers will be called in
// the FIFO manner.
func (f *Flame) Before(h BeforeHandler) {
f.befores = append(f.befores, h)
}
func (f *Flame) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if f.urlPrefix != "" {
r.URL.Path = strings.TrimPrefix(r.URL.Path, f.urlPrefix)
}
for _, h := range f.befores {
if h(w, r) {
return
}
}
f.Router.ServeHTTP(w, r)
}
// Run starts the HTTP server on "0.0.0.0:2830". The listen address can be
// altered by the environment variable "FLAMEGO_ADDR". The instance can be
// stopped by calling `Flame.Stop`.
func (f *Flame) Run(args ...interface{}) {
logger := f.logger.WithPrefix("🧙 Flamego")
host := "0.0.0.0"
port := "2830"
if addr := os.Getenv("FLAMEGO_ADDR"); addr != "" {
fields := strings.SplitN(addr, ":", 2)
host = fields[0]
port = fields[1]
}
if len(args) == 1 {
switch arg := args[0].(type) {
case string:
host = arg
case int:
port = strconv.Itoa(arg)
default:
logger.Print("Ignoring invalid type of argument", "type", fmt.Sprintf("%T", arg), "value", arg)
}
} else if len(args) >= 2 {
if arg, ok := args[0].(string); ok {
host = arg
}
if arg, ok := args[1].(int); ok {
port = strconv.Itoa(arg)
}
}
addr := host + ":" + port
logger.Print("Serving on http://localhost:"+port, "env", Env())
server := &http.Server{
Addr: addr,
Handler: f,
ReadHeaderTimeout: 3 * time.Second,
}
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Fatal("Failed to start server", "error", err)
}
}()
<-f.stop
if err := server.Shutdown(gocontext.Background()); err != nil {
logger.Fatal("Failed to shutdown server", "error", err)
}
logger.Print("Server stopped")
}
// Stop stops the server started by the Flame instance.
func (f *Flame) Stop() {
f.stop <- struct{}{}
}
// EnvType defines the runtime environment.
type EnvType string
const (
EnvTypeDev EnvType = "development"
EnvTypeProd EnvType = "production"
EnvTypeTest EnvType = "test"
)
var env = func() atomic.Value {
var v atomic.Value
v.Store(EnvTypeDev)
return v
}()
// Env returns the current runtime environment. It can be altered by SetEnv or
// the environment variable "FLAMEGO_ENV".
func Env() EnvType {
return env.Load().(EnvType)
}
// SetEnv sets the current runtime environment. Valid values are EnvTypeDev,
// EnvTypeProd and EnvTypeTest, all else ignored.
func SetEnv(e EnvType) {
if e == EnvTypeDev ||
e == EnvTypeProd ||
e == EnvTypeTest {
env.Store(e)
}
}
func init() {
SetEnv(EnvType(os.Getenv("FLAMEGO_ENV")))
}