-
Notifications
You must be signed in to change notification settings - Fork 110
/
simple-web-server.go
96 lines (75 loc) · 2.95 KB
/
simple-web-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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"gopkg.in/ini.v1"
)
type configurationListHandler struct {
appMode string
privateKeyPath string
publicKeyPath string
paypalURL string
paypalCertPath string
dbCon string
dbUser string
dbPassword string
}
func (h *configurationListHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "<h1>I am a GO application running inside Kubernetes.</h1> <h2>My properties are:</h2><ul>")
fmt.Fprintf(w, "<li>app_mode: "+h.appMode+"</li>")
fmt.Fprintf(w, "<li>private_key: "+h.privateKeyPath+"</li>")
fmt.Fprintf(w, "<li>public_key: "+h.publicKeyPath+"</li>")
fmt.Fprintf(w, "<li>paypal_url: "+h.paypalURL+"</li>")
fmt.Fprintf(w, "<li>paypal_cert: "+h.paypalCertPath+"</li>")
fmt.Fprintf(w, "<li>db_con: "+h.dbCon+"</li>")
fmt.Fprintf(w, "<li>db_user: "+h.dbUser+"</li>")
fmt.Fprintf(w, "<li>db_password: "+h.dbPassword+"</li>")
fmt.Fprintf(w, "</ul>")
fmt.Fprintf(w, "<h2> Private Signing key </h2>")
fmt.Fprintf(w, "<pre> %s</pre>", readFileToString(h.privateKeyPath))
fmt.Fprintf(w, "<h2> Public Signing key </h2>")
fmt.Fprintf(w, "<pre> %s</pre>", readFileToString(h.publicKeyPath))
fmt.Fprintf(w, "<h2> Paypal cert </h2>")
fmt.Fprintf(w, "<pre> %s</pre>", readFileToString(h.paypalCertPath))
fmt.Fprintf(w, "<h2> Mysql URL </h2>")
fmt.Fprintf(w, "<pre> %s</pre>", readFileToString(h.dbCon))
fmt.Fprintf(w, "<h2> Mysql username </h2>")
fmt.Fprintf(w, "<pre> %s</pre>", readFileToString(h.dbUser))
fmt.Fprintf(w, "<h2> Mysql password </h2>")
fmt.Fprintf(w, "<pre> %s</pre>", readFileToString(h.dbPassword))
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "OK\n")
}
func main() {
cfg, err := ini.LooseLoad("/config/settings.ini", "settings.ini")
if err != nil {
fmt.Printf("Failed to read configuration file: %v", err)
}
fmt.Println("App Mode:", cfg.Section("").Key("app_mode").String())
clh := configurationListHandler{}
clh.appMode = cfg.Section("").Key("app_mode").String()
clh.privateKeyPath = cfg.Section("security").Key("private_key").String()
clh.publicKeyPath = cfg.Section("security").Key("public_key").String()
clh.paypalURL = cfg.Section("paypal").Key("paypal_url").String()
clh.paypalCertPath = cfg.Section("paypal").Key("paypal_cert").String()
clh.dbCon = cfg.Section("mysql").Key("db_con").String()
clh.dbUser = cfg.Section("mysql").Key("db_user").String()
clh.dbPassword = cfg.Section("mysql").Key("db_password").String()
fmt.Println("Simple web server is starting now on port 8080...")
http.Handle("/", &clh)
http.HandleFunc("/health", healthHandler)
err = http.ListenAndServe(":8080", nil)
if err != nil {
fmt.Printf("Failed to start server at port 8080: %v", err)
}
}
func readFileToString(filename string) string {
data, err := ioutil.ReadFile(filename)
if err != nil {
return "Could not read " + filename
}
return string(data)
}