This commit is contained in:
DeveloperDurp 2024-07-28 09:40:32 -05:00
parent 035a813154
commit 67b4818274
10 changed files with 181 additions and 33 deletions

42
pkg/catfact/handler.go Normal file
View file

@ -0,0 +1,42 @@
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)
}

25
pkg/index/handler.go Normal file
View file

@ -0,0 +1,25 @@
package index
import (
"html/template"
"net/http"
)
func GetIndex(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseGlob("view/*.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
data := struct {
Message string
MainTitle string
}{
Message: "Hello World!!!",
MainTitle: "SimpleWebsite!",
}
tmpl.ExecuteTemplate(w, "index.html", data)
}