-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
43 lines (34 loc) · 805 Bytes
/
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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
workDir, _ := os.Getwd()
port := flag.String("port", "5000", "Port for the application")
dir := flag.String("dir", workDir, "Directory to serve")
flag.Parse()
fs := http.FileServer(http.Dir(*dir))
http.Handle("/", fs)
srvr := http.Server{
Addr: fmt.Sprintf(":%s", *port),
}
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGKILL, syscall.SIGINT, syscall.SIGQUIT)
go func() {
log.Printf("FileServer started at - http://localhost:%s\n", *port)
log.Fatal(srvr.ListenAndServe())
}()
<-stop
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
srvr.Shutdown(ctx)
log.Println("Server shutteddown gracefully")
}