I am new in Go language so I have a question. I have API from where I need to parse information to cards. I have a script which GET all information from API and place it into fields in card. I understand how to take one part of information. But I need to create many cards with different information from API and place them on HTML page. How I can do this?
server.go file for HTML hosting.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"text/template"
)
type Response struct {
Artists string `json:"artists"`
Locations string `json:"locations"`
Dates string `json:"dates"`
Relation string `json:"relation"`
}
type Artist struct {
ID int `json:"id"`
Image string `json:"image"`
Name string `json:"name"`
Members []string `json:"members"`
CreationDate string `json:"creationDate"`
FirstAlbum string `json:"firstAlbum"`
}
func main() {
http.HandleFunc("/", formHandler) // to define the path/page, where we are going to use the mentioned function
fmt.Printf("Starting server at port 8080\n")
if err := http.ListenAndServe(":8080", nil); err != nil { //to initialise our server on :8080
log.Fatal("HTTP status 500 - Internal server error: %s", err)
}
}
func formHandler(w http.ResponseWriter, r *http.Request) {
response, err := http.Get("https://groupietrackers.herokuapp.com/api")
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
var responseObject Response
json.Unmarshal(responseData, &responseObject)
artist_link, err := http.Get(responseObject.Artists)
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
artistData, err := ioutil.ReadAll(artist_link.Body)
if err != nil {
log.Fatal(err)
}
var artistObject Artist
json.Unmarshal(artistData, &artistObject)
t, _ := template.ParseFiles("index.html")
t.Execute(w, artistObject)
}
index.html file for showing information to pearson.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<style>
iframe {
width: 99vw;
}
.card {
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
transition: 0.3s;
width: 40%;
}
.card:hover {
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
}
.container {
padding: 2px 16px;
}
</style>
<body>
<header>
<h1>groupie-tracker</h1>
</header>
<div>
{{range .}}
<div >
<div >
<h4><b>{{ .Name }}</b></h4>
<p>{{ .FirstAlbum }}</p>
</div>
</div>
{{end}}
</div>
</body>
</html>
CodePudding user response:
There are a couple ways to handle templates. But you definitely want to parse your files once and store them.
One way is to create a global var and store the template/templates in there.
Also you expect a slice of Artist structs, correct? So you want to unmarshal into a slice, not a single object. If you check the error on unmarshal, you will see that.
Last, there is a problem with the CreationDate field. Unmarshal error shows that too.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"text/template"
)
type Response struct {
Artists string `json:"artists"`
Locations string `json:"locations"`
Dates string `json:"dates"`
Relation string `json:"relation"`
}
type Artist struct {
ID int `json:"id"`
Image string `json:"image"`
Name string `json:"name"`
Members []string `json:"members"`
CreationDate int `json:"creationDate"`
FirstAlbum string `json:"firstAlbum"`
}
var temp *template.Template
func main() {
temp = template.Must(template.ParseFiles("index.html"))
http.HandleFunc("/", formHandler) // to define the path/page, where we are going to use the mentioned function
fmt.Printf("Starting server at port 8080\n")
if err := http.ListenAndServe(":8080", nil); err != nil { //to initialise our server on :8080
log.Fatal("HTTP status 500 - Internal server error: %s", err)
}
}
func formHandler(w http.ResponseWriter, r *http.Request) {
response, err := http.Get("https://groupietrackers.herokuapp.com/api")
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
var responseObject Response
json.Unmarshal(responseData, &responseObject)
artist_link, err := http.Get(responseObject.Artists)
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
artistData, err := ioutil.ReadAll(artist_link.Body)
if err != nil {
log.Fatal(err)
}
var artists []Artist
err = json.Unmarshal(artistData, &artists)
if err != nil {
log.Fatal(err)
}
temp.Execute(w, artists)
}