-
Notifications
You must be signed in to change notification settings - Fork 2
/
idiomaticgo.go
61 lines (54 loc) · 1.98 KB
/
idiomaticgo.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
package main
import (
"html/template"
"io"
"log"
"net/http"
"github.com/shurcooL/home/httputil"
"github.com/shurcooL/home/internal/exp/service/notification"
"github.com/shurcooL/home/internal/page/idiomaticgo"
"github.com/shurcooL/httperror"
"github.com/shurcooL/issues"
"github.com/shurcooL/users"
)
var idiomaticGoHTML = template.Must(template.New("").Parse(`<html>
<head>
{{.AnalyticsHTML}} <title>Idiomatic Go</title>
<link href="/icon.svg" rel="icon" type="image/svg+xml">
<meta name="viewport" content="width=device-width">
<link href="/assets/fonts/fonts.css" rel="stylesheet" type="text/css">
<link href="/blog/assets/gfm/gfm.css" rel="stylesheet" type="text/css">
<style type="text/css">
.markdown-body { font-family: Go; }
tt, code, pre { font-family: "Go Mono"; }
</style>
<link href="/assets/idiomaticgo/style.css" rel="stylesheet" type="text/css">
<script async src="/assets/idiomaticgo/idiomaticgo.js"></script>
</head>
<body>`))
func initIdiomaticGo(issues issues.Service, notification notification.Service, usersService users.Service) {
http.Handle("/idiomatic-go", cookieAuth{httputil.ErrorHandler(usersService, func(w http.ResponseWriter, req *http.Request) error {
if req.Method != "GET" {
return httperror.Method{Allowed: []string{"GET"}}
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
data := struct{ AnalyticsHTML template.HTML }{analyticsHTML}
err := idiomaticGoHTML.Execute(w, data)
if err != nil {
return err
}
// Server-side rendering (for now).
authenticatedUser, err := usersService.GetAuthenticated(req.Context())
if err != nil {
log.Println(err)
authenticatedUser = users.User{} // THINK: Should it be a fatal error or not? What about on frontend vs backend?
}
returnURL := req.RequestURI
err = idiomaticgo.RenderBodyInnerHTML(req.Context(), w, issues, notification, authenticatedUser, returnURL)
if err != nil {
return err
}
_, err = io.WriteString(w, `</body></html>`)
return err
})})
}