-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathserver.go
More file actions
47 lines (37 loc) · 1.16 KB
/
server.go
File metadata and controls
47 lines (37 loc) · 1.16 KB
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
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
)
func apiHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, world!")
}
func setupHandlersAndMiddleware(mux *http.ServeMux, l *log.Logger) http.Handler {
mux.HandleFunc("/api", apiHandler)
return loggingMiddleware(mux, l)
}
func loggingMiddleware(h http.Handler, l *log.Logger) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
h.ServeHTTP(w, r)
l.Printf("protocol=%s path=%s method=%s duration=%f", r.Proto, r.URL.Path, r.Method, time.Now().Sub(startTime).Seconds())
})
}
func main() {
listenAddr := os.Getenv("LISTEN_ADDR")
if len(listenAddr) == 0 {
listenAddr = ":8443"
}
tlsCertFile := os.Getenv("TLS_CERT_FILE_PATH")
tlsKeyFile := os.Getenv("TLS_KEY_FILE_PATH")
if len(tlsCertFile) == 0 || len(tlsKeyFile) == 0 {
log.Fatal("TLS_CERT_FILE_PATH and TLS_KEY_FILE_PATH must both be specified")
}
mux := http.NewServeMux()
l := log.New(os.Stdout, "tls-server", log.Lshortfile|log.LstdFlags)
m := setupHandlersAndMiddleware(mux, l)
log.Fatal(http.ListenAndServeTLS(listenAddr, tlsCertFile, tlsKeyFile, m))
}