aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go115
1 files changed, 113 insertions, 2 deletions
diff --git a/main.go b/main.go
index 66250be..6e187cb 100644
--- a/main.go
+++ b/main.go
@@ -6,13 +6,19 @@ import (
"log"
"net/http"
"os"
+ "path/filepath"
+ "strings"
"time"
"github.com/labstack/echo/v5"
"github.com/lrstanley/girc"
"github.com/pelletier/go-toml/v2"
"github.com/pocketbase/pocketbase"
+ "github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
+ "github.com/pocketbase/pocketbase/plugins/ghupdate"
+ "github.com/pocketbase/pocketbase/plugins/jsvm"
+ "github.com/pocketbase/pocketbase/plugins/migratecmd"
)
const (
@@ -20,8 +26,7 @@ const (
)
type handlerWrapper struct {
- irc *girc.Client
- // irc chan *girc.Client
+ irc *girc.Client
config TomlConfig
}
@@ -111,6 +116,22 @@ func runIRC(appConfig TomlConfig, ircChan chan *girc.Client) {
}
}
+// func main() {
+// app := pocketbase.New()
+
+// if err := app.Start(); err != nil {
+// log.Fatal(err)
+// }
+// }
+
+func defaultPublicDir() string {
+ if strings.HasPrefix(os.Args[0], os.TempDir()) {
+ return "./pb_public"
+ }
+
+ return filepath.Join(os.Args[0], "../pb_public")
+}
+
func main() {
var appConfig TomlConfig
@@ -140,6 +161,96 @@ func main() {
return nil
})
+ var hooksDir string
+ app.RootCmd.PersistentFlags().StringVar(
+ &hooksDir,
+ "hooksDir",
+ "",
+ "the directory with the JS app hooks",
+ )
+
+ var hooksWatch bool
+ app.RootCmd.PersistentFlags().BoolVar(
+ &hooksWatch,
+ "hooksWatch",
+ true,
+ "auto restart the app on pb_hooks file change",
+ )
+
+ var hooksPool int
+ app.RootCmd.PersistentFlags().IntVar(
+ &hooksPool,
+ "hooksPool",
+ 25,
+ "the total prewarm goja.Runtime instances for the JS app hooks execution",
+ )
+
+ var migrationsDir string
+ app.RootCmd.PersistentFlags().StringVar(
+ &migrationsDir,
+ "migrationsDir",
+ "",
+ "the directory with the user defined migrations",
+ )
+
+ var automigrate bool
+ app.RootCmd.PersistentFlags().BoolVar(
+ &automigrate,
+ "automigrate",
+ true,
+ "enable/disable auto migrations",
+ )
+
+ var publicDir string
+ app.RootCmd.PersistentFlags().StringVar(
+ &publicDir,
+ "publicDir",
+ defaultPublicDir(),
+ "the directory to serve static files",
+ )
+
+ var indexFallback bool
+ app.RootCmd.PersistentFlags().BoolVar(
+ &indexFallback,
+ "indexFallback",
+ true,
+ "fallback the request to index.html on missing static path (eg. when pretty urls are used with SPA)",
+ )
+
+ var queryTimeout int
+ app.RootCmd.PersistentFlags().IntVar(
+ &queryTimeout,
+ "queryTimeout",
+ 30,
+ "the default SELECT queries timeout in seconds",
+ )
+
+ app.RootCmd.ParseFlags(os.Args[1:])
+
+ jsvm.MustRegister(app, jsvm.Config{
+ MigrationsDir: migrationsDir,
+ HooksDir: hooksDir,
+ HooksWatch: hooksWatch,
+ HooksPoolSize: hooksPool,
+ })
+
+ migratecmd.MustRegister(app, app.RootCmd, migratecmd.Config{
+ TemplateLang: migratecmd.TemplateLangJS,
+ Automigrate: automigrate,
+ Dir: migrationsDir,
+ })
+
+ ghupdate.MustRegister(app, app.RootCmd, ghupdate.Config{})
+
+ app.OnAfterBootstrap().PreAdd(func(e *core.BootstrapEvent) error {
+ app.Dao().ModelQueryTimeout = time.Duration(queryTimeout) * time.Second
+ return nil
+ })
+
+ app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
+ e.Router.GET("/*", apis.StaticDirectoryHandler(os.DirFS(publicDir), indexFallback))
+ return nil
+ })
if err := app.Start(); err != nil {
log.Fatal(err)
}