-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
472 lines (399 loc) · 10.8 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"image"
"image/jpeg"
"image/png"
"io"
"io/ioutil"
"log"
"math"
"net/http"
_ "net/http/pprof"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"text/template"
"time"
"github.com/Bredgren/gotracer/webtracer/lib"
"github.com/nfnt/resize"
)
const path = "/src/github.com/Bredgren/gotracer/webtracer"
const imgType = "jpg"
const thumbnailSize = 128
var goPath = os.Getenv("GOPATH")
var templ *template.Template
var (
debug bool
port int
maxHistory int
)
func init() {
flag.BoolVar(&debug, "D", false, "Debug mode. More logs, use unminified assets, etc.")
flag.IntVar(&port, "p", 8080, "Set http port")
flag.IntVar(&maxHistory, "history", 10, "Maximum render history to keep track of")
}
func main() {
setup()
http.HandleFunc("/", httpHandler)
h := &handler{}
http.HandleFunc("/render", h.renderHandler)
http.HandleFunc("/history", h.historyHandler)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}
func setup() {
e := os.Chdir(filepath.Join(goPath, path))
if e != nil {
log.Fatal(e)
}
flag.Parse()
if debug {
log.Println("Debug mode enabled!")
}
templ = template.Must(template.New("templ").Funcs(template.FuncMap{
"debug": func() bool { return debug },
"formatTime": func(t time.Time) string { return t.Format(time.Stamp) },
}).ParseFiles("./tmpl/page.tmpl"))
}
func httpHandler(w http.ResponseWriter, r *http.Request) {
log.Println("host:", r.Host) // TODO: restrict host?
if r.RequestURI != "/" && !strings.Contains(r.RequestURI, "?") {
log.Println("serve file", r.RequestURI)
http.ServeFile(w, r, "./"+r.RequestURI)
return
}
log.Println("handle", r.RequestURI)
defer func() {
if e := r.Body.Close(); e != nil {
log.Printf("Error closing body: %v\n", e)
}
}()
initItem := &lib.RenderItem{}
initScene := r.FormValue("initial")
if initScene != "" {
allHistory, e := getAllHistory()
if e != nil {
msg := fmt.Sprintf("Error getting all history: %v", e)
log.Println(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
found := false
for _, h := range allHistory {
if h.Scene == initScene {
initItem = h
found = true
break
}
}
if !found {
http.Error(w, fmt.Sprintf("Scene not found: %v", initScene), http.StatusNotFound)
return
}
}
initItemJ, e := json.Marshal(initItem)
if e != nil {
msg := fmt.Sprintf("Error marshalling initial item: %v", e)
log.Println(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
e = templ.ExecuteTemplate(w, "Main", &renderPage{
InitialItem: string(initItemJ),
})
if e != nil {
http.Error(w, e.Error(), http.StatusInternalServerError)
}
}
type renderPage struct {
InitialItem string
}
type handler struct {
sync.Mutex
}
func (h *handler) renderHandler(w http.ResponseWriter, r *http.Request) {
log.Println(r.RequestURI)
defer func() {
if e := r.Body.Close(); e != nil {
log.Printf("Error closing body: %v\n", e)
}
}()
if r.Method == "GET" {
log.Println("redirect to /")
http.Redirect(w, r, "/", http.StatusFound)
return
}
// Read scene from body
body, e := ioutil.ReadAll(r.Body)
if e != nil {
msg := fmt.Sprintf("Error reading body of POST: %v", e)
log.Println(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
h.Lock()
defer h.Unlock()
// Run raytrace command
tmpImage := "img/tmp." + r.FormValue("format")
if e := runRaytrace(tmpImage, string(body)); e != nil {
msg := fmt.Sprintf("Error running raytrace: %v", e)
log.Println(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
// Save scene file
tmpScene := "img/tmp.json"
if e := saveScene(tmpScene, body); e != nil {
msg := fmt.Sprintf("Error saving %s: %v", tmpScene, e)
log.Println(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
// Deal with history and get final image name
imgName, e := saveImageAndScene(tmpImage, tmpScene)
if e != nil {
msg := fmt.Sprintf("Error saving/renmaing img/scene files: %v", e)
log.Println(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
// Send final image name back to client
fmt.Fprintln(w, imgName)
}
func (h *handler) historyHandler(w http.ResponseWriter, r *http.Request) {
log.Println(r.RequestURI)
defer func() {
if e := r.Body.Close(); e != nil {
log.Printf("Error closing body: %v\n", e)
}
}()
h.Lock()
defer h.Unlock()
if r.FormValue("_") == "recent" {
history, e := getRecentHistory()
if e != nil {
msg := fmt.Sprintf("Error getting history: %v", e)
log.Println(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
fmt.Fprintln(w, history)
return
}
start, end := r.FormValue("start"), r.FormValue("end")
allHistory, e := getAllHistory()
if e != nil {
msg := fmt.Sprintf("Error getting all history: %v", e)
log.Println(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
// Remember that the oldest item is last
defaultStartDate := allHistory[len(allHistory)-1].Date
defaultEndDate := allHistory[0].Date
// Use default if unspecified
if start == "" {
start = defaultStartDate.Format("2006-01-02")
}
if end == "" {
end = defaultEndDate.Format("2006-01-02")
}
// Parse dates, if malformed then use default
startDate, e := time.ParseInLocation("2006-01-02", start, time.Local)
if e != nil {
log.Printf("Error parsing start time: %v: %v", start, e)
startDate = defaultStartDate
}
endDate, e := time.ParseInLocation("2006-01-02", end, time.Local)
if e != nil {
log.Printf("Error parsing end time: %v: %v\n", end, e)
endDate = defaultEndDate
}
e = templ.ExecuteTemplate(w, "History", &historyPage{
DefaultStartDate: defaultStartDate.Format("2006-01-02"),
DefaultEndDate: defaultEndDate.Format("2006-01-02"),
StartDate: start,
EndDate: end,
Items: historyRange(allHistory, startDate, endDate),
})
if e != nil {
http.Error(w, e.Error(), http.StatusInternalServerError)
}
}
type historyPage struct {
DefaultStartDate, DefaultEndDate string
StartDate, EndDate string
Items []*lib.RenderItem
}
func historyRange(history []*lib.RenderItem, start, end time.Time) []*lib.RenderItem {
if end.Before(start) {
end = start
}
// Add 1 day to include the end day
end = end.Add(time.Duration(24) * time.Hour)
firstIndex := sort.Search(len(history), func(i int) bool {
return !end.Before(history[i].Date)
})
lastIndex := sort.Search(len(history), func(i int) bool {
return !start.Before(history[i].Date)
})
return history[firstIndex:lastIndex]
}
func runRaytrace(imgName, scene string) error {
cmd := exec.Command("raytrace", "-out", imgName)
// Grab stdin and send scene through it
cmdIn, e := cmd.StdinPipe()
if e != nil {
return fmt.Errorf("getting stdin pipe: %v", e)
}
fmt.Fprintln(cmdIn, scene)
if e := cmdIn.Close(); e != nil {
return fmt.Errorf("closing stdin to raytrace: %v", e)
}
if output, e := cmd.CombinedOutput(); e != nil {
return fmt.Errorf("running and getting output: %v: %s", e, string(output))
}
return nil
}
func saveScene(name string, body []byte) error {
sceneFile, e := os.Create(name)
if e != nil {
return fmt.Errorf("opening %s: %v", name, e)
}
_, e = io.Copy(sceneFile, bytes.NewReader(body))
if e != nil {
return fmt.Errorf("copying to %s: %v", name, e)
}
e = sceneFile.Close()
if e != nil {
return fmt.Errorf("closing %s: %v", name, e)
}
return nil
}
func saveImageAndScene(tmpImg, tmpScn string) (newImg string, err error) {
timeStamp := strconv.FormatInt(time.Now().UnixNano(), 10)
base := "img/render-" + timeStamp
newImg = base + filepath.Ext(tmpImg)
newScn := base + ".json"
if e := os.Rename(tmpImg, newImg); e != nil {
return "", fmt.Errorf("renaming %s to %s: %v", tmpImg, newImg, e)
}
if e := os.Rename(tmpScn, newScn); e != nil {
return "", fmt.Errorf("renaming %s to %s: %v", tmpScn, newScn, e)
}
if e := saveThumbnail(newImg); e != nil {
return "", fmt.Errorf("saving thumbnail: %v", e)
}
return
}
func saveThumbnail(imgName string) (err error) {
imgFile, e := os.Open(imgName)
if e != nil {
return fmt.Errorf("opening image %s: %v", imgName, e)
}
defer func() {
if e := imgFile.Close(); e != nil && err == nil {
err = fmt.Errorf("closing img: %v", e)
}
}()
img, _, e := image.Decode(imgFile)
if e != nil {
return fmt.Errorf("decoding image %s: %v", imgName, e)
}
thumbImg := resize.Thumbnail(thumbnailSize, thumbnailSize, img, resize.Bilinear)
format := filepath.Ext(imgName)
thumbFileName := strings.Split(imgName, ".")[0] + "-thumb" + format
thumbFile, e := os.Create(thumbFileName)
if e != nil {
return fmt.Errorf("creating thumbnail file: %v", e)
}
defer func() {
if e := thumbFile.Close(); e != nil && err == nil {
err = fmt.Errorf("closing thumbnail image: %v", e)
}
}()
switch format[1:] {
case "jpg", "jpeg":
jpeg.Encode(thumbFile, thumbImg, nil)
case "png":
png.Encode(thumbFile, thumbImg)
default:
panic(fmt.Sprintf("Saving image as format %s is not implemented", format[1:]))
}
return nil
}
type byDate []*lib.RenderItem
func (h byDate) Len() int {
return len(h)
}
func (h byDate) Less(i, j int) bool {
return h[i].Date.Before(h[j].Date)
}
func (h byDate) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
func getRecentHistory() (historyJSON string, err error) {
all, e := getAllHistory()
if e != nil {
return "", fmt.Errorf("getting all history: %v", e)
}
j, e := json.Marshal(all[:int(math.Min(float64(len(all)), float64(maxHistory)))])
if e != nil {
return "", fmt.Errorf("marshal recent history to json: %v", e)
}
return string(j), nil
}
// Returns all history sorted by date with newest first
func getAllHistory() (items []*lib.RenderItem, err error) {
dir, e := os.Open("img")
defer func() {
if e := dir.Close(); e != nil && err == nil {
err = fmt.Errorf("closing img dir: %v", e)
}
}()
if e != nil {
return nil, fmt.Errorf("opening img dir: %v", e)
}
allFiles, e := dir.Readdirnames(-1)
if e != nil {
return nil, fmt.Errorf("reading dirnames: %v", e)
}
itemMap := make(map[time.Time]*lib.RenderItem)
for _, f := range allFiles {
if match := lib.RenderFileRe.FindStringSubmatch(f); match != nil {
unixTime, e := strconv.ParseInt(match[1], 10, 64)
if e != nil {
// Should be impossible since the regex ensures the match is an int
panic(fmt.Errorf("converting time of %s to int: %v", f, e))
}
date := time.Unix(0, unixTime)
if _, ok := itemMap[date]; !ok {
itemMap[date] = &lib.RenderItem{Date: date}
}
isThumb := match[2] != ""
format := match[3]
if isThumb {
itemMap[date].Thumb = "img/" + f
} else if format == "json" {
itemMap[date].Scene = "img/" + f
} else {
itemMap[date].Render = "img/" + f
}
}
}
items = make([]*lib.RenderItem, len(itemMap))
i := 0
for _, item := range itemMap {
items[i] = item
i++
}
sort.Sort(sort.Reverse(byDate(items)))
return
}