mirror of
https://gitlab.durp.info/durfy/apps/durpweb.git
synced 2026-05-07 07:50:31 -05:00
43 lines
765 B
Go
43 lines
765 B
Go
|
|
package catfact
|
||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"html/template"
|
||
|
|
"io"
|
||
|
|
"net/http"
|
||
|
|
)
|
||
|
|
|
||
|
|
type CatFact struct {
|
||
|
|
Fact string `json:"fact"`
|
||
|
|
Length int `json:"length"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetCatFact(w http.ResponseWriter, r *http.Request) {
|
||
|
|
tmpl, err := template.ParseGlob("view/*.html")
|
||
|
|
// Send a POST request to the specified URL with the request body
|
||
|
|
response, err := http.Get(
|
||
|
|
"http://catfact.ninja/fact",
|
||
|
|
)
|
||
|
|
if err != nil {
|
||
|
|
}
|
||
|
|
defer response.Body.Close()
|
||
|
|
|
||
|
|
// Read the response body
|
||
|
|
responseBody, err := io.ReadAll(response.Body)
|
||
|
|
if err != nil {
|
||
|
|
}
|
||
|
|
|
||
|
|
// Unmarshal the JSON response
|
||
|
|
var resp CatFact
|
||
|
|
if err := json.Unmarshal(responseBody, &resp); err != nil {
|
||
|
|
}
|
||
|
|
|
||
|
|
data := struct {
|
||
|
|
Fact string
|
||
|
|
}{
|
||
|
|
Fact: resp.Fact,
|
||
|
|
}
|
||
|
|
|
||
|
|
tmpl.ExecuteTemplate(w, "CatFact", data)
|
||
|
|
}
|