summaryrefslogtreecommitdiff
path: root/main.go
blob: cacd01c8d54c8cc27a2b08b6af0185006f5d06a8 (plain)
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
package main

import (
	"fmt"
	"log"
	"os"
	"os/signal"
	"syscall"
	"net/http"
	"time"

	"github.com/gorilla/mux"
)

const (
	Addr = "127.0.0.1:8000"
	ReadTimeout = 15 * time.Second
	WriteTimeout = 15 * time.Second
)

func main() {
	run()
}

func run() {
	log.Println("finserv finance server was started.")
	defer log.Println("finserv finance server was stopped.")

	r := mux.NewRouter()

	r.HandleFunc("/", hello)

	srv := &http.Server{
		Handler:      r,
		Addr:         Addr,
		WriteTimeout: WriteTimeout,
		ReadTimeout:  ReadTimeout,
	}

	go srv.ListenAndServe()

	await(syscall.SIGTERM, syscall.SIGINT)
}

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "Hello world!")
}

func await(signals ...syscall.Signal) {
	listener := make(chan os.Signal, 1)
	for _, s := range signals {
		signal.Notify(listener, s)
	}
	sig := <-listener
	log.Printf("Received OS signal '%v'\n", sig)
}