This repository has been archived by the owner on Jun 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.go
140 lines (120 loc) · 3.87 KB
/
webserver.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
package webserver
import (
"errors"
"time"
"github.com/go-gem/sessions"
routing "github.com/qiangxue/fasthttp-routing"
"github.com/valyala/fasthttp"
"github.com/zekroTJA/slms/internal/auth"
"github.com/zekroTJA/slms/internal/database"
)
// A WebServer handles the REST API
// connections.
type WebServer struct {
db database.Middleware
auth auth.Provider
sessions sessions.Store
config *Config
server *fasthttp.Server
router *routing.Router
limitManager *RateLimitManager
redirectStatus int
}
// Config contains the configuration
// values for the WebServer.
type Config struct {
Address string `json:"address"`
RootRedirect string `json:"root_redirect"`
OnlyHTTPSRootLink bool `json:"only_https_rootlink"`
PermanentRedirect bool `json:"permanent_redirect"`
APITokenHash string `json:"api_token_hash"`
SessionStoreKey string `json:"session_store_key"`
TLS *ConfigTLS `json:"tls"`
}
// ConfigTLS contains the configuration
// values for TLS encryption for the
// WebServer.
type ConfigTLS struct {
Use bool `json:"use"`
CertFile string `json:"cert_file"`
KeyFile string `json:"key_file"`
}
// NewWebServer creates a new instance
// of WebServer and registers all set
// request handlers.
func NewWebServer(conf *Config, db database.Middleware, authProvider auth.Provider) (*WebServer, error) {
if len(conf.APITokenHash) < 8 {
return nil, errors.New("api_token must have at least 8 characters")
}
router := routing.New()
cookieStore := sessions.NewCookieStore([]byte(conf.SessionStoreKey))
cookieStore.MaxAge(600)
ws := &WebServer{
auth: authProvider,
sessions: cookieStore,
db: db,
config: conf,
router: router,
limitManager: NewRateLimitManager(),
server: &fasthttp.Server{
Handler: sessions.ClearHandler(router.HandleRequest),
},
}
if ws.config.PermanentRedirect {
ws.redirectStatus = fasthttp.StatusPermanentRedirect
} else {
ws.redirectStatus = fasthttp.StatusTemporaryRedirect
}
ws.registerHandlers()
return ws, nil
}
func (ws *WebServer) registerHandlers() {
ws.router.Use(ws.handlerHeaderServer, ws.handlerFileServer)
// GET /:SHORT
ws.router.Get("/<short>", ws.handlerShort)
// GROUP # /api
api := ws.router.Group("/api")
api.Use(ws.handlerAuth)
// POST /api/login
api.Post("/login",
ws.limitManager.GetHandler(10*time.Second, 3),
ws.handlerLogin)
// GET /api/shortlinks/count
api.Get("/shortlinks/count",
ws.limitManager.GetHandler(1*time.Second, 10),
ws.handlerGetShortLinkCount)
// GET /api/shortlinks
shortLinks := api.Get("/shortlinks",
ws.limitManager.GetHandler(1*time.Second, 10),
ws.handlerGetShortLinks)
// POST /api/shortlinks
shortLinks.Post(
ws.limitManager.GetHandler(3*time.Second, 3),
ws.handlerCreateShortLink)
// GET /api/shortlinks/:ID
shortLinksID := api.Get("/shortlinks/<id>",
ws.limitManager.GetHandler(1*time.Second, 5),
ws.handlerGetShortLink)
// POST /api/shortlinks/:ID
shortLinksID.Post(
ws.limitManager.GetHandler(2*time.Second, 3),
ws.handlerEditShortLink)
// DELETE /api/shortlinks/:ID
shortLinksID.Delete(
ws.limitManager.GetHandler(2*time.Second, 5),
ws.handlerDeleteShortLink)
}
// ListenAndServeBlocking starts listening for HTTP requests
// and serving responses to the specified address in the config.
// The server will run in TLS mode when set in the config.
// The startet event loop will block the current go routine.
func (ws *WebServer) ListenAndServeBlocking() error {
if ws.config.TLS != nil && ws.config.TLS.Use {
if ws.config.TLS.CertFile == "" || ws.config.TLS.KeyFile == "" {
return errors.New("cert file and key file must be specified")
}
return ws.server.ListenAndServeTLS(
ws.config.Address, ws.config.TLS.CertFile, ws.config.TLS.KeyFile)
}
return ws.server.ListenAndServe(ws.config.Address)
}