/
imgscale.go
55 lines (50 loc) · 1.22 KB
/
imgscale.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
package main
import (
"flag"
"fmt"
"github.com/vanng822/gopid"
"github.com/vanng822/imgscale/imgscale"
"log"
"net/http"
"os"
"os/signal"
"syscall"
)
func main() {
var (
configPath string
host string
port int
pidFile string
force bool
)
flag.StringVar(&host, "h", "127.0.0.1", "Host to listen on")
flag.IntVar(&port, "p", 8080, "Port number to listen on")
flag.StringVar(&configPath, "c", "./config/formats.json", "Path to configurations")
flag.StringVar(&pidFile, "pid", "imgscale.pid", "Pid file")
flag.BoolVar(&force, "f", false, "Force and remove pid file")
flag.Parse()
if pidFile != "" {
gopid.CheckPid(pidFile, force)
gopid.CreatePid(pidFile)
defer gopid.CleanPid(pidFile)
}
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Kill, os.Interrupt, syscall.SIGTERM, syscall.SIGUSR2)
handler := imgscale.Configure(configPath)
defer handler.Cleanup()
http.Handle("/", handler)
log.Printf("listening to address %s:%d", host, port)
go http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), nil)
for {
sig := <-sigc
switch sig {
case syscall.SIGUSR2:
log.Println("Reloading config")
handler.Reload()
default:
log.Printf("Got signal: %s", sig)
return
}
}
}