-
Notifications
You must be signed in to change notification settings - Fork 41
/
main.go
85 lines (69 loc) · 1.87 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"embed"
"fmt"
"html/template"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/jritsema/gotoolbox"
"github.com/jritsema/gotoolbox/web"
)
var (
//go:embed all:templates/*
templateFS embed.FS
//go:embed css/output.css
css embed.FS
//parsed templates
html *template.Template
)
func main() {
//exit process immediately upon sigterm
handleSigTerms()
//parse templates
var err error
html, err = web.TemplateParseFSRecursive(templateFS, ".html", true, nil)
if err != nil {
panic(err)
}
//add routes
router := http.NewServeMux()
router.Handle("GET /css/output.css", http.FileServer(http.FS(css)))
//add
router.Handle("GET /company/add", web.Action(addCompany))
router.Handle("POST /company", web.Action(saveNewCompany))
router.Handle("GET /company", web.Action(cancelSaveNewCompany))
//edit
router.Handle("GET /company/edit/{id}", web.Action(editCompany))
router.Handle("PUT /company/{id}", web.Action(saveExistingCompany))
router.Handle("GET /company/{id}", web.Action(cancelSaveExistingCompany))
//delete
router.Handle("DELETE /company/{id}", web.Action(deleteCompany))
//home
router.Handle("GET /", web.Action(index))
router.Handle("GET /index.html", web.Action(index))
//logging/tracing
nextRequestID := func() string {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
logger := log.New(os.Stdout, "http: ", log.LstdFlags)
middleware := tracing(nextRequestID)(logging(logger)(router))
port := gotoolbox.GetEnvWithDefault("PORT", "8080")
logger.Println("listening on http://localhost:" + port)
if err := http.ListenAndServe(":"+port, middleware); err != nil {
logger.Println("http.ListenAndServe():", err)
os.Exit(1)
}
}
func handleSigTerms() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("received SIGTERM, exiting")
os.Exit(1)
}()
}