package main import ( "embed" "fmt" "math/rand" "net/http" "strings" ) //go:embed facts.txt var facts embed.FS func loadFacts() ([]string, error) { // Read the embedded file data, err := facts.ReadFile("facts.txt") if err != nil { return nil, err } // Split the file into lines and return as a slice lines := string(data) return splitLines(lines), nil } func splitLines(data string) []string { return strings.Split(strings.TrimSpace(data), "\n") } func randomFactHandler(facts []string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // Get a random index index := rand.Intn(len(facts)) // 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, ` Random Fact

%s

`, facts[index]) } } func main() { // Load facts from the embedded file facts, err := loadFacts() if err != nil { panic(err) } // Set up the HTTP server http.HandleFunc("/", randomFactHandler(facts)) fmt.Println("Server is running on http://localhost:8080") err = http.ListenAndServe(":8080", nil) if err != nil { panic(err) } }