-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
111 lines (90 loc) · 2.97 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"github.com/supporttools/hello-world/pkg/config"
"github.com/supporttools/hello-world/pkg/logging"
"github.com/supporttools/hello-world/pkg/metrics"
"github.com/supporttools/hello-world/pkg/templates"
"github.com/supporttools/hello-world/pkg/version"
)
var (
// Global logger variable
logger *logrus.Logger
)
func main() {
// Load configuration from environment variables
config.LoadConfiguration()
// Set up logging based on the configuration
logger = logging.SetupLogging(&config.CFG)
// Log the current configuration if debug mode is enabled
if config.CFG.Debug {
logger.Infoln("Debug mode enabled")
logger.Infof("Port: %d", config.CFG.Port)
logger.Infof("Metrics Port: %d", config.CFG.MetricsPort)
}
// Start the metrics server in a separate goroutine
go metrics.StartMetricsServer(config.CFG.MetricsPort)
// Start the web server
webserver(config.CFG.Port)
}
func webserver(port int) {
logger.Println("Starting web server...")
// Set up HTTP handlers
http.HandleFunc("/", helloWorldHandler)
// Serve static files from the /img directory
http.Handle("/img/", http.StripPrefix("/img/", http.FileServer(http.Dir("./img"))))
// Serve Prometheus metrics
http.Handle("/metrics", promhttp.Handler())
// Build the server address
serverAddress := fmt.Sprintf(":%d", port)
logger.Printf("Serving HelloWorld on HTTP port: %s\n", serverAddress)
// Define server with timeouts
server := &http.Server{
Addr: serverAddress,
ReadTimeout: 10 * 60 * 60, // Max time to read request body
WriteTimeout: 10 * 60 * 60, // Max time to write response
IdleTimeout: 60 * 60 * 60, // Max time to keep idle connections open
}
// Start the HTTP server with timeout settings
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("HTTP server failed to start: %v", err)
}
}
// helloWorldHandler handles requests to the root path and serves the HTML template
func helloWorldHandler(w http.ResponseWriter, r *http.Request) {
logger.WithFields(logrus.Fields{
"method": r.Method,
"url": r.URL.String(),
}).Info("Received request")
// Prepare template data
data := map[string]interface{}{
"Hostname": getHostname(),
"GitCommit": version.GitCommit,
"Host": r.Host,
"Headers": r.Header,
}
// Render the HTML template
output, err := templates.CompileTemplateFromMap(templates.HelloWorldTemplate, data)
if err != nil {
http.Error(w, "Error rendering template", http.StatusInternalServerError)
logger.Error("Error rendering template: ", err)
return
}
// Write the rendered template to the response
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, output)
}
// getHostname gets the hostname of the server
func getHostname() string {
hostname, err := os.Hostname()
if err != nil {
logger.Error("Error getting hostname: ", err)
return "unknown"
}
return hostname
}