This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
87 lines (72 loc) · 1.65 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
package main
import (
"os"
"log"
"net/http"
"time"
"strconv"
"github.com/rs/xaccess"
"github.com/rs/xhandler"
"github.com/rs/xlog"
"github.com/rs/xmux"
"github.com/okzk/sdnotify"
)
var (
cfg config
mw middleware
uno *unoconv
)
func init() {
// read config data
cfg.initDefaultConfig()
uno = initUnoconv()
//plug the xlog handler's input to Go's default logger
log.SetFlags(0)
xlogger := xlog.New(cfg.loggerConfig)
log.SetOutput(xlogger)
//register some middleware handlers
mw.initCommonHandlers(
xlog.NewHandler(cfg.loggerConfig),
xaccess.NewHandler(),
)
sdnotify.Ready()
watchdog(uno)
}
func watchdog(uno *unoconv) {
var watchdog_usec time.Duration
if value, ok := os.LookupEnv("WATCHDOG_USEC"); ok {
if v, err := strconv.Atoi(value); err == nil {
watchdog_usec = time.Duration(v) * time.Microsecond
} else {
return
}
} else {
return
}
go func(uno *unoconv, watchdog_usec time.Duration) {
for {
time.Sleep(watchdog_usec / 2)
if checkUnoconv(uno) {
sdnotify.Watchdog()
}
}
}(uno, watchdog_usec)
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func main() {
router := xmux.New()
addr := getEnv("LISTEN_ADDR", ":3000")
certFile := getEnv("LISTEN_CERTFILE", "")
keyFile := getEnv("LISTEN_KEYFILE", "")
router.GET("/unoconv/health", xhandler.HandlerFuncC(healthHandler))
router.POST("/unoconv/:filetype", xhandler.HandlerFuncC(unoconvHandler))
if certFile != "" && keyFile != "" {
log.Fatal(http.ListenAndServeTLS(addr, certFile, keyFile, mw.Handler(router)))
}
log.Fatal(http.ListenAndServe(addr, mw.Handler(router)))
}