-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (58 loc) · 1.2 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
package main
import (
"context"
_ "embed"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
)
var (
//go:embed VERSION
appVersion string
buildTime string
buildCommit string
)
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("[%s]: %s\n", r.Method, r.URL)
next.ServeHTTP(w, r)
})
}
func main() {
fmt.Printf("go-srv %s (%s %s)\n", appVersion, buildCommit, buildTime)
port := 6969
flag.IntVar(&port, "port", 6969, "")
quiet := false
flag.BoolVar(&quiet, "quiet", false, "")
flag.Parse()
host := fmt.Sprintf(":%d", port)
handler := http.FileServer(http.Dir("."))
if !quiet {
log.Printf("running on: %s\n", host)
handler = loggingMiddleware(handler)
}
srv := http.Server{
Addr: host,
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: handler,
}
go func() {
if err := srv.ListenAndServe(); err != nil {
if !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}
}()
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
<-ch
srv.Shutdown(context.Background())
os.Exit(0)
}