-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (46 loc) · 1.16 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/roychowdhuryrohit-dev/slug/lib/config"
"github.com/roychowdhuryrohit-dev/slug/lib/http"
)
func main() {
config.SetupConfig()
portConfig, _ := config.ConfigMap.Load(config.Port)
port, _ := portConfig.(*string)
dirPathConfig, _ := config.ConfigMap.Load(config.DocumentRoot)
dirPath, _ := dirPathConfig.(*string)
timeoutEnvConfig, _ := config.ConfigMap.Load(config.Timeout)
timeout, _ := timeoutEnvConfig.(*int)
router := http.NewFileRouter()
handler, err := http.FileServer(http.Dir(*dirPath))
if err != nil {
log.Panicln(err.Error())
}
router.AddRoute("/", handler)
srv := &http.Server{
Addr: fmt.Sprintf(":%s", *port),
Router: router,
}
exit := make(chan os.Signal, 1)
signal.Notify(exit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Panicln(err.Error())
}
}()
log.Println("server started")
<-exit
log.Println("shutting down server")
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(*timeout)*time.Second)
defer func() {
cancel()
}()
srv.Shutdown(ctx)
}