-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgofast.go
118 lines (91 loc) · 2.63 KB
/
gofast.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
// A fast web framework written in Go.
//
// Author: Vincent Composieux <vincent.composieux@gmail.com>
package gofast
import (
"fmt"
"net/http"
"os"
"sort"
"time"
"github.com/sirupsen/logrus"
"golang.org/x/net/http2"
)
const (
PORT string = ":8080"
VERSION string = "1.0-beta"
)
type Gofast struct {
*logrus.Logger
*Router
*Templating
*Middleware
}
// Bootstrap bootstraps a new instance
func Bootstrap() *Gofast {
logrus.WithFields(logrus.Fields{"version": VERSION}).Info("gofast is running")
logger := logrus.New()
router := NewRouter()
templating := NewTemplating()
middleware := NewMiddleware()
return &Gofast{logger, &router, &templating, &middleware}
}
// PrepareHttpServer prepares a HTTP server
func (g *Gofast) PrepareHttpServer() string {
sort.Sort(RouteLen(g.GetRoutes()))
http.Handle("/", g)
assetsDirectory := g.GetAssetsDirectory()
assetsUrl := fmt.Sprintf("/%s/", assetsDirectory)
assetsPrefix := fmt.Sprintf("/%s", assetsDirectory)
http.Handle(assetsUrl, http.StripPrefix(assetsPrefix, http.FileServer(http.Dir(assetsDirectory))))
port := PORT
if p := os.Getenv("PORT"); p != "" {
port = fmt.Sprintf(":%s", p)
}
return port
}
// Listen listens and handles HTTP requests
func (g *Gofast) Listen() {
port := g.PrepareHttpServer()
http.ListenAndServe(port, nil)
}
// ListenHttp2 listens and handles HTTP/2 requests
func (g *Gofast) ListenHttp2(certificate string, key string) {
port := g.PrepareHttpServer()
server := &http.Server{Addr: port, Handler: nil}
http2.ConfigureServer(server, nil)
logrus.Fatal(server.ListenAndServeTLS(certificate, key))
}
// ServeHTTP serves HTTP request by matching the correct route
func (g *Gofast) ServeHTTP(res http.ResponseWriter, req *http.Request) {
matchedRoute := g.GetFallback()
for _, route := range g.GetRoutes() {
if "fallback" == route.name {
continue
}
if (route.method == req.Method || "*" == route.method) && route.pattern.MatchString(req.URL.Path) {
matchedRoute = route
break
}
}
g.HandleRoute(res, req, matchedRoute)
}
// HandleRoute handles a route with the initialized context
func (g *Gofast) HandleRoute(res http.ResponseWriter, req *http.Request, route Route) {
startTime := time.Now()
context := NewContext()
context.SetLogger(g.Logger)
context.SetRoute(&route)
context.SetRequest(req)
context.SetResponse(res)
handler := g.HandleMiddlewares(context)
handler(context)
stopTime := time.Now()
logrus.WithFields(logrus.Fields{
"name": route.name,
"method": req.Method,
"code": context.GetResponse().GetStatusCode(),
"url": req.URL.String(),
"duration": stopTime.Sub(startTime),
}).Info("Route matched")
}