-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyo.go
102 lines (87 loc) · 2.36 KB
/
yo.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
package yo
import (
"io/fs"
"os"
"os/exec"
"time"
yoauth "yo/auth"
. "yo/cfg"
yoctx "yo/ctx"
yodb "yo/db"
_ "yo/jobs"
yojobs "yo/jobs"
yolog "yo/log"
yomail "yo/mail"
yosrv "yo/srv"
. "yo/util"
"yo/util/str"
)
var ts2jsInAppSideStaticDir func()
var AppPkgPath = "yo/"
var buildDeployablyNow = func() {}
func init() {
time.Local = time.UTC
}
func Init(staticFileDirYo fs.FS, staticFileDirApp fs.FS) (listenAndServe func()) {
time.Local = time.UTC // repeat of init() above because who knows what happened in between (well, so far, we do. but still =)
AppPkgPath = AppPkgPath[:str.Idx(AppPkgPath, '/')]
yosrv.StaticFileDir_App, yosrv.StaticFileDir_Yo =
staticFileDirApp, staticFileDirYo
yolog.PrintLnLn("DB init...")
if !IsDevMode {
time.Sleep(4 * time.Second)
}
yodb.Ensure[ErrEntry, ErrEntryField]("", nil, false)
db_structs := yodb.InitAndConnectAndMigrateAndMaybeCodegen()
yolog.PrintLnLn("API init...")
yoauth.Init()
listenAndServe = yosrv.InitAndMaybeCodegen(db_structs)
if ts2jsInAppSideStaticDir != nil { // set only in dev-mode
ts2jsInAppSideStaticDir()
}
if os.Getenv("YO_BUILD") != "" {
buildDeployablyNow()
os.Exit(0)
}
yolog.PrintLnLn("Jobs init...")
{
ctx := yoctx.NewCtxNonHttp(yojobs.Timeout1Min, false, "")
defer ctx.OnDone(nil)
yodb.Upsert[yojobs.JobDef](ctx, &yoauth.UserPwdReqJobDef)
yodb.Upsert[yojobs.JobDef](ctx, &yomail.MailReqJobDef)
yodb.Upsert[yojobs.JobDef](ctx, &errJobDef)
yojobs.Init(ctx) // some db clean-ups in there, doesn't `Engine.Resume` though, that's below
listen_and_serve := listenAndServe
listenAndServe = func() {
go yojobs.Default.Resume()
listen_and_serve()
}
}
yolog.PrintLnLn("yo.Init done")
if IsDevMode && os.Getenv("NO_FE") == "" {
go runFrontend()
}
return
}
func runFrontend() {
var cmd *exec.Cmd
const prefer_client_app = false
if dir_client_app := "guis/wails"; prefer_client_app && FsIsDir(dir_client_app) {
cmd = exec.Command("wails", "dev")
cmd.Dir = dir_client_app
} else {
port := str.FromInt(Cfg.YO_API_HTTP_PORT)
cmd = exec.Command("wbd",
// "about:"+port,
"http://localhost:"+port+"?"+str.FromI64(time.Now().UnixNano(), 36),
// "http://localhost:"+port+"/__yostatic/yo.html?"+str.FromI64(time.Now().UnixNano(), 36),
)
}
if err := cmd.Start(); err != nil {
panic(err)
}
if err := cmd.Wait(); err != nil {
panic(err)
}
os.Exit(0)
}