/
server.go
201 lines (175 loc) · 5.47 KB
/
server.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package server
import (
"context"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/gorilla/mux"
core "github.com/miphilipp/devchat-server/internal"
"github.com/miphilipp/devchat-server/internal/communication/session"
"github.com/miphilipp/devchat-server/internal/communication/websocket"
"github.com/miphilipp/devchat-server/internal/conversations"
"github.com/miphilipp/devchat-server/internal/messaging"
"github.com/miphilipp/devchat-server/internal/user"
"github.com/throttled/throttled"
"golang.org/x/text/language"
)
var matcher = language.NewMatcher([]language.Tag{
language.German,
language.English,
})
type ServerConfig struct {
Addr string
IndexFileName string
AssetsFolder string
AvatarFolder string
MediaFolder string
RootURL string
AllowedRequestsPerMinute int
Webpages []string
}
type Webserver struct {
config ServerConfig
router *mux.Router
Server *http.Server
logger log.Logger
socket *websocket.Server
session *session.Manager
userService user.Service
conversationService conversations.Service
messageService messaging.Service
}
func (s *Webserver) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path, err := filepath.Abs(r.URL.Path)
if err != nil {
level.Error(s.logger).Log("handler", "default", "err", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// prepend the path with the path to the static directory
path = filepath.Join(s.config.AssetsFolder, path)
// check whether a file exists at the given path
_, err = os.Stat(path)
if os.IsNotExist(err) {
// file does not exist, serve index.html
if containsPath(s.config.Webpages, r.URL.Path) {
if pusher, ok := w.(http.Pusher); ok {
s.pushFiles(pusher, r)
}
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, filepath.Join(s.config.AssetsFolder, s.config.IndexFileName))
} else {
writeJSONError(w, core.ErrRessourceDoesNotExist, http.StatusNotFound)
}
return
} else if err != nil {
// if we got an error (that wasn't that the file doesn't exist) stating the
// file, return a 500 internal server error and stop
level.Error(s.logger).Log("handler", "default", "err", err)
writeJSONError(w, core.ErrUnknownError, http.StatusNotFound)
return
}
// otherwise, use http.FileServer to serve the static dir
w.Header().Set("Cache-Control", "max-age=31536000")
http.FileServer(http.Dir(s.config.AssetsFolder)).ServeHTTP(w, r)
}
func (s *Webserver) pushFiles(pusher http.Pusher, r *http.Request) {
options := &http.PushOptions{
Header: http.Header{
"Accept-Encoding": r.Header["Accept-Encoding"],
},
}
cssPath := filepath.Join(s.config.AssetsFolder, "css")
cssFiles, _ := ioutil.ReadDir(cssPath)
for _, file := range cssFiles {
path, _ := filepath.Abs(filepath.Join(cssPath, file.Name()))
if err := pusher.Push(path, options); err != nil {
s.logger.Log("PushFail", err)
}
}
jsPath := filepath.Join(s.config.AssetsFolder, "js")
jsFiles, _ := ioutil.ReadDir(jsPath)
for _, file := range jsFiles {
path, _ := filepath.Abs(filepath.Join(cssPath, file.Name()))
if err := pusher.Push(path, options); err != nil {
s.logger.Log("PushFail", err)
}
}
}
func limitSize(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, 32<<20+1024)
next.ServeHTTP(w, r)
})
}
func parseLanguage(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
accept := r.Header.Get("Accept-Language")
tag, _ := language.MatchStrings(matcher, accept)
copiedRequest := r.WithContext(context.WithValue(r.Context(), "Language", tag))
next.ServeHTTP(w, copiedRequest)
})
}
// New creates a new instance of type Webserver
func New(
cfg ServerConfig,
userService user.Service,
cService conversations.Service,
mService messaging.Service,
socket *websocket.Server,
session *session.Manager,
limiterStore throttled.GCRAStore,
logger log.Logger) *Webserver {
router := mux.NewRouter()
srv := &http.Server{
Handler: router,
Addr: cfg.Addr,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
app := &Webserver{
config: cfg,
router: router,
Server: srv,
userService: userService,
conversationService: cService,
messageService: mService,
logger: logger,
socket: socket,
session: session,
}
if cfg.AllowedRequestsPerMinute == 0 {
cfg.AllowedRequestsPerMinute = 10
}
quota := throttled.RateQuota{MaxRate: throttled.PerMin(cfg.AllowedRequestsPerMinute), MaxBurst: 5}
rateLimiter, err := throttled.NewGCRARateLimiter(limiterStore, quota)
if err != nil {
return nil
}
httpRateLimiter := throttled.HTTPRateLimiter{
RateLimiter: rateLimiter,
VaryBy: &throttled.VaryBy{Path: true, Method: true, RemoteAddr: true},
}
router.Use(limitSize)
router.Use(parseLanguage)
router.Use(httpRateLimiter.RateLimit)
return app
}
// SetupFileServer sets up file server handlers for all webpages and its subpages.
func (s *Webserver) SetupFileServer() {
for _, path := range s.config.Webpages {
s.router.PathPrefix(path).Handler(s).Methods(http.MethodGet)
}
}
func containsPath(paths []string, path string) bool {
for _, p := range paths {
if p == path {
return true
}
}
return false
}