This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (112 loc) · 2.16 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"github.com/evanw/esbuild/pkg/api"
"github.com/fsnotify/fsnotify"
"github.com/gin-gonic/gin"
)
const uiSrc = "ui-src"
const dist = "dist"
func main() {
refresh()
watcher := startWatching()
defer watcher.Close()
r := gin.Default()
fs := getFS()
r.StaticFS("/ui/", fs)
log.Printf("http://localhost:8080/ui")
r.Run()
}
func refresh() {
log.Println("transpiling & copying")
transpile()
copyStatic()
}
func startWatching() *fsnotify.Watcher {
watcher, err := fsnotify.NewWatcher()
crashOnError(err)
// Start listening for events.
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Has(fsnotify.Write) {
log.Println("modified: ", event.Name)
refresh()
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error: ", err)
}
}
}()
err = watcher.Add(uiSrc)
crashOnError(err)
return watcher
}
func getFS() http.FileSystem {
return http.FS(os.DirFS(dist))
}
func copyStatic() {
for _, f := range []string{
"index.html",
"index.css",
} {
text, err := os.ReadFile(filepath.Join(uiSrc, f))
crashOnError(err)
os.WriteFile(filepath.Join(dist, f), text, 0644)
}
}
func transpile() {
result := api.Build(api.BuildOptions{
EntryPoints: []string{
filepath.Join(uiSrc, "index.ts"),
},
Bundle: true,
Outdir: dist,
MinifySyntax: false,
MinifyWhitespace: false,
MinifyIdentifiers: false,
Sourcemap: api.SourceMapInline,
Engines: []api.Engine{
{Name: api.EngineChrome, Version: "58"},
{Name: api.EngineFirefox, Version: "57"},
{Name: api.EngineSafari, Version: "11"},
{Name: api.EngineEdge, Version: "16"},
},
Write: true,
})
handleErrors(result.Errors)
}
func crashOnError(err error) {
if err != nil {
panic(err)
}
}
func handleErrors(errors []api.Message) {
for _, msg := range errors {
if msg.Location != nil {
fmt.Printf(
"%s:%v:%v: %s\n",
msg.Location.File,
msg.Location.Line,
msg.Location.Column,
msg.Text,
)
} else {
fmt.Println(msg)
}
}
if len(errors) > 0 {
os.Exit(1)
}
}