diff --git a/Dockerfile b/Dockerfile index b19649d..7de7a23 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,6 +3,7 @@ FROM golang:alpine AS builder WORKDIR /app COPY go.mod ./ +COPY template.html ./ COPY facts.txt ./ COPY main.go ./ diff --git a/main.go b/main.go index a32c12e..b491712 100644 --- a/main.go +++ b/main.go @@ -3,21 +3,24 @@ package main import ( "embed" "fmt" + "html/template" "math/rand" "net/http" "strings" ) -//go:embed facts.txt -var facts embed.FS +//go:embed facts.txt template.html +var content embed.FS + +type PageData struct { + Fact string +} func loadFacts() ([]string, error) { - // Read the embedded file - data, err := facts.ReadFile("facts.txt") + data, err := content.ReadFile("facts.txt") if err != nil { - return nil, err + return nil, fmt.Errorf("error reading facts file: %w", err) } - // Split the file into lines and return as a slice lines := string(data) return splitLines(lines), nil } @@ -26,67 +29,50 @@ func splitLines(data string) []string { return strings.Split(strings.TrimSpace(data), "\n") } -func randomFactHandler(facts []string) http.HandlerFunc { +func randomFactHandler(facts []string, tmpl *template.Template) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - // Get a random index - index := rand.Intn(len(facts)) + if len(facts) == 0 { + http.Error(w, "No facts available", http.StatusInternalServerError) + return + } + + index := rand.Intn(len(facts)) + fact := facts[index] - // Check if the request is from curl by looking at the User-Agent userAgent := r.Header.Get("User-Agent") isCurl := strings.HasPrefix(strings.ToLower(userAgent), "curl") if isCurl { - // For curl requests, just return the fact as plain text w.Header().Set("Content-Type", "text/plain") - fmt.Fprintln(w, facts[index]) + fmt.Fprintln(w, fact) return } - // For browser requests, return the HTML version w.Header().Set("Content-Type", "text/html") - fmt.Fprintf(w, ` - - -
- - -%s
- - - `, facts[index]) + err := tmpl.Execute(w, PageData{Fact: fact}) + if err != nil { + http.Error(w, "Error rendering template", http.StatusInternalServerError) + return + } } } func main() { - // Load facts from the embedded file facts, err := loadFacts() if err != nil { - panic(err) + panic(fmt.Sprintf("Failed to load facts: %v", err)) } - // Set up the HTTP server - http.HandleFunc("/", randomFactHandler(facts)) - fmt.Println("Server is running on http://localhost:8080") - err = http.ListenAndServe(":8080", nil) + tmpl, err := template.ParseFS(content, "template.html") if err != nil { - panic(err) + panic(fmt.Sprintf("Failed to parse template: %v", err)) + } + + http.HandleFunc("/", randomFactHandler(facts, tmpl)) + fmt.Println("Server is running on http://localhost:8080") + + if err := http.ListenAndServe(":8080", nil); err != nil { + panic(fmt.Sprintf("Server failed to start: %v", err)) } } diff --git a/template.html b/template.html new file mode 100644 index 0000000..806ece6 --- /dev/null +++ b/template.html @@ -0,0 +1,39 @@ + + + + + +{{.Fact}}
+