-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
87 lines (76 loc) · 1.88 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 (
"context"
"flag"
"fmt"
bimg "gopkg.in/h2non/bimg.v1"
"log"
"os"
"os/signal"
"syscall"
)
func checkVipsVersion(majorVersion, minorVersion int) error {
minMajorVersion, minMinorVersion := 8, 9
if (majorVersion < minMajorVersion) || (majorVersion == minMajorVersion && minorVersion < minMinorVersion) {
return fmt.Errorf("Install libips=>'%d.%d'. Current version is %d.%d",
minMajorVersion, minMinorVersion, majorVersion, minorVersion)
}
return nil
}
func runServer(ctx context.Context) error {
if err := checkVipsVersion(bimg.VipsMajorVersion, bimg.VipsMinorVersion); err != nil {
return err
}
configPath := flag.String("config", "", "Path of config file in yml format")
flag.Parse()
if *configPath == "" {
return fmt.Errorf("Set config.yml path via -config flag.")
}
file, err := os.Open(*configPath)
if err != nil {
return fmt.Errorf("Error loading config: %v", err)
}
config, err := parseConfig(file)
file.Close()
if err != nil {
return err
}
if config.LogPath != "" {
logFile, err := os.OpenFile(config.LogPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
return fmt.Errorf("Could not open log file: %v", err)
}
defer logFile.Close()
log.SetOutput(logFile)
} else {
log.SetOutput(os.Stdout)
}
server := createServer(config)
done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)
defer close(done)
serverErr := make(chan error)
defer close(serverErr)
go func() {
log.Printf("Starting server on %s", config.ServerAddress)
if err := server.ListenAndServe(config.ServerAddress); err != nil {
serverErr <- err
}
}()
select {
case <-done:
return server.Shutdown()
case <-ctx.Done():
return server.Shutdown()
case err := <-serverErr:
return err
}
}
func main() {
ctx := context.Background()
err := runServer(ctx)
if err != nil {
log.Fatal(err)
ctx.Done()
}
}