-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (84 loc) · 2.18 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
88
89
90
91
92
93
94
95
96
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"os/signal"
"sync"
"time"
)
const filevault = "filevault"
func main() {
ctx := context.Background()
if err := run(ctx, os.Args, os.Getenv, os.Stdin, os.Stdout, os.Stderr); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
func newFlagset(cfg *Config, getenv func(string) string) *flag.FlagSet {
defaultValue := func(key string) string {
val := getenv(key)
if val != "" {
return val
}
switch key {
case "FILEVAULT_HOST":
return cfg.Host
case "FILEVAULT_PORT":
return cfg.Port
case "FILEVAULT_DIR":
return cfg.Dir
default:
return ""
}
}
fs := flag.NewFlagSet(filevault, flag.ContinueOnError)
fs.StringVar(&cfg.Dir, "dir", defaultValue("FILEVAULT_DIR"), "directory which will be used for storing the files")
fs.StringVar(&cfg.Host, "host", defaultValue("FILEVAULT_HOST"), "host addr on which the http server will run")
fs.StringVar(&cfg.Port, "port", defaultValue("FILEVAULT_PORT"), "port on which the http server will listen")
return fs
}
func run(
ctx context.Context,
args []string,
getenv func(string) string,
stdin io.Reader,
stdout, stderr io.Writer,
) error {
cfg := NewConfig()
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
defer cancel()
fs := newFlagset(&cfg, getenv)
fs.SetOutput(stdout)
fs.Parse(args[1:])
svc := NewFilevaultService(cfg)
httpServer := &http.Server{
Addr: net.JoinHostPort(cfg.Host, cfg.Port),
Handler: NewServer(cfg, svc),
}
go func() {
log.Printf("listening on %s\n", httpServer.Addr)
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Fprintf(stderr, "error listening and serving: %s\n", err)
}
}()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
<-ctx.Done()
shutdownCtx := context.Background()
shutdownCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
if err := httpServer.Shutdown(shutdownCtx); err != nil {
fmt.Fprintf(os.Stderr, "error shutting down http server: %s\n", err)
}
}()
wg.Wait()
return nil
}