forked from viamrobotics/rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (126 loc) · 3.41 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Package main provides a utility to run a remote control only web server. It is configured with
a config file that looks like the following:
{
"webrtc_enabled": true,
"host": "something.0000000000.viam.cloud",
"webrtc_signaling_address": "https://app.viam.com:443",
"webrtc_additional_ice_servers": [{"credential":"cred","urls":"turn:global.turn.twilio.com:3478","username":"cred"}],
"baked_auth": {
"authEntity": "something.0000000000.viam.cloud",
"creds": {
"type": "some-key",
"payload": "payload"
}
}
}
# Usage
# Run with development server
ENV=development go run go.viam.com/rdk/web/cmd/directremotecontrol --config ~/some/config_like_above.json
*/
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"html/template"
"io/fs"
"net"
"net/http"
"os"
"time"
"github.com/Masterminds/sprig"
"github.com/NYTimes/gziphandler"
"github.com/edaniels/golog"
"go.viam.com/utils"
"goji.io"
"goji.io/pat"
robotweb "go.viam.com/rdk/robot/web"
"go.viam.com/rdk/web"
)
// Arguments for the command.
type Arguments struct {
Config string `flag:"config,required"`
Port utils.NetPortFlag `flag:"port,default=8080"`
}
var logger = golog.NewDebugLogger("robot_server")
func main() {
utils.ContextualMain(mainWithArgs, logger)
}
func mainWithArgs(ctx context.Context, args []string, logger golog.Logger) error {
var argsParsed Arguments
if err := utils.ParseFlags(args, &argsParsed); err != nil {
return err
}
configData, err := os.ReadFile(argsParsed.Config)
if err != nil {
return err
}
var data robotweb.AppTemplateData
if err := json.Unmarshal(configData, &data); err != nil {
return err
}
if data.Host == "" {
return errors.New("must set host in config")
}
if data.Env == "" {
if os.Getenv("ENV") == "development" {
data.Env = "development"
} else {
data.Env = "production"
}
}
listener, err := net.Listen("tcp", fmt.Sprintf("localhost:%s", argsParsed.Port.String()))
if err != nil {
return err
}
mux := goji.NewMux()
embedFS, err := fs.Sub(web.AppFS, "runtime-shared/static")
if err != nil {
return err
}
staticDir := http.FS(embedFS)
t := template.New("foo").Funcs(template.FuncMap{
//nolint:gosec
"jsSafe": func(js string) template.JS {
return template.JS(js)
},
//nolint:gosec
"htmlSafe": func(html string) template.HTML {
return template.HTML(html)
},
}).Funcs(sprig.FuncMap())
t, err = t.ParseFS(web.AppFS, "runtime-shared/templates/*.html")
if err != nil {
return err
}
template := t.Lookup("webappindex.html")
mux.Handle(pat.Get("/static/*"), gziphandler.GzipHandler(http.StripPrefix("/static", http.FileServer(staticDir))))
mux.Handle(pat.Get("/"), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
dataCopy := data
if err := r.ParseForm(); err != nil {
logger.Debugw("failed to parse form", "error", err)
}
if r.Form.Get("grpc") == "true" {
data.WebRTCEnabled = false
}
if err := template.Execute(w, dataCopy); err != nil {
logger.Debugw("couldn't execute web page", "error", err)
}
}))
httpServer := http.Server{
Handler: mux,
ReadHeaderTimeout: time.Second * 5,
}
utils.PanicCapturingGo(func() {
<-ctx.Done()
defer func() {
if err := httpServer.Shutdown(context.Background()); err != nil {
logger.Errorw("error shutting down", "error", err)
}
}()
})
logger.Infow("serving", "address", listener.Addr().String())
return httpServer.Serve(listener)
}