curl support, no html in that case

This commit is contained in:
Weetile 2024-11-24 22:16:17 +00:00
parent 621314b7bc
commit 88ec940b9b

54
main.go
View File

@ -17,7 +17,6 @@ func loadFacts() ([]string, error) {
if err != nil {
return nil, err
}
// Split the file into lines and return as a slice
lines := string(data)
return splitLines(lines), nil
@ -32,34 +31,46 @@ func randomFactHandler(facts []string) http.HandlerFunc {
// Get a random index
index := rand.Intn(len(facts))
// Write the random fact as an HTML response with CSS
// 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])
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; /* Increase text size */
font-weight: bold;
font-family: Arial, sans-serif;
}
</style>
<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>
<p>%s</p>
</body>
</html>
`, facts[index])
`, facts[index])
}
}
@ -78,3 +89,4 @@ func main() {
panic(err)
}
}