/
routes.go
54 lines (47 loc) · 1.28 KB
/
routes.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
package routes
import (
"fmt"
"io/fs"
"net/http/httputil"
"net/url"
"os"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase/apis"
)
func Register(router *echo.Echo) {
// Serve static files
if os.Getenv("APP_ENV") == "production" {
fileSystem := echo.MustSubFS(router.Filesystem, "static")
router.GET("/*", spaFS(fileSystem))
} else {
fmt.Println(" - Development mode -- proxy frontend to http://web:5173")
router.GET("/*", reverseProxy("http://web:5173"))
}
router.GET("/api/me", func(c echo.Context) error {
if c.Get(apis.ContextAdminKey) != nil {
return c.JSON(200, c.Get("admin"))
} else {
return c.JSON(200, c.Get("user"))
}
})
}
func spaFS(fileSystem fs.FS) echo.HandlerFunc {
staticHandler := echo.StaticDirectoryHandler(fileSystem, true)
return func(c echo.Context) error {
// If err is echo.ErrNotFound, serve root
err := staticHandler(c)
if err == echo.ErrNotFound {
return c.FileFS(".", fileSystem) // SPA time!
}
return err
}
}
// Simple reverse proxy for development mode (don't use in production)
func reverseProxy(host string) echo.HandlerFunc {
remote, _ := url.Parse(host)
proxy := httputil.NewSingleHostReverseProxy(remote)
return func(c echo.Context) error {
proxy.ServeHTTP(c.Response(), c.Request())
return nil
}
}