updated padding
This commit is contained in:
parent
88ec940b9b
commit
bbdfb23f70
@ -3,6 +3,7 @@ FROM golang:alpine AS builder
|
|||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY go.mod ./
|
COPY go.mod ./
|
||||||
|
COPY template.html ./
|
||||||
COPY facts.txt ./
|
COPY facts.txt ./
|
||||||
COPY main.go ./
|
COPY main.go ./
|
||||||
|
|
||||||
|
80
main.go
80
main.go
@ -3,21 +3,24 @@ package main
|
|||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed facts.txt
|
//go:embed facts.txt template.html
|
||||||
var facts embed.FS
|
var content embed.FS
|
||||||
|
|
||||||
|
type PageData struct {
|
||||||
|
Fact string
|
||||||
|
}
|
||||||
|
|
||||||
func loadFacts() ([]string, error) {
|
func loadFacts() ([]string, error) {
|
||||||
// Read the embedded file
|
data, err := content.ReadFile("facts.txt")
|
||||||
data, err := facts.ReadFile("facts.txt")
|
|
||||||
if err != nil {
|
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)
|
lines := string(data)
|
||||||
return splitLines(lines), nil
|
return splitLines(lines), nil
|
||||||
}
|
}
|
||||||
@ -26,67 +29,50 @@ func splitLines(data string) []string {
|
|||||||
return strings.Split(strings.TrimSpace(data), "\n")
|
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) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
// Get a random index
|
if len(facts) == 0 {
|
||||||
index := rand.Intn(len(facts))
|
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")
|
userAgent := r.Header.Get("User-Agent")
|
||||||
isCurl := strings.HasPrefix(strings.ToLower(userAgent), "curl")
|
isCurl := strings.HasPrefix(strings.ToLower(userAgent), "curl")
|
||||||
|
|
||||||
if isCurl {
|
if isCurl {
|
||||||
// For curl requests, just return the fact as plain text
|
|
||||||
w.Header().Set("Content-Type", "text/plain")
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
fmt.Fprintln(w, facts[index])
|
fmt.Fprintln(w, fact)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// For browser requests, return the HTML version
|
|
||||||
w.Header().Set("Content-Type", "text/html")
|
w.Header().Set("Content-Type", "text/html")
|
||||||
fmt.Fprintf(w, `
|
err := tmpl.Execute(w, PageData{Fact: fact})
|
||||||
<!DOCTYPE html>
|
if err != nil {
|
||||||
<html lang="en">
|
http.Error(w, "Error rendering template", http.StatusInternalServerError)
|
||||||
<head>
|
return
|
||||||
<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;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<p>%s</p>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
`, facts[index])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Load facts from the embedded file
|
|
||||||
facts, err := loadFacts()
|
facts, err := loadFacts()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(fmt.Sprintf("Failed to load facts: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up the HTTP server
|
tmpl, err := template.ParseFS(content, "template.html")
|
||||||
http.HandleFunc("/", randomFactHandler(facts))
|
|
||||||
fmt.Println("Server is running on http://localhost:8080")
|
|
||||||
err = http.ListenAndServe(":8080", nil)
|
|
||||||
if err != nil {
|
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