-
Notifications
You must be signed in to change notification settings - Fork 0
/
sola.go
119 lines (98 loc) · 2.09 KB
/
sola.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
package sola
import (
"net/http"
"sync"
"github.com/jinzhu/gorm"
)
type (
// Context for Middleware
Context interface {
// Set/Get
Store() map[string]interface{}
Origin() Context
Shadow() Context
Set(key string, value interface{})
Get(key string) interface{}
// API
Sola() *Sola
SetCookie(cookie *http.Cookie)
Request() *http.Request
Response() http.ResponseWriter
// Writer
Blob(code int, contentType string, bs []byte) (err error)
HTML(code int, data string) error
String(code int, data string) error
JSON(code int, data interface{}) error
File(f File) (err error)
// Reader
GetJSON(data interface{}) error
// Handler
Handle(code int) Handler
}
context struct {
origin Context
lock sync.RWMutex
store map[string]interface{}
}
// Handler func
Handler func(Context) error
// Middleware func
Middleware func(Handler) Handler
// C alias of Context
C Context
// H alias of Handler
H Handler
// M func
M func(c C, next H) error
// Sola App
Sola struct {
// core
middlewares []Middleware
// handler
handlers map[int]Handler // not 500
ErrorHandler func(error, Context) // 500
// config
devMode bool
// orm
orm map[string]*gorm.DB
}
)
// New Sola App
func New() *Sola {
return &Sola{
middlewares: []Middleware{},
handlers: map[int]Handler{
HandleCodePass: HandlePass,
},
orm: map[string]*gorm.DB{},
}
}
// Dev Mode
func (s *Sola) Dev() {
s.devMode = true
}
// Use Middleware
func (s *Sola) Use(ms ...Middleware) {
s.middlewares = append(s.middlewares, ms...)
}
// ServeHTTP to impl http handler
func (s *Sola) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c := newContext()
c.Set(CtxSola, s)
c.Set(CtxRequest, r)
c.Set(CtxResponse, w)
h := Merge(s.middlewares...).Handler()
if err := h(c); err != nil {
if s.ErrorHandler != nil {
s.ErrorHandler(err, c)
} else if s.devMode {
if ie, ok := err.(*InternalError); ok {
ie.Write(w)
return
}
c.String(http.StatusInternalServerError, err.Error())
} else {
c.String(http.StatusInternalServerError, "Internal Server Error")
}
}
}