-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
113 lines (96 loc) · 3.23 KB
/
server.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"context"
"flag"
"io"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/gorilla/mux"
)
// favicon handler
func favicon_handler(w http.ResponseWriter, r *http.Request) {
// Serve the favicon
http.ServeFile(w, r, "./assets/art/favicon.ico")
}
func health_handler(w http.ResponseWriter, r *http.Request) {
// return 200 code
w.WriteHeader(http.StatusOK)
}
// teapot handler
func teapot_handler(w http.ResponseWriter, r *http.Request) {
// return teapot state
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusTeapot)
// serve a teapot image with linkage
log.Print("Tea Served.")
io.WriteString(w, "<html><h1><a href='https://datatracker.ietf.org/doc/html/rfc2324/'>HTCPTP</h1><img src='https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftaooftea.com%2Fwp-content%2Fuploads%2F2015%2F12%2Fyixing-dark-brown-small.jpg&f=1&nofb=1' alt='Im a teapot'></a><html>")
}
func getPort() string {
port := os.Getenv("PORT")
if port == "" {
port = ":3000"
} else {
port = ":" + port
}
return port
}
// Main server program
func main() {
// initialize a time.Duration variable to hold a wait time-period
var wait time.Duration
// define a graceful termination period
flag.DurationVar(&wait, "graceful-timeout", time.Second*15, "the duration for which the server gracefully wait for existing connections to finish - e.g. 15s or 1m")
flag.Parse()
// Initialize a new router
router := mux.NewRouter()
srv := &http.Server{
// address to listen on
Addr: getPort(),
// Good practice to set timeouts to avoid Slowloris attacks.
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
// router to serve as my handler
Handler: router, // Pass our instance of gorilla/mux in.
}
// site icon server
router.HandleFunc("/favicon.ico", favicon_handler).Methods("GET")
// teapot handler
router.HandleFunc("/teapot", teapot_handler).Methods("GET")
// health handler
router.HandleFunc("/health", health_handler).Methods("GET")
// define the fileserver root dir
router.PathPrefix("/").Handler(http.StripPrefix("/",
http.FileServer(http.Dir("./Production"))))
// pass all requests to my router
http.Handle("/", router)
// print listener status
log.Print("Listening at https://org-site.up.railway.app")
// Run the server in a goroutine so that it doesn't block.
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Println(err)
}
}()
// define channel to recieve server shutdown signal
shutdown_chan := make(chan os.Signal, 1)
// I'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
signal.Notify(shutdown_chan, os.Interrupt)
// Block until I receive the signal in channel c
<-shutdown_chan
// Create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), wait)
defer cancel()
// Doesn't block if no connections, but will otherwise wait until the timeout deadline.
srv.Shutdown(ctx)
// Optionally, I could run srv.Shutdown in a goroutine and block on
// <-ctx.Done() if my application should wait for other services
// to finalize based on context cancellation.
log.Println("shutting down")
// exit the program succesfully
os.Exit(0)
}