-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (64 loc) · 1.52 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
package main
import (
"embed"
"log/slog"
"os"
"os/signal"
"time"
"github.com/padi2312/dorcs/pkg/dorcs"
"github.com/padi2312/dorcs/pkg/server"
"github.com/padi2312/dorcs/pkg/utils"
)
var version = "0.5.0"
//go:embed frontend/dist/*
var assets embed.FS
func main() {
isWatchMode := false
isPreviewMode := false
d := dorcs.NewDorcs(assets)
args := os.Args[1:]
if len(args) > 0 {
switch args[0] {
case "--watch":
isWatchMode = true
d.SetWatchMode()
case "--version":
slog.Info("🚀 dorcs v" + version)
os.Exit(0)
case "--preview":
isPreviewMode = true
default:
slog.Error("Invalid argument: " + args[0])
}
}
slog.Info("🚀 Starting dorcs v" + version)
d.Build()
server := server.NewServer(d.Config)
server.EnableStaticServe()
// Only enable websocket in watch mode
if isWatchMode {
server.EnableWebsocket()
}
if isWatchMode {
slog.Info("👓 Watching for changes in dir: " + *d.Config.Source)
go utils.NewFileWatcher(*d.Config.Source).Watch(func(s string) {
start := time.Now()
err := d.ProcessFile(s)
if err != nil {
slog.Error("Error processing file:", err)
}
duration := time.Since(start)
slog.Info("🔄 Rebuild of " + s + " completed in " + duration.Round(time.Millisecond).String())
if d.LatestModifiedUrl != nil {
server.BroadcastMessage(*d.LatestModifiedUrl)
}
})
}
if isWatchMode || isPreviewMode {
quit := make(chan os.Signal, 1)
go server.Start()
signal.Notify(quit, os.Interrupt)
<-quit
slog.Info("🛑 Exiting dorcs...")
}
}