-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
156 lines (134 loc) · 4.41 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"fmt"
"io"
"log"
"math/rand"
"net"
"net/http"
"os"
"os/signal"
// "path/filepath"
"syscall"
"time"
)
const (
message = "Demo app"
logoURL = "https://raw.githubusercontent.com/kubernetes/kubernetes/master/logo/logo.png"
// Logosize = height=\"200\" width=\"200\"
// logoURL = "https://cfgmgmtcamp.eu/images/logo.png" #
// Logo size = height=\"129\" width=\"355\"
)
var (
addr = ":8080"
hostname string
env string
function string
commit string
)
var quotes = []string{
"Software is like sex: it's better when it's free. - Linus Torvalds",
"Programs must be written for people to read, and only incidentally for machines to execute. - Harold Abelson",
"Ethical software is software that respects users' freedom and community. To be ethical, software must be free as in freedom. - Richard Stallman",
"Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. - Steve Jobs",
"Good design is as little design as possible. - Dieter Rams",
"Any sufficiently advanced technology is indistinguishable from magic. - Arthur C. Clarke",
"Innovation distinguishes between a leader and a follower. - Steve Jobs",
"Without requirements or design, programming is the art of adding bugs to an empty text file. - Louis Srygley",
"To me, mathematics, computer science, and the arts are insanely related. They're all creative expressions. - Sebastian Thrun",
"This is awesome! - Michael Trip",
"Ingress testing! - Michael Trip",
"Added some more quotes for testing - Michael Trip",
}
func init() {
rand.Seed(time.Now().UnixNano())
hostname, _ = os.Hostname()
env = os.Getenv("ENV")
if env == "" {
env = "default"
}
function = os.Getenv("FUNCTION")
if function == "" {
function = "not specified"
}
// Create the static directory if it doesn't exist
if _, err := os.Stat("static"); os.IsNotExist(err) {
if err := os.Mkdir("static", 0755); err != nil {
log.Fatal(err)
}
}
}
func clientIP(r *http.Request) string {
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return ""
}
userIP := net.ParseIP(ip)
if userIP == nil {
return ""
}
return userIP.String()
}
func downloadFile(url string, filepath string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
return err
}
func deleteStaticDir() {
if _, err := os.Stat("static"); !os.IsNotExist(err) {
if err := os.RemoveAll("static"); err != nil {
log.Println(err)
}
}
}
func getVersion() string {
if commit == "" {
return "No version found"
}
return commit
}
func main() {
log.SetOutput(os.Stdout) // Set log output to stdout
// Download the Kubernetes logo
if err := downloadFile(logoURL, "static/logo.png"); err != nil {
log.Fatal(err)
}
// Handle stop signal to delete the static directory
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigCh
log.Println("Received stop signal. Deleting static directory...")
deleteStaticDir()
os.Exit(0)
}()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
quote := quotes[rand.Intn(len(quotes))]
log.Printf("%s - %s %s %s", clientIP(r), r.Method, r.URL.Path, r.Proto)
fmt.Fprintf(w, "<html><head><link rel=\"icon\" href=\"/favicon.ico\"><title>Golang Application version %s</title></head><body><img src=\"/static/logo.png\" alt=\"Kubernetes logo\" height=\"200\" width=\"200\"><h1>%s</h1><h2>Version %s</h2><p>Hostname: %s</p><p>Environment: %s</p><p>Function: %s</p><p>Client IP: %s</p><p>Headers:</p><ul>", getVersion(), message, getVersion(), hostname, env, function, clientIP(r))
for name, values := range r.Header {
for _, value := range values {
fmt.Fprintf(w, "<li>%s: %s</li>", name, value)
}
}
fmt.Fprintf(w, "</ul><p>Quote of the day:</p><blockquote>%s</blockquote></body></html>", quote)
})
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/logo.png")
})
log.Printf("Starting server on %s...\n", addr)
if err := http.ListenAndServe(addr, nil); err != nil {
log.Fatal(err)
}
}