-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (61 loc) · 2.1 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/undg/go-prapi/buildinfo"
"github.com/undg/go-prapi/json"
"github.com/undg/go-prapi/utils"
"github.com/undg/go-prapi/ws"
)
// @TODO (undg) 2024-10-06: different port for dev and production
func startServer(mux *http.ServeMux) {
mux.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v1/schema/status":
json.ServeStatusSchemaJSON(w, r)
case "/api/v1/schema/message":
json.ServeMessageSchemaJSON(w, r)
case "/api/v1/schema/response":
json.ServeResponseSchemaJSON(w, r)
case "/api/v1/ws":
ws.HandleWebSocket(w, r)
default:
http.NotFound(w, r)
}
})
fs := http.FileServer(http.Dir("/tmp/bin/pr-web/dist"))
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := os.Stat("/tmp/bin/pr-web/dist" + r.URL.Path); os.IsNotExist(err) {
http.ServeFile(w, r, "/tmp/bin/pr-web/dist/index.html")
} else {
fs.ServeHTTP(w, r)
}
}))
}
func main() {
ip := utils.GetLocalIP()
b := buildinfo.Get()
fmt.Print(`
┌───────────────────────────────────────────────────┐
│ GO-PRAPI │
├───────────────────────────────────────────────────┤
GitVersion: `, b.GitVersion, `
GitCommit: `, b.GitCommit, `
BuildDate: `, b.BuildDate, `
Compiler: `, b.Compiler, `
Platform: `, b.Platform, `
GoVersion: `, b.GoVersion, `
└───────────────────────────────────────────────────┘
`)
fmt.Println("\n🔥 Igniting server on ws://" + ip + utils.PORT + "\n")
mux := http.NewServeMux()
startServer(mux)
// @TODO (undg) 2024-10-10: do not broadcast to client that send outgoing update
go ws.BroadcastUpdates()
err := http.ListenAndServe(utils.PORT, mux)
if err != nil {
log.Fatal("ERROR ", err)
}
}