updated padding
This commit is contained in:
parent
88ec940b9b
commit
bbdfb23f70
@ -3,6 +3,7 @@ FROM golang:alpine AS builder
|
||||
WORKDIR /app
|
||||
|
||||
COPY go.mod ./
|
||||
COPY template.html ./
|
||||
COPY facts.txt ./
|
||||
COPY main.go ./
|
||||
|
||||
|
78
main.go
78
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, `
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Random Fact</title>
|
||||
<style>
|
||||
body {
|
||||
background-color: #24273a;
|
||||
color: #8aadf4;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 100vh;
|
||||
margin: 0;
|
||||
font-size: 24px;
|
||||
font-weight: bold;
|
||||
font-family: Arial, sans-serif;
|
||||
err := tmpl.Execute(w, PageData{Fact: fact})
|
||||
if err != nil {
|
||||
http.Error(w, "Error rendering template", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<p>%s</p>
|
||||
</body>
|
||||
</html>
|
||||
`, facts[index])
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
|
39
template.html
Normal file
39
template.html
Normal file
@ -0,0 +1,39 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Random Fact</title>
|
||||
<style>
|
||||
body {
|
||||
background-color: #24273a;
|
||||
color: #8aadf4;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 100vh;
|
||||
margin: 0;
|
||||
font-size: 24px;
|
||||
font-weight: bold;
|
||||
font-family: Arial, sans-serif;
|
||||
}
|
||||
.container {
|
||||
width: 66.666%;
|
||||
max-width: 800px;
|
||||
padding: 2rem;
|
||||
text-align: center;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
p {
|
||||
margin: 0;
|
||||
word-wrap: break-word;
|
||||
line-height: 1.5;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<p>{{.Fact}}</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
Loading…
x
Reference in New Issue
Block a user