-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
127 lines (109 loc) · 3.71 KB
/
main.go
File metadata and controls
127 lines (109 loc) · 3.71 KB
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
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"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
var (
port = flag.Int("port", 8080, "Port to host web frontend (http).")
portSSL = flag.Int("port_ssl", 8443, "Port to host web frontend (https). Requires cert files set in env.")
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 maxAgeHandler(seconds int, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Cache-Control", fmt.Sprintf("max-age=%d, public, must-revalidate, proxy-revalidate", seconds))
h.ServeHTTP(w, r)
})
}
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)
filebrowse.WatchMountHealthy(*root)
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() {
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("/metrics", promhttp.Handler())
http.Handle("/",
maxAgeHandler(600,
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"))
})
var err error
if cert, key := os.Getenv("SSL_CERT"), os.Getenv("SSL_KEY"); cert != "" && key != "" {
go func() {
// Redirect HTTP traffic to HTTPS endpoint
log.Infof("Hosting https redirect on port %d", *port)
err := http.ListenAndServe(fmt.Sprintf(":%d", *port), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://"+r.Host+r.RequestURI, http.StatusMovedPermanently)
}))
log.Infof("HTTP redirect server exited with status %v", err)
}()
log.Infof("Hosting web frontend on port %d", *portSSL)
err = http.ListenAndServeTLS(fmt.Sprintf(":%d", *portSSL), cert, key, nil)
} else {
// Fallback to serving on HTTP
log.Infof("Hosting web frontend on port %d", *port)
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)
}