-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
52 lines (46 loc) · 1.23 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
package rtfblog
import (
"net/http"
"path/filepath"
"time"
)
// server contains a collection of dependencies needed to run the HTTP server.
type server struct {
cryptoHelper CryptoHelper
gctx globalContext
conf Config
mets metrics
}
func newServer(
cryptoHelper CryptoHelper,
gctx globalContext,
conf Config,
) server {
return server{
cryptoHelper: cryptoHelper,
gctx: gctx,
conf: conf,
mets: initMetrics(),
}
}
func (s *server) serveStaticFile(w http.ResponseWriter, req *http.Request, ctx *Context, fileName string) error {
filePath := filepath.Join(s.conf.Server.StaticDir, fileName)
file, err := ctx.assets.Open(filePath)
defer file.Close()
if err != nil {
return err
}
var zero time.Time
http.ServeContent(w, req, fileName, zero, file)
return nil
}
func (s *server) serveRobots(w http.ResponseWriter, req *http.Request, ctx *Context) error {
s.mets.numRobotsServed.Inc()
return s.serveStaticFile(w, req, ctx, "robots.txt")
}
func (s *server) serveFavicon(w http.ResponseWriter, req *http.Request, ctx *Context) error {
if s.conf.Server.Favicon == "" {
return performSimpleStatus(w, http.StatusNotFound)
}
return s.serveStaticFile(w, req, ctx, s.conf.Server.Favicon)
}