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"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -14,25 +15,41 @@ import (
|
|||||||
//go:embed templates/*.html
|
//go:embed templates/*.html
|
||||||
var templatesFS embed.FS
|
var templatesFS embed.FS
|
||||||
|
|
||||||
//go:embed static/*
|
|
||||||
var staticFiles embed.FS
|
|
||||||
|
|
||||||
type ErrorPageData struct {
|
type ErrorPageData struct {
|
||||||
StatusCode int
|
StatusCode int
|
||||||
Title string
|
Title string
|
||||||
Description string
|
Description string
|
||||||
|
ServerName string
|
||||||
|
ServerFlag string
|
||||||
}
|
}
|
||||||
|
|
||||||
type NonErrorPageData struct {
|
type NonErrorPageData struct {
|
||||||
Hostname string
|
Hostname string
|
||||||
|
ServerName string
|
||||||
|
ServerFlag string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
serverName string
|
||||||
|
serverFlag string
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
tmpl, err := template.ParseFS(templatesFS, "templates/*.html")
|
tmpl, err := template.ParseFS(templatesFS, "templates/*.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to parse templates: %v", err)
|
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("/", defaultHandler(tmpl))
|
||||||
|
|
||||||
http.HandleFunc("/error/", errorHandler(tmpl))
|
http.HandleFunc("/error/", errorHandler(tmpl))
|
||||||
@@ -48,7 +65,9 @@ func defaultHandler(tmpl *template.Template) http.HandlerFunc {
|
|||||||
hostname := r.Host
|
hostname := r.Host
|
||||||
|
|
||||||
data := NonErrorPageData{
|
data := NonErrorPageData{
|
||||||
Hostname: hostname,
|
Hostname: hostname,
|
||||||
|
ServerName: serverName,
|
||||||
|
ServerFlag: serverFlag,
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
@@ -67,7 +86,9 @@ func maintenanceHandler(tmpl *template.Template) http.HandlerFunc {
|
|||||||
hostname := r.Host
|
hostname := r.Host
|
||||||
|
|
||||||
data := NonErrorPageData{
|
data := NonErrorPageData{
|
||||||
Hostname: hostname,
|
Hostname: hostname,
|
||||||
|
ServerName: serverName,
|
||||||
|
ServerFlag: serverFlag,
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusServiceUnavailable)
|
w.WriteHeader(http.StatusServiceUnavailable)
|
||||||
@@ -117,6 +138,8 @@ func errorHandler(tmpl *template.Template) http.HandlerFunc {
|
|||||||
StatusCode: statusCode,
|
StatusCode: statusCode,
|
||||||
Title: msg.Title,
|
Title: msg.Title,
|
||||||
Description: msg.Description,
|
Description: msg.Description,
|
||||||
|
ServerName: serverName,
|
||||||
|
ServerFlag: serverFlag,
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(data.StatusCode)
|
w.WriteHeader(data.StatusCode)
|
||||||
|
|||||||
@@ -49,11 +49,19 @@ html, body {
|
|||||||
|
|
||||||
.error-message p {
|
.error-message p {
|
||||||
font-size: 1rem;
|
font-size: 1rem;
|
||||||
margin-bottom: 1.5rem;
|
|
||||||
color: #495057;
|
color: #495057;
|
||||||
line-height: 1.4;
|
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 {
|
.btn {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
padding: 0.6rem 1.6rem;
|
padding: 0.6rem 1.6rem;
|
||||||
@@ -83,6 +91,9 @@ html, body {
|
|||||||
No website exists at {{ .Hostname }}. Check the address and try again.
|
No website exists at {{ .Hostname }}. Check the address and try again.
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
<footer class="server-footer">
|
||||||
|
Served by {{ .ServerFlag }} {{ .ServerName }}
|
||||||
|
</footer>
|
||||||
</main>
|
</main>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -54,6 +54,15 @@ html, body {
|
|||||||
line-height: 1.4;
|
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 {
|
.btn {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
padding: 0.6rem 1.6rem;
|
padding: 0.6rem 1.6rem;
|
||||||
@@ -84,7 +93,9 @@ html, body {
|
|||||||
</p>
|
</p>
|
||||||
<a href="/" class="btn">Return to Homepage</a>
|
<a href="/" class="btn">Return to Homepage</a>
|
||||||
</div>
|
</div>
|
||||||
|
<footer class="server-footer">
|
||||||
|
Served by {{ .ServerFlag }} {{ .ServerName }}
|
||||||
|
</footer>
|
||||||
</main>
|
</main>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
@@ -50,11 +50,19 @@ html, body {
|
|||||||
|
|
||||||
.error-message p {
|
.error-message p {
|
||||||
font-size: 1rem;
|
font-size: 1rem;
|
||||||
margin-bottom: 1.5rem;
|
|
||||||
color: #495057;
|
color: #495057;
|
||||||
line-height: 1.4;
|
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 {
|
.btn {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
padding: 0.6rem 1.6rem;
|
padding: 0.6rem 1.6rem;
|
||||||
@@ -84,7 +92,9 @@ html, body {
|
|||||||
{{ .Hostname }} is under scheduled maintenance. Check back later.
|
{{ .Hostname }} is under scheduled maintenance. Check back later.
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
<footer class="server-footer">
|
||||||
|
Served by {{ .ServerFlag }} {{ .ServerName }}
|
||||||
|
</footer>
|
||||||
</main>
|
</main>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
Reference in New Issue
Block a user