Add localised server information to each page
All checks were successful
Build and Push container image / build-and-push-image (push) Successful in 49s
All checks were successful
Build and Push container image / build-and-push-image (push) Successful in 49s
This commit is contained in:
Binary file not shown.
35
src/main.go
35
src/main.go
@@ -6,6 +6,7 @@ import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -14,25 +15,41 @@ import (
|
||||
//go:embed templates/*.html
|
||||
var templatesFS embed.FS
|
||||
|
||||
//go:embed static/*
|
||||
var staticFiles embed.FS
|
||||
|
||||
type ErrorPageData struct {
|
||||
StatusCode int
|
||||
Title string
|
||||
Description string
|
||||
ServerName string
|
||||
ServerFlag string
|
||||
}
|
||||
|
||||
type NonErrorPageData struct {
|
||||
Hostname string
|
||||
Hostname string
|
||||
ServerName string
|
||||
ServerFlag string
|
||||
}
|
||||
|
||||
var (
|
||||
serverName string
|
||||
serverFlag string
|
||||
)
|
||||
|
||||
func main() {
|
||||
tmpl, err := template.ParseFS(templatesFS, "templates/*.html")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse templates: %v", err)
|
||||
}
|
||||
|
||||
serverName = os.Getenv("SERVER_NAME")
|
||||
if serverName == "" {
|
||||
serverName = "unknown-server"
|
||||
}
|
||||
|
||||
serverFlag = os.Getenv("SERVER_FLAG")
|
||||
if serverFlag == "" {
|
||||
serverFlag = "🇬🇧"
|
||||
}
|
||||
|
||||
http.HandleFunc("/", defaultHandler(tmpl))
|
||||
|
||||
http.HandleFunc("/error/", errorHandler(tmpl))
|
||||
@@ -48,7 +65,9 @@ func defaultHandler(tmpl *template.Template) http.HandlerFunc {
|
||||
hostname := r.Host
|
||||
|
||||
data := NonErrorPageData{
|
||||
Hostname: hostname,
|
||||
Hostname: hostname,
|
||||
ServerName: serverName,
|
||||
ServerFlag: serverFlag,
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
@@ -67,7 +86,9 @@ func maintenanceHandler(tmpl *template.Template) http.HandlerFunc {
|
||||
hostname := r.Host
|
||||
|
||||
data := NonErrorPageData{
|
||||
Hostname: hostname,
|
||||
Hostname: hostname,
|
||||
ServerName: serverName,
|
||||
ServerFlag: serverFlag,
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
@@ -117,6 +138,8 @@ func errorHandler(tmpl *template.Template) http.HandlerFunc {
|
||||
StatusCode: statusCode,
|
||||
Title: msg.Title,
|
||||
Description: msg.Description,
|
||||
ServerName: serverName,
|
||||
ServerFlag: serverFlag,
|
||||
}
|
||||
|
||||
w.WriteHeader(data.StatusCode)
|
||||
|
||||
@@ -49,11 +49,19 @@ html, body {
|
||||
|
||||
.error-message p {
|
||||
font-size: 1rem;
|
||||
margin-bottom: 1.5rem;
|
||||
color: #495057;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.server-footer {
|
||||
margin-top: 1.5rem;
|
||||
font-size: 0.8rem;
|
||||
color: #8c9ba5;
|
||||
border-top: 1px solid #eef2f5;
|
||||
padding-top: 1rem;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.btn {
|
||||
display: inline-block;
|
||||
padding: 0.6rem 1.6rem;
|
||||
@@ -83,6 +91,9 @@ html, body {
|
||||
No website exists at {{ .Hostname }}. Check the address and try again.
|
||||
</p>
|
||||
</div>
|
||||
<footer class="server-footer">
|
||||
Served by {{ .ServerFlag }} {{ .ServerName }}
|
||||
</footer>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
@@ -54,6 +54,15 @@ html, body {
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.server-footer {
|
||||
margin-top: 1.5rem;
|
||||
font-size: 0.8rem;
|
||||
color: #8c9ba5;
|
||||
border-top: 1px solid #eef2f5;
|
||||
padding-top: 1rem;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.btn {
|
||||
display: inline-block;
|
||||
padding: 0.6rem 1.6rem;
|
||||
@@ -84,7 +93,9 @@ html, body {
|
||||
</p>
|
||||
<a href="/" class="btn">Return to Homepage</a>
|
||||
</div>
|
||||
<footer class="server-footer">
|
||||
Served by {{ .ServerFlag }} {{ .ServerName }}
|
||||
</footer>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
</html>
|
||||
@@ -50,11 +50,19 @@ html, body {
|
||||
|
||||
.error-message p {
|
||||
font-size: 1rem;
|
||||
margin-bottom: 1.5rem;
|
||||
color: #495057;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.server-footer {
|
||||
margin-top: 1.5rem;
|
||||
font-size: 0.8rem;
|
||||
color: #8c9ba5;
|
||||
border-top: 1px solid #eef2f5;
|
||||
padding-top: 1rem;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.btn {
|
||||
display: inline-block;
|
||||
padding: 0.6rem 1.6rem;
|
||||
@@ -84,7 +92,9 @@ html, body {
|
||||
{{ .Hostname }} is under scheduled maintenance. Check back later.
|
||||
</p>
|
||||
</div>
|
||||
<footer class="server-footer">
|
||||
Served by {{ .ServerFlag }} {{ .ServerName }}
|
||||
</footer>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user