-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
88 lines (71 loc) · 2.09 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
86
87
88
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gorilla/mux"
)
var templates = template.Must(template.ParseGlob("routes/*.html"))
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
router := mux.NewRouter()
// routes
router.HandleFunc("/", indexHandler).Methods("GET")
router.HandleFunc("/healthy", healthyHandler).Methods("GET")
// Serve static files from the "public" directory
router.PathPrefix("/public/").Handler(http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
router.Path("/public").Handler(http.StripPrefix("/public", http.FileServer(http.Dir("public"))))
// error handler
router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
server := &http.Server{
Addr: ":" + port,
Handler: router,
ReadHeaderTimeout: 10 * time.Second,
}
fmt.Printf("Kubernetes backend started on port %s...\n", port)
// handle SIGTERM signal for graceful shutdown
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigCh
fmt.Println("Closing on SIGTERM...")
err := server.Close()
if err != nil {
log.Fatal("Error closing server:", err)
}
fmt.Println("Server closed.")
}()
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatal("Error starting server:", err)
}
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "index", http.StatusNotFound)
}
func healthyHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "ok")
}
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "Not Found")
}
func renderTemplate(w http.ResponseWriter, tmpl string, status int) {
w.WriteHeader(status)
err := templates.ExecuteTemplate(w, tmpl+".html", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}