/
serve.go
292 lines (259 loc) · 7.29 KB
/
serve.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package cmd
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"os"
"path/filepath"
"sort"
"sync"
"time"
"github.com/Palats/mapshot/embed"
"github.com/golang/glog"
"github.com/spf13/cobra"
)
// shotInfo gives internal information about a single mapshot.
type shotInfo struct {
name string
// HTTP path were the tiles & data is served.
path string
// Name of the save. Always uses slashes.
savename string
json *MapshotJSON
// Filesystem path of this mapshot.
fsPath string
}
// ShotsJSON is the data sent to the UI to build the listing.
type ShotsJSON struct {
All []*ShotsJSONSave `json:"all"`
}
// ShotsJSONSave is part of ShotsJSON.
type ShotsJSONSave struct {
Savename string `json:"savename"`
Versions []*ShotsJSONInfo `json:"versions"`
}
// ShotsJSONInfo is part of ShotsJSONSave.
type ShotsJSONInfo struct {
Name string `json:"name,omitempty"`
Path string `json:"path,omitempty"`
TicksPlayed int64 `json:"ticks_played,omitempty"`
}
// MapshotJSON is a partial representation of the content of mapshot.json.
type MapshotJSON struct {
// Many field omitted that are not used from go.
TicksPlayed int64 `json:"ticks_played,omitempty"`
}
// MapshotConfigJSON is a representation of the viewer configuration.
type MapshotConfigJSON struct {
Path string `json:"path"`
}
func findShots(baseDir string) ([]shotInfo, error) {
realDir, err := filepath.EvalSymlinks(baseDir)
if err != nil {
return nil, fmt.Errorf("unable to eval symlinks for %s: %w", baseDir, err)
}
glog.Infof("Looking for shots in %s", realDir)
var shots []shotInfo
err = filepath.Walk(realDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Base(path) != "mapshot.json" {
return nil
}
glog.Infof("found mapshot.json: %s", path)
raw, err := ioutil.ReadFile(path)
if err != nil {
glog.Errorf("file %s is not readable", path)
return nil
}
mapshotData := &MapshotJSON{}
if err := json.Unmarshal(raw, mapshotData); err != nil {
glog.Errorf("file %s does not have valid JSON", path)
return nil
}
shotPath := filepath.Dir(path)
relpath, err := filepath.Rel(realDir, shotPath)
if err != nil {
glog.Infof("unable to get relative path of %q: %v", shotPath, err)
return nil
}
savename := filepath.ToSlash(filepath.Dir(relpath))
shots = append(shots, shotInfo{
fsPath: shotPath,
name: filepath.ToSlash(relpath),
savename: savename,
json: mapshotData,
path: "/data/" + filepath.ToSlash(relpath) + "/",
})
return nil
})
if err != nil {
return nil, err
}
return shots, nil
}
// Server implements a server presenting available mapshots and serving their
// content.
type Server struct {
baseDir string
listingMux, viewerMux http.Handler
m sync.Mutex
mux *http.ServeMux
}
func newServer(baseDir string, listingMux, viewerMux http.Handler) *Server {
s := &Server{
baseDir: baseDir,
listingMux: listingMux,
viewerMux: viewerMux,
}
s.updateMux()
return s
}
// watch regularly updates the list of available maps. Current implementation is
// the dumbest possible one - it just rescan files every few seconds and
// recreate a completely new mux in that case.
func (s *Server) watch(ctx context.Context) {
for {
// Update list of maps regular, with some fuzzing.
delay := time.Duration(8000+rand.Int63n(2000)) * time.Millisecond
select {
case <-time.After(delay):
case <-ctx.Done():
return
}
s.updateMux()
}
}
func (s *Server) updateMux() {
// Find all existing mapshots.
shots, err := findShots(s.baseDir)
if err != nil {
shots = nil
glog.Errorf("unable to find mapshots at %s: %v", s.baseDir, err)
}
// Build shots.json
sort.Slice(shots, func(i, j int) bool {
return shots[i].json.TicksPlayed > shots[j].json.TicksPlayed
})
kwShots := map[string]*ShotsJSONSave{}
var savenames []string
for _, shot := range shots {
if kwShots[shot.savename] == nil {
savenames = append(savenames, shot.savename)
kwShots[shot.savename] = &ShotsJSONSave{
Savename: shot.savename,
}
}
kwShots[shot.savename].Versions = append(kwShots[shot.savename].Versions, &ShotsJSONInfo{
Name: shot.name,
Path: shot.path,
TicksPlayed: shot.json.TicksPlayed,
})
}
sort.Strings(savenames)
var data ShotsJSON
for _, savename := range savenames {
data.All = append(data.All, kwShots[savename])
}
jsonData, err := json.Marshal(data)
if err != nil {
jsonData = nil
glog.Errorf("unable to build shots.json: %v", err)
}
// Serve each shot data
mux := http.NewServeMux()
for _, shot := range shots {
mux.Handle(shot.path, http.StripPrefix(shot.path, http.FileServer(http.Dir(shot.fsPath))))
}
// Serve pointer to latest
for _, versions := range data.All {
savename := versions.Savename
if len(versions.Versions) < 1 {
continue
}
latest := versions.Versions[0]
cfg := &MapshotConfigJSON{
Path: latest.Path,
}
jsonCfg, err := json.Marshal(cfg)
if err != nil {
glog.Errorf("unable to build mapshot config: %v", err)
}
mux.HandleFunc("/latest/"+savename, func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write(jsonCfg)
})
}
// Serve basic site.
mux.Handle("/", s.listingMux)
mux.HandleFunc("/shots.json", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
})
// Serve map viewer.
mux.Handle("/map/", http.StripPrefix("/map", s.viewerMux))
s.m.Lock()
defer s.m.Unlock()
// Only update if reading did not fail - or if it was the first call, to
// make sure we always have a mux.
if shots != nil || s.mux == nil {
s.mux = mux
}
}
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
s.m.Lock()
mux := s.mux
s.m.Unlock()
mux.ServeHTTP(w, req)
}
var cmdServe = &cobra.Command{
Use: "serve",
Short: "Start a HTTP server giving access to mapshot generated data.",
Long: `Start a HTTP server giving access to mapshot generated data.
It serves data from Factorio script-output directory.
`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
baseDir, err := factorioSettings.ScriptOutput()
if err != nil {
return err
}
fmt.Printf("Serving data from %s\n", baseDir)
s := newServer(baseDir, builtinListingMux, builtinViewerMux)
go s.watch(cmd.Context())
addr := fmt.Sprintf(":%d", port)
fmt.Printf("Listening on %s ...\n", addr)
return http.ListenAndServe(addr, s)
},
}
func buildMux(files map[string]string) *http.ServeMux {
mux := http.NewServeMux()
for fname, content := range files {
fname := fname
content := content
mux.HandleFunc("/"+fname, func(w http.ResponseWriter, req *http.Request) {
b := bytes.NewReader([]byte(content))
http.ServeContent(w, req, fname, builtinModTime, b)
})
if fname == "index.html" {
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
b := bytes.NewReader([]byte(content))
http.ServeContent(w, req, fname, builtinModTime, b)
})
}
}
return mux
}
var port int
var builtinModTime = time.Now()
var builtinListingMux = buildMux(embed.ListingFiles)
var builtinViewerMux = buildMux(embed.ViewerFiles)
func init() {
cmdServe.PersistentFlags().IntVar(&port, "port", 8080, "Port to listen on.")
cmdRoot.AddCommand(cmdServe)
}