-
Notifications
You must be signed in to change notification settings - Fork 4
/
picel.go
88 lines (69 loc) · 1.81 KB
/
picel.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
/*
picel (picture element) is an image processing micro service.
https://github.com/henvic/picel
*/
package main
import (
"flag"
"fmt"
"net/http"
"os/exec"
"strings"
"time"
"github.com/henvic/picel/image"
"github.com/henvic/picel/logger"
"github.com/henvic/picel/server"
"github.com/henvic/picel/version"
)
const (
defaultAddr = ":8123"
defaultBackend = ""
)
var (
addr string
verbose bool
flagVersion bool
)
func init() {
flag.StringVar(&addr, "addr", defaultAddr, "Serving address")
flag.StringVar(&server.Backend, "backend", defaultBackend, "Image storage back-end server")
flag.DurationVar(&server.DownloadTimeout, "downloadTimeout", 5*time.Second, "Timeout for downloading an image from the origin server")
flag.BoolVar(&verbose, "verbose", false, "Pipe image processing output to stderr/stdout")
flag.BoolVar(&flagVersion, "version", false, "Print version information and quit")
}
func showVersion() {
fmt.Println("picel version", version.Version)
}
func existsDependency(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}
func checkMissingDependencies(arg ...string) {
var missing []string
left := len(arg)
for left > 0 {
if !existsDependency(arg[left-1]) {
missing = append(missing, arg[left-1])
}
left--
}
if missing != nil {
logger.Stderr.Println("Dependencies missing:", strings.Join(missing, ", "))
}
}
func main() {
flag.Parse()
image.Verbose = verbose
server.Verbose = verbose
if flagVersion {
showVersion()
return
}
checkMissingDependencies("convert", "cwebp", "gif2webp")
logger.Stdout.Println(fmt.Sprintf("picel started listening on %v", addr))
if server.Backend != "" {
logger.Stdout.Println(fmt.Sprintf("Single backend mode: %v", server.Backend))
}
http.HandleFunc("/", server.Handler)
panic(http.ListenAndServe(addr, nil))
}