-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathticode.go
More file actions
108 lines (86 loc) · 2.58 KB
/
Copy pathticode.go
File metadata and controls
108 lines (86 loc) · 2.58 KB
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
package main
import (
"context"
"embed"
"errors"
"flag"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/thingsdb/ThingsCode/app"
"github.com/thingsdb/ThingsCode/install"
)
//go:embed dist
var webContent embed.FS
func main() {
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatalf("Failed to detect user home directory: %v", err)
}
defaultSettingsPath := filepath.Join(homeDir, ".config", "ThingsCode", "settings.json")
settingsFilePtr := flag.String("settings-file", defaultSettingsPath, "Path to the settings JSON file")
httpPortPtr := flag.Uint("port", 6213, "Specific port for the HTTP webserver")
disableOpenBrowser := flag.Bool("disable-open-browser", false, "Disable opening ThingsCode in your default browser")
cmdInstall := flag.Bool("install", false, "Install ThingsCode")
cmdVersion := flag.Bool("version", false, "Print version and exit")
// Parse arguments
flag.Parse()
if *cmdInstall {
install.Install()
os.Exit(0)
}
if *cmdVersion {
fmt.Printf("ThingsCode Version %s\n", app.Version)
os.Exit(0)
}
settingsFile := *settingsFilePtr
settings := app.InitSettings(settingsFile)
// This strips the "dist" prefix so files are served from root.
webFS, err := fs.Sub(webContent, "dist")
if err != nil {
log.Fatal(err)
}
// Create a file server that serves from our embedded filesystem
fileServer := http.FileServer(http.FS(webFS))
// Serve static files at the root path
http.Handle("/", fileServer)
// Serve WebSockets
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
app.ServeWs(w, r)
})
server := &http.Server{
Addr: fmt.Sprintf(":%d", *httpPortPtr),
Handler: nil,
}
shutdownChan := make(chan os.Signal, 1)
signal.Notify(shutdownChan, os.Interrupt, syscall.SIGTERM)
go func() {
log.Printf("Server starting on :%d\n", *httpPortPtr)
if !*disableOpenBrowser {
go func() {
_ = app.OpenUrl(fmt.Sprintf("http://localhost:%d/", *httpPortPtr))
}()
}
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("Critical webserver listener crash: %v", err)
}
}()
sig := <-shutdownChan
log.Printf("Captured signal (%v). Shutdown...\n", sig)
// Max 5 seconds to shutdown...
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Printf("Server forced termination timeout error: %v\n", err)
} else {
log.Println("HTTP network listeners closed cleanly.")
}
settings.CleanTask(0)
log.Println("ThingsCode successfully halted.")
}