-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
157 lines (124 loc) · 2.77 KB
/
Copy pathmain.go
File metadata and controls
157 lines (124 loc) · 2.77 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
const usageText = `vanity - Go vanity import path server
Usage:
vanity help
vanity validate -config vanity.yaml
vanity serve -config vanity.yaml -listen 0.0.0.0:3030
Commands:
help Show this help message.
validate Validate configuration.
serve Start HTTP server.
Options:
-config PATH
Path to YAML configuration file.
-listen ADDRESS
HTTP listen address (default ":3030").
`
func main() {
os.Exit(run(os.Args[1:]))
}
func run(args []string) int {
if len(args) == 0 {
printUsage()
return 2
}
switch args[0] {
case "help", "-h", "--help":
printUsage()
return 0
case "validate":
return validate(args[1:])
case "serve":
return serve(args[1:])
default:
fmt.Fprintf(os.Stderr, "vanity: unknown command %q\n\n", args[0])
return 2
}
}
func validate(args []string) int {
fs := flag.NewFlagSet("vanity validate", flag.ContinueOnError)
fs.SetOutput(os.Stderr)
configFile := fs.String("config", "vanity.yaml", "path to YAML configuration file")
if err := fs.Parse(args); err != nil {
return 2
}
if fs.NArg() != 0 {
fmt.Fprintln(os.Stderr, "vanity validate: unexpected arguments")
return 2
}
if _, err := LoadConfig(*configFile); err != nil {
fmt.Fprintf(os.Stderr, "vanity validate: %v\n", err)
return 1
}
fmt.Printf("valid: %s\n", *configFile)
return 0
}
func serve(args []string) int {
fs := flag.NewFlagSet("vanity serve", flag.ContinueOnError)
fs.SetOutput(os.Stderr)
configFile := fs.String(
"config",
"vanity.yaml",
"path to YAML configuration file",
)
listenAddr := fs.String(
"listen",
":3030",
"HTTP listen address",
)
if err := fs.Parse(args); err != nil {
return 2
}
if fs.NArg() != 0 {
fmt.Fprintln(os.Stderr, "vanity serve: unexpected arguments")
return 2
}
config, err := LoadConfig(*configFile)
if err != nil {
fmt.Fprintf(os.Stderr, "vanity serve: %v\n", err)
return 1
}
server, err := NewServer(config)
if err != nil {
fmt.Fprintf(os.Stderr, "vanity serve: %v\n", err)
return 1
}
httpServer := &http.Server{
Addr: *listenAddr,
Handler: server,
}
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(stop)
go func() {
<-stop
ctx, cancel := context.WithTimeout(
context.Background(),
10*time.Second,
)
defer cancel()
if err := httpServer.Shutdown(ctx); err != nil {
fmt.Fprintf(os.Stderr, "vanity serve: shutdown: %v\n", err)
}
}()
fmt.Printf("listening on %s\n", *listenAddr)
if err := httpServer.ListenAndServe(); err != nil &&
err != http.ErrServerClosed {
fmt.Fprintf(os.Stderr, "vanity serve: %v\n", err)
return 1
}
return 0
}
func printUsage() {
fmt.Print(usageText)
}