-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (54 loc) · 1.4 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
package main
import (
"html/template"
"net/http"
"os"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/themisir/blocks/assets"
"github.com/themisir/blocks/renderer"
)
func should(e *echo.Echo, err error) {
if err != nil {
e.Logger.Fatal(err)
}
}
func env(name string, fallback string) string {
if value, ok := os.LookupEnv(name); ok {
return value
}
return fallback
}
func main() {
e := echo.New()
addr := env("ADDRESS", ":1323")
db := env("DB", "data.sqlite3")
baseUrl := env("BASE_URL", "http://localhost:1323/")
jwksUrl := env("JWKS_URL", "")
handler, err := NewHandler(db, baseUrl)
should(e, err)
e.Renderer, err = renderer.Template(e, assets.FS, "views", template.FuncMap{
"AuthorDisplay": AuthorDisplay,
"SafeHtml": func(s string) template.HTML {
return template.HTML(s)
},
})
should(e, err)
e.Pre(middleware.RemoveTrailingSlash())
e.Use(middleware.Logger())
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Filesystem: http.FS(assets.FS),
Root: "static",
}))
if jwksUrl == "" {
e.Use(AnonymousAccess())
} else {
e.Use(Authorize(jwksUrl))
}
e.GET("/", handler.HandleGetPosts)
e.GET("/posts/:id", handler.HandleGetSinglePost)
e.POST("/posts", handler.HandleCreatePost)
e.POST("/posts/:id/replies", handler.HandleCreatePost)
e.POST("/posts/:id/delete", handler.HandleDeletePost)
should(e, e.Start(addr))
}