-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
168 lines (141 loc) · 3.69 KB
/
main.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
package main
import (
"context"
"crypto/tls"
"embed"
"html/template"
"io"
"os"
"os/signal"
"strings"
"time"
"net/http"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
"golang.org/x/crypto/acme"
"golang.org/x/crypto/acme/autocert"
)
//go:embed static
var staticFiles embed.FS
//go:embed templates
var templateFiles embed.FS
type Template struct {
templates *template.Template
}
type PageData struct {
PageTitle string
PageID string
Heading string
Content template.HTML
}
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
if err := t.templates.ExecuteTemplate(w, name, data); err != nil {
c.Logger().Error(err)
return err
}
return nil
}
func Essay(c echo.Context) error {
essayName := c.Param("essay")
c.Logger().Info("Reading essay: ", essayName)
fileName := essayName + ".md"
post, err := GetMarkdown("essay/" + fileName)
c.Logger().Info("Reading file: ", fileName)
if err != nil {
return err
}
title := FileNameToTitle(string(fileName))
pageTitle := title + " - DEAD DROP"
data := PageData{
PageTitle: pageTitle,
PageID: "Essay",
Heading: title,
Content: template.HTML(post),
}
return c.Render(http.StatusOK, "default", data)
}
func Home(c echo.Context) error {
posts, err := GetAllEssaysAsListItems()
if err != nil {
return err
}
allPosts := "<ul>" + strings.Join(posts, "") + "</ul>"
data := PageData{
PageTitle: "DEAD DROP",
PageID: "Home",
Heading: "Essays",
Content: template.HTML(allPosts),
}
return c.Render(http.StatusOK, "default", data)
}
func About(c echo.Context) error {
fileName := "single-pages/about.md"
aboutText, err := GetMarkdown(fileName)
if err != nil {
return err
}
heading := "About"
pageTitle := "About - DEAD DROP"
data := PageData{
PageTitle: pageTitle,
PageID: "About",
Heading: heading,
Content: template.HTML(aboutText),
}
return c.Render(http.StatusOK, "default", data)
}
func main() {
e := echo.New()
e.Logger.SetLevel(log.INFO)
e.Use(middleware.Recover())
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "${time_rfc3339} ${status} ${method} ${uri} ${latency_human}\n",
}))
t := &Template{
templates: template.Must(template.ParseFS(templateFiles, "templates/*.html")),
}
e.Renderer = t
e.GET("/", Home)
e.GET("/about", About)
e.GET("/essay/:essay", Essay)
e.Static("/static", "static")
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
go func() {
// check for --dev flag
if len(os.Args) > 1 && os.Args[1] == "--dev" {
e.Logger.Fatal(e.Start(":4000"))
} else {
// redirect http to https
e.Pre(middleware.HTTPSRedirect())
// redirect www to non-www
e.Pre(middleware.HTTPSNonWWWRedirect())
autoTLSManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
// Cache certificates to avoid issues with rate limits (https://letsencrypt.org/docs/rate-limits)
Cache: autocert.DirCache("/var/www/.cache"),
//HostPolicy: autocert.HostWhitelist("<DOMAIN>"),
}
s := http.Server{
Addr: ":443",
Handler: e, // set Echo as handler
TLSConfig: &tls.Config{
//Certificates: nil, // <-- s.ListenAndServeTLS will populate this field
GetCertificate: autoTLSManager.GetCertificate,
NextProtos: []string{acme.ALPNProto},
},
//ReadTimeout: 30 * time.Second, // use custom timeouts
}
if err := s.ListenAndServeTLS("", ""); err != http.ErrServerClosed {
e.Logger.Fatal(err)
}
}
}()
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
}
}