-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
93 lines (73 loc) · 1.94 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
package main
import (
"embed"
"fmt"
"io/fs"
"net/http"
"strings"
"isabelroses.com/api"
"isabelroses.com/lib"
"isabelroses.com/pages"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func customHTTPErrorHandler(err error, c echo.Context) {
// Ignore the error for requests to /api URLs
if strings.Contains(c.Path(), "/api") {
c.Logger().Error(err)
return
}
code := http.StatusInternalServerError
if he, ok := err.(*echo.HTTPError); ok {
code = he.Code
}
c.Logger().Error(err)
if err := c.Render(code, fmt.Sprintf("%v", pages.ErrorPage(c, code)), nil); err != nil {
c.Logger().Error(err)
}
}
//go:embed public/*
var PubFS embed.FS
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.RequestID())
e.Use(middleware.Secure())
e.Pre(middleware.RemoveTrailingSlash())
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Level: 5,
}))
e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(50)))
e.HTTPErrorHandler = customHTTPErrorHandler
e.GET("/", pages.Home)
e.GET("/projects", pages.Projects)
e.GET("/donations", pages.Donos)
e.GET("/2004", pages.A2004)
blogGroup := e.Group("/blog")
blogGroup.GET("", pages.Blog)
blogGroup.GET("/:slug", pages.Post)
blogGroup.GET("/tag/:tag", pages.Blog)
apiGroup := e.Group("/api")
apiGroup.POST("/kofi", api.Kofi)
apiGroup.POST("/github", api.Github)
e.GET("/rss.xml", func(c echo.Context) error {
c.Redirect(http.StatusMovedPermanently, "/feed.xml")
return nil
})
e.GET("/feed.xml", func(c echo.Context) error {
atom := lib.AtomFeed()
return c.XML(http.StatusOK, atom)
})
e.GET("/feed.json", func(c echo.Context) error {
json := lib.JSONFeed()
return c.JSON(http.StatusOK, json)
})
strippedFS, err := fs.Sub(PubFS, "public")
if err != nil {
panic(err)
}
fs := http.FS(strippedFS)
e.GET("/*", echo.WrapHandler(http.FileServer(fs)))
e.Logger.Fatal(e.Start(":3000"))
}