-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (53 loc) · 1.79 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
package main
import (
"net/http"
"os"
"strings"
"github.com/labstack/echo/v4"
"bakersfieldtechnology.com/assets"
"bakersfieldtechnology.com/components"
"bakersfieldtechnology.com/components/errorpage"
"bakersfieldtechnology.com/components/homepage"
"bakersfieldtechnology.com/components/privacypolicy"
)
func main() {
app := echo.New()
// https://echo.labstack.com/docs/error-handling#error-pages
customHTTPErrorHandler := func(err error, c echo.Context) {
code := http.StatusInternalServerError
if he, ok := err.(*echo.HTTPError); ok {
code = he.Code
}
if code == 404 {
components.Render(c, 404, errorpage.NotFound())
return
}
components.Render(c, code, errorpage.ServerError())
}
app.HTTPErrorHandler = customHTTPErrorHandler
app.GET("/assets/public/*", echo.WrapHandler(http.StripPrefix("/assets/public", assets.AssetsHandler)))
app.GET("/", func(c echo.Context) error {
return components.Render(c, http.StatusOK, homepage.Homepage())
})
app.GET("/privacy-policy/", func(c echo.Context) error {
return components.Render(c, http.StatusOK, privacypolicy.PrivacyPolicy())
})
// I tried to use echo's middleware.AddTrailingSlashWithConfig, but it
// added a trailing slash to assets and broke URLs. Since this is the only
// route that needs the slash, we'll handle it manually.
app.GET("/privacy-policy", func(c echo.Context) error {
return c.Redirect(301, "/privacy-policy/")
})
// https://pkg.go.dev/github.com/labstack/echo/[email protected]#Echo.StaticFS
app.StaticFS("/*", assets.PublicFiles)
app.RouteNotFound("/*", func(c echo.Context) error {
return components.Render(c, http.StatusOK, homepage.Homepage())
})
port, ok := os.LookupEnv("APP_PORT")
if !ok {
port = "3000"
} else {
port = strings.Trim(port, " ")
}
app.Logger.Fatal(app.Start(":" + port))
}