diff --git a/main.go b/main.go index d7396d0..a32c12e 100644 --- a/main.go +++ b/main.go @@ -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, ` - - - Random Fact - + + + Random Fact + -

%s

+

%s

- `, facts[index]) + `, facts[index]) } } @@ -78,3 +89,4 @@ func main() { panic(err) } } +