-
Notifications
You must be signed in to change notification settings - Fork 5
/
goa.go
116 lines (93 loc) · 2.36 KB
/
goa.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 goa
import (
"log"
"net/http"
"sync"
"github.com/goa-go/goa/responser"
"github.com/pkg/errors"
)
// Middleware is based part of goa,
// any processing will take place here.
// should be used liked app.Use(middleware).
type Middleware func(*Context)
// Middlewares is []Middleware.
type Middlewares []Middleware
// Goa is the framework's instance.
type Goa struct {
middlewares Middlewares
pool sync.Pool
}
// New returns the initialized Goa instance.
func New() *Goa {
app := &Goa{}
app.pool.New = func() interface{} {
return &Context{app: app}
}
return app
}
// ServeHTTP makes the app implement the http.Handler interface.
func (app *Goa) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if len(app.middlewares) > 0 {
c := app.pool.Get().(*Context)
// c.middlewares = app.middlewares
c.init(w, r)
app.handleRequest(c)
app.pool.Put(c)
}
}
// Use a middleware.
func (app *Goa) Use(m Middleware) {
app.middlewares = append(app.middlewares, m)
}
// Listen starts server with the addr.
func (app *Goa) Listen(addr string) error {
return http.ListenAndServe(addr, app)
}
func (app *Goa) handleRequest(c *Context) {
defer func() {
if err := recover(); err != nil {
app.onerror(c, err)
}
}()
app.middlewares[0](c)
if !c.redirected && !c.Handled {
app.handleResponse(c)
}
}
func (app *Goa) handleResponse(c *Context) {
// Content-Type
if c.ct != "" {
c.writeContentType(c.ct)
}
// Status code
c.ResponseWriter.WriteHeader(c.status)
// Response
if c.responser == nil {
c.String(http.StatusText(c.status))
}
if err := c.respond(c.responser); err != nil {
log.Printf("[ERROR] %+v", errors.WithStack(err))
c.respond(responser.String{Data: http.StatusText(http.StatusInternalServerError)})
}
}
func (app *Goa) onerror(c *Context, err interface{}) {
code := http.StatusInternalServerError
msg := http.StatusText(http.StatusInternalServerError)
if e, ok := err.(Error); ok {
code = e.Code
msg = e.Msg
} else if e, ok := err.(error); ok {
log.Printf("[ERROR] %+v", errors.WithStack(e))
msg = e.Error()
} else if str, ok := err.(string); ok {
log.Print("[ERROR] ", str)
msg = str
} else {
log.Print("[ERROR] ", err)
}
c.ct = "text/plain; charset=utf-8"
c.writeContentType(c.ct)
c.SetHeader("X-Content-Type-Options", "nosniff")
c.ResponseWriter.WriteHeader(code)
c.respond(responser.String{Data: msg})
}