|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "html/template" |
| 5 | + "log" |
| 6 | + "net" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +type PageData struct { |
| 13 | + EnvVars map[string]string |
| 14 | + ConfigVars map[string]string |
| 15 | + Hostname string |
| 16 | + IPAddress string |
| 17 | +} |
| 18 | + |
| 19 | +func main() { |
| 20 | + log.Println("Starting the server on port 8080") |
| 21 | + http.HandleFunc("/", handler) |
| 22 | + log.Fatal(http.ListenAndServe(":8080", nil)) // Log fatal errors if server fails |
| 23 | +} |
| 24 | + |
| 25 | +func handler(w http.ResponseWriter, r *http.Request) { |
| 26 | + log.Printf("Received request from %s %s\n", r.RemoteAddr, r.Method) |
| 27 | + |
| 28 | + envVars := os.Environ() |
| 29 | + envMap := make(map[string]string) |
| 30 | + for _, env := range envVars { |
| 31 | + parts := strings.SplitN(env, "=", 2) |
| 32 | + if len(parts) == 2 { |
| 33 | + envMap[parts[0]] = parts[1] |
| 34 | + } |
| 35 | + } |
| 36 | + log.Println("Fetched environment variables") |
| 37 | + |
| 38 | + configVars := readConfigFiles("/etc/config") |
| 39 | + log.Printf("Fetched %d config variables\n", len(configVars)) |
| 40 | + |
| 41 | + host, ip := getHostIP() |
| 42 | + log.Printf("Fetched hostname: %s, IP address: %s\n", host, ip) |
| 43 | + |
| 44 | + data := PageData{ |
| 45 | + EnvVars: envMap, |
| 46 | + ConfigVars: configVars, |
| 47 | + Hostname: host, |
| 48 | + IPAddress: ip, |
| 49 | + } |
| 50 | + |
| 51 | + renderTemplate(w, data) |
| 52 | +} |
| 53 | + |
| 54 | +func renderTemplate(w http.ResponseWriter, data PageData) { |
| 55 | + tmpl := ` |
| 56 | + <!DOCTYPE html> |
| 57 | + <html lang="en"> |
| 58 | + <head> |
| 59 | + <meta charset="UTF-8"> |
| 60 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 61 | + <title>Environment and Config Variables</title> |
| 62 | + <style> |
| 63 | + body { |
| 64 | + font-family: Arial, sans-serif; |
| 65 | + margin: 2em; |
| 66 | + background-color: #f4f4f9; |
| 67 | + } |
| 68 | + h1 { |
| 69 | + color: #333; |
| 70 | + } |
| 71 | + .container { |
| 72 | + background: #fff; |
| 73 | + padding: 20px; |
| 74 | + border-radius: 8px; |
| 75 | + box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); |
| 76 | + } |
| 77 | + .value { |
| 78 | + color: #007BFF; |
| 79 | + font-weight: bold; |
| 80 | + } |
| 81 | + .env-table { |
| 82 | + width: 100%; |
| 83 | + border-collapse: collapse; |
| 84 | + margin-top: 1em; |
| 85 | + } |
| 86 | + .env-table th, .env-table td { |
| 87 | + border: 1px solid #ddd; |
| 88 | + padding: 8px; |
| 89 | + } |
| 90 | + .env-table th { |
| 91 | + background-color: #f4f4f9; |
| 92 | + text-align: left; |
| 93 | + } |
| 94 | + </style> |
| 95 | + </head> |
| 96 | + <body> |
| 97 | + <div class="container"> |
| 98 | + <h1>Environment and Config Variable Viewer</h1> |
| 99 | + <p><strong>Hostname:</strong> <span class="value">{{.Hostname}}</span></p> |
| 100 | + <p><strong>IP Address:</strong> <span class="value">{{.IPAddress}}</span></p> |
| 101 | +
|
| 102 | + <h2>Environment Variables</h2> |
| 103 | + <table class="env-table"> |
| 104 | + <tr> |
| 105 | + <th>Key</th> |
| 106 | + <th>Value</th> |
| 107 | + </tr> |
| 108 | + {{range $key, $value := .EnvVars}} |
| 109 | + <tr> |
| 110 | + <td>{{$key}}</td> |
| 111 | + <td>{{$value}}</td> |
| 112 | + </tr> |
| 113 | + {{end}} |
| 114 | + </table> |
| 115 | +
|
| 116 | + <h2>Config Variables (from ConfigMap Volume)</h2> |
| 117 | + <table class="env-table"> |
| 118 | + <tr> |
| 119 | + <th>Key</th> |
| 120 | + <th>Value</th> |
| 121 | + </tr> |
| 122 | + {{range $key, $value := .ConfigVars}} |
| 123 | + <tr> |
| 124 | + <td>{{$key}}</td> |
| 125 | + <td>{{$value}}</td> |
| 126 | + </tr> |
| 127 | + {{end}} |
| 128 | + </table> |
| 129 | + </div> |
| 130 | + </body> |
| 131 | + </html> |
| 132 | + ` |
| 133 | + t := template.Must(template.New("webpage").Parse(tmpl)) |
| 134 | + err := t.Execute(w, data) |
| 135 | + if err != nil { |
| 136 | + log.Printf("Error rendering template: %v\n", err) |
| 137 | + http.Error(w, "Failed to render template", http.StatusInternalServerError) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func readConfigFiles(configDir string) map[string]string { |
| 142 | + configVars := make(map[string]string) |
| 143 | + |
| 144 | + log.Printf("Reading config files from directory: %s\n", configDir) |
| 145 | + |
| 146 | + files, err := os.ReadDir(configDir) |
| 147 | + if err != nil { |
| 148 | + log.Printf("Error reading config directory: %v\n", err) |
| 149 | + return configVars // Return empty map if error occurs |
| 150 | + } |
| 151 | + |
| 152 | + for _, file := range files { |
| 153 | + if !file.IsDir() { |
| 154 | + content, err := os.ReadFile(configDir + "/" + file.Name()) |
| 155 | + if err == nil { |
| 156 | + configVars[file.Name()] = string(content) |
| 157 | + log.Printf("Loaded config file: %s\n", file.Name()) |
| 158 | + } else { |
| 159 | + log.Printf("Error reading config file %s: %v\n", file.Name(), err) |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + return configVars |
| 164 | +} |
| 165 | + |
| 166 | +func getHostIP() (string, string) { |
| 167 | + host, err := os.Hostname() |
| 168 | + if err != nil { |
| 169 | + log.Printf("Error getting hostname: %v\n", err) |
| 170 | + host = "Unknown" |
| 171 | + } |
| 172 | + |
| 173 | + addrs, err := net.InterfaceAddrs() |
| 174 | + if err != nil { |
| 175 | + log.Printf("Error getting network interface addresses: %v\n", err) |
| 176 | + return host, "Unknown" |
| 177 | + } |
| 178 | + |
| 179 | + for _, addr := range addrs { |
| 180 | + if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() { |
| 181 | + if ipNet.IP.To4() != nil { |
| 182 | + log.Printf("Found IP address: %s\n", ipNet.IP.String()) |
| 183 | + return host, ipNet.IP.String() |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + return host, "Unknown" |
| 189 | +} |
0 commit comments