-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (84 loc) · 2.48 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
package main
import (
"embed"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/plugins/migratecmd"
_ "phcreery/partman/migrations"
"phcreery/partman/server"
)
//go:embed package.json
var f embed.FS
func GetVersion() string {
// Set the version from package.json
type PackageJSON struct {
Version string `json:"version"`
}
content, err := f.ReadFile("package.json")
if err != nil {
fmt.Println(err)
}
var packageJSON PackageJSON
json.Unmarshal(content, &packageJSON)
if err != nil {
log.Fatal("Error during Unmarshal(): ", err)
}
var Version = packageJSON.Version
return Version
}
//go:embed all:dist-ui
//go:embed dist-ui/*
var distDir embed.FS
// DistDirFS contains the embedded dist-ui directory files (without the "dist-ui" prefix)
var DistDirFS = echo.MustSubFS(distDir, "dist-ui")
const uiPath = "/" // trailedAdminPath
// bindStaticAdminUI registers the endpoints that serves the static admin UI.
// https://github.com/pocketbase/pocketbase/apis/base.go
func bindStaticAdminUI(app core.App, e *core.ServeEvent) error {
// redirect to trailing slash to ensure that relative urls will still work properly
// e.Router.GET(
// strings.TrimRight(uiPath, "/"),
// func(c echo.Context) error {
// return c.Redirect(http.StatusTemporaryRedirect, uiPath)
// },
// )
// serves static files from the /dist directory
// (similar to echo.StaticFS but with gzip middleware enabled)
e.Router.GET(
uiPath+"*",
apis.StaticDirectoryHandler(DistDirFS, false),
// middleware.Gzip(),
)
return nil
}
func main() {
var Version = GetVersion()
app := pocketbase.New()
app.RootCmd.Use = filepath.Base(os.Args[0]) // "partman"
app.RootCmd.Short = "partman CLI"
app.RootCmd.Version = Version
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
server.AddProxyRequests(app, e)
server.AddDashboardRequests(app, e, Version)
server.AddProjectBuildRoute(app, e)
server.AddImportComponentsRoute(app, e)
server.AddImportProjectComponentsRoute(app, e)
bindStaticAdminUI(app, e)
return nil
})
// server.ComponentTotalStockCounterHook(app)
server.ComponentLogsHook(app)
migratecmd.MustRegister(app, app.RootCmd, &migratecmd.Options{
Automigrate: true, // auto creates migration files when making collection changes
})
if err := app.Start(); err != nil {
log.Fatal(err)
}
}