embed templates in binary
All checks were successful
Build docker container / Build image (push) Successful in 26s

This commit is contained in:
Mans Ziesel 2024-06-03 13:56:16 +02:00
parent c96249c0b5
commit 7c3c156139

View File

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"embed"
"encoding/json" "encoding/json"
"fmt" "fmt"
"html/template" "html/template"
@ -27,6 +28,9 @@ type contextKey string
const netAddrKey = contextKey("netAddr") const netAddrKey = contextKey("netAddr")
//go:embed templates/*
var templatesFS embed.FS
func RealIpMiddleware(next http.Handler) http.Handler { func RealIpMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
netAddr := extractNetAddress(r) netAddr := extractNetAddress(r)
@ -85,7 +89,7 @@ func mainHandler(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path { switch r.URL.Path {
case "/": case "/":
fp := path.Join("templates", "index.html") fp := path.Join("templates", "index.html")
tmpl, err := template.ParseFiles(fp) tmpl, err := template.ParseFS(templatesFS, fp)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return