-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathneko.go
116 lines (103 loc) · 2.94 KB
/
neko.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
package neko
import (
"fmt"
"github.com/julienschmidt/httprouter"
"net/http"
"os"
"sync"
)
type (
HandlerFunc func(*Context)
// HtmlEngine is an interface for parsing html templates and redering HTML.
HtmlEngine interface {
Render(view string, context interface{}, status ...int) error
}
Engine struct {
*RouterGroup
AppName string
router *httprouter.Router
allNoRoute []HandlerFunc
pool sync.Pool
}
)
func Version() string {
return "0.0.1"
}
// Classic creates a classic Neko with some basic default middleware - neko.Logger and neko.Recovery.
func Classic(appName ...string) *Engine {
engine := New()
if appName != nil && appName[0] != "" {
engine.AppName = appName[0]
}
engine.Use(Logger())
engine.Use(Recovery())
return engine
}
// New returns a new blank Engine instance without any middleware attached.
func New() *Engine {
engine := &Engine{}
engine.AppName = "NEKO"
engine.RouterGroup = &RouterGroup{
absolutePath: "/",
engine: engine,
}
engine.router = httprouter.New()
engine.router.NotFound = http.HandlerFunc(engine.handle404)
engine.pool.New = func() interface{} {
ctx := &Context{Engine: engine}
return ctx
}
return engine
}
func (c *Engine) Use(middlewares ...HandlerFunc) {
c.RouterGroup.Use(middlewares...)
c.allNoRoute = c.combineHandlers(nil)
}
// ServeHTTP makes the router implement the http.Handler interface.
func (c *Engine) ServeHTTP(res http.ResponseWriter, req *http.Request) {
c.router.ServeHTTP(res, req)
}
// Run run the http server.
func (c *Engine) Run(addr string) error {
fmt.Printf("[%s] Listening and serving HTTP on %s \n", c.AppName, addr)
if err := http.ListenAndServe(addr, c); err != nil {
return err
}
return nil
}
// Run run the https server.
func (c *Engine) RunTLS(addr string, cert string, key string) error {
fmt.Printf("[%s] Listening and serving HTTPS on %s \n", c.AppName, addr)
if err := http.ListenAndServeTLS(addr, cert, key, c); err != nil {
return err
}
return nil
}
func (c *Engine) handle404(w http.ResponseWriter, req *http.Request) {
ctx := c.createContext(w, req, nil, c.allNoRoute)
ctx.Writer.WriteHeader(404)
ctx.Next()
if !ctx.Writer.Written() {
if ctx.Writer.Status() == 404 {
ctx.Writer.Header().Set("Content-Type", "text/html")
ctx.Writer.Write([]byte(`<!DOCTYPE html><html><head><meta charset="UTF-8"><title>404 PAGE NOT FOUND</title></head><body style="padding:0;text-align:center;"><div style="padding-top:1em;font-size:2.5em;">404 PAGE NOT FOUND</div><div style="font-size:1em;color:#999;">Powered by Neko</div></body></html>`))
} else {
ctx.Writer.WriteHeader(ctx.Writer.Status())
}
}
c.reuseContext(ctx)
}
const (
DEV string = "development"
PROD string = "production"
TEST string = "test"
)
// NekoEnv is the environment that Neko is executing in.
// The NEKO_ENV is read on initialization to set this variable.
var NekoEnv = DEV
func init() {
env := os.Getenv("NEKO_ENV")
if len(env) > 0 {
NekoEnv = env
}
}