forked from jheidel/timelapse-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (83 loc) · 2.48 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
package main
import (
"context"
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"timelapse-queue/engine"
"timelapse-queue/filebrowse"
"timelapse-queue/util"
assetfs "github.com/elazarl/go-bindata-assetfs"
log "github.com/sirupsen/logrus"
)
var (
port = flag.Int("port", 8080, "Port to host web frontend.")
root = flag.String("root", "/home/jeff", "Filesystem root.")
// Timestamp that can be set with ldflags for versioning.
// Expected to be empty, or unix seconds.
BuildTimestamp string
)
func main() {
flag.Parse()
// Configure logging.
customFormatter := new(log.TextFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
customFormatter.FullTimestamp = true
log.SetFormatter(customFormatter)
ffmpegp, err := util.LocateFFmpeg()
if err != nil {
log.Errorf("Unable to locate ffmpeg binary: %v", err)
fmt.Println("Either ensure the ffmpeg binary is in $PATH,")
fmt.Println("or set the FFMPEG environment variable.")
os.Exit(1)
return
} else {
log.Infof("Located ffmpeg binary, %v", ffmpegp)
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
fb := filebrowse.NewFileBrowser(*root)
ih := &filebrowse.ImageHost{fb}
lh := &filebrowse.LogHost{fb}
jq := engine.NewJobQueue()
go jq.Loop(context.Background())
eng := &engine.TestServer{
Browser: fb,
Queue: jq,
}
go func() {
log.Infof("Hosting web frontend on port %d", *port)
http.Handle("/filebrowser", fb)
http.HandleFunc("/timelapse", fb.ServeTimelapse)
http.Handle("/image", ih)
http.Handle("/log", lh)
http.Handle("/convert", eng)
http.Handle("/queue", jq)
http.HandleFunc("/queue-cancel", jq.ServeCancel)
http.HandleFunc("/queue-remove", jq.ServeRemove)
http.HandleFunc("/profiles", engine.ServeProfiles)
http.Handle("/",
http.FileServer(
&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "web/build/default"}))
http.HandleFunc("/build", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
ts, err := strconv.Atoi(BuildTimestamp)
if err != nil {
log.Fatalf("build timestamp %v not an integer", BuildTimestamp)
}
t := time.Unix(int64(ts), 0)
fmt.Fprintf(w, "%s", t.Format("Jan 2, 2006 3:04 PM"))
})
err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
log.Infof("HTTP server exited with status %v", err)
os.Exit(1)
}()
sig := <-sigs
log.Warningf("Caught signal %v", sig)
}