generated from fun-stack/example
-
Notifications
You must be signed in to change notification settings - Fork 4
/
httpserver.go
109 lines (84 loc) · 3.7 KB
/
httpserver.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
package main
import (
"embed"
"io/fs"
"net/http"
"os"
"time"
// "github.com/dyninc/qstring"
"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"
)
const (
// writeTimeout = 2500 * time.Millisecond
writeTimeout = 60 * time.Second
readHeaderTimeout = 5 * time.Second
)
//go:embed static
var staticFS embed.FS
func (app app) httpServer(onPanic func(error)) *http.Server {
l := app.logger
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
listenAddress := os.Getenv("LISTEN_ADDRESS")
staticRoot, err := fs.Sub(staticFS, "static")
if err != nil {
LogFatal(l, "fs.Sub", err)
}
server := &http.Server{
Addr: listenAddress + ":" + port,
WriteTimeout: writeTimeout - 100*time.Millisecond,
ReadHeaderTimeout: readHeaderTimeout,
}
router := httprouter.New()
router.GET("/static/*filepath", app.serveFiles(http.FS(staticRoot)))
router.GET("/", middleware("hntop", l, onPanic, app.frontpageHandler("hntop")))
// router.GET("/hntop", middleware("hntop", l, onPanic, app.frontpageHandler("hntop")))
router.GET("/new", middleware("new", l, onPanic, app.frontpageHandler("new")))
router.GET("/top", middleware("top", l, onPanic, app.frontpageHandler("hntop")))
router.GET("/best", middleware("best", l, onPanic, app.frontpageHandler("best")))
router.GET("/best-upvoterate", middleware("best-upvoterate", l, onPanic, app.frontpageHandler("best-upvoterate")))
router.GET("/best-upvoterate-fatigue", middleware("best-upvoterate-fatigue", l, onPanic, app.frontpageHandler("best-upvoterate-fatigue")))
router.GET("/ask", middleware("ask", l, onPanic, app.frontpageHandler("ask")))
router.GET("/show", middleware("show", l, onPanic, app.frontpageHandler("show")))
router.GET("/raw", middleware("raw", l, onPanic, app.frontpageHandler("raw")))
router.GET("/highdelta", middleware("highdelta", l, onPanic, app.frontpageHandler("highdelta")))
router.GET("/lowdelta", middleware("lowdelta", l, onPanic, app.frontpageHandler("lowdelta")))
router.GET("/stats", middleware("stats", l, onPanic, app.statsHandler()))
router.GET("/about", middleware("about", l, onPanic, app.aboutHandler()))
router.POST("/vote", middleware("upvote", l, onPanic, app.voteHandler()))
router.GET("/score", middleware("score", l, onPanic, app.scoreHandler()))
router.GET("/login", middleware("login", l, onPanic, app.loginHandler()))
router.GET("/logout", middleware("logout", l, onPanic, app.logoutHandler()))
router.GET("/health", middleware("health", l, onPanic, app.healthHandler()))
server.Handler = app.preRouterMiddleware(router, writeTimeout-100*time.Millisecond)
return server
}
func (app app) frontpageHandler(ranking string) func(http.ResponseWriter, *http.Request, OptionalFrontPageParams) error {
return func(w http.ResponseWriter, r *http.Request, params OptionalFrontPageParams) error {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
err := app.serveFrontPage(r, w, ranking, params.WithDefaults())
return errors.Wrap(err, "serveFrontPage")
}
}
func (app app) statsHandler() func(http.ResponseWriter, *http.Request, StatsPageParams) error {
return func(w http.ResponseWriter, r *http.Request, params StatsPageParams) error {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
userID := app.getUserID(r)
err := app.statsPage(w, r, params, userID)
if err != nil {
return err
}
return err
}
}
func (app app) serveFiles(root http.FileSystem) func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
fileServer := http.FileServer(root)
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Set("Cache-Control", "public, max-age=86400") // 1 hours
r.URL.Path = p.ByName("filepath")
fileServer.ServeHTTP(w, r)
}
}