-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
54 lines (50 loc) · 1.38 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
// Copyright (c) 2017 Andrea Masi. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE.txt file.
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/eraclitux/middle"
)
func main() {
homeHndlr := &homeHandler{}
homeHndlr.templateInit()
hasher, err := makeAuthorizer(os.Getenv("HTTP_USER"), os.Getenv("HTTP_PASSWORD"))
if err != nil {
log.Fatalln(err)
}
infoLogger := log.New(os.Stdout, "[INFO] ", log.Ldate|log.Ltime)
//
// Routes
//
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
http.HandleFunc("/key", middle.Auth(hasher, middle.Log(infoLogger, http.HandlerFunc(handleKey))))
http.Handle("/", middle.Log(infoLogger, middle.Auth(hasher, homeHndlr)))
addr := fmt.Sprintf("%s:%s", os.Getenv("LISTENING_ADDRESS"), os.Getenv("LISTENING_PORT"))
httpServer := &http.Server{
Addr: addr,
}
sigCh := make(chan os.Signal, 1)
shutDownCh := make(chan struct{})
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigCh
httpServer.Shutdown(context.Background())
shutDownCh <- struct{}{}
}()
err = httpServer.ListenAndServe()
if err != nil {
log.Println(err)
// In case error has not been
// caused by signals (es. address already in use)
// avoid to block on shutDownCh.
close(sigCh)
}
<-shutDownCh
}