Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
b-j-roberts committed Apr 22, 2024
1 parent 3b2e739 commit aaee25d
Show file tree
Hide file tree
Showing 8 changed files with 556 additions and 557 deletions.
16 changes: 8 additions & 8 deletions backend/config/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
)

type BackendScriptsConfig struct {
PlacePixelDevnet string `json:"place_pixel_devnet"`
PlaceExtraPixelsDevnet string `json:"place_extra_pixels_devnet"`
AddTemplateDevnet string `json:"add_template_devnet"`
MintNFTDevnet string `json:"mint_nft_devnet"`
PlacePixelDevnet string `json:"place_pixel_devnet"`
PlaceExtraPixelsDevnet string `json:"place_extra_pixels_devnet"`
AddTemplateDevnet string `json:"add_template_devnet"`
MintNFTDevnet string `json:"mint_nft_devnet"`
}

type BackendConfig struct {
Expand All @@ -22,10 +22,10 @@ var DefaultBackendConfig = BackendConfig{
Host: "localhost",
Port: 8080,
Scripts: BackendScriptsConfig{
PlacePixelDevnet: "../scripts/place_pixel.sh",
PlaceExtraPixelsDevnet: "../scripts/place_extra_pixels.sh",
AddTemplateDevnet: "../scripts/add_template.sh",
MintNFTDevnet: "../scripts/mint_nft.sh",
PlacePixelDevnet: "../scripts/place_pixel.sh",
PlaceExtraPixelsDevnet: "../scripts/place_extra_pixels.sh",
AddTemplateDevnet: "../scripts/add_template.sh",
MintNFTDevnet: "../scripts/mint_nft.sh",
},
}

Expand Down
417 changes: 208 additions & 209 deletions backend/routes/indexer.go

Large diffs are not rendered by default.

296 changes: 148 additions & 148 deletions backend/routes/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,184 +14,184 @@ import (
)

func InitNFTRoutes() {
http.HandleFunc("/get-nft", getNFT)
http.HandleFunc("/get-nfts", getNFTs)
http.HandleFunc("/get-my-nfts", getMyNFTs)
http.HandleFunc("/mint-nft-devnet", mintNFTDevnet)
// Create a static file server for the nft images
http.Handle("/nft-images/", http.StripPrefix("/nft-images/", http.FileServer(http.Dir("."))))
//http.HandleFunc("/nft-image", nftImage)
http.HandleFunc("/get-nft", getNFT)
http.HandleFunc("/get-nfts", getNFTs)
http.HandleFunc("/get-my-nfts", getMyNFTs)
http.HandleFunc("/mint-nft-devnet", mintNFTDevnet)
// Create a static file server for the nft images
http.Handle("/nft-images/", http.StripPrefix("/nft-images/", http.FileServer(http.Dir("."))))
//http.HandleFunc("/nft-image", nftImage)
}

type NFTData struct {
TokenID int `json:"tokenId"`
Position int `json:"position"`
Width int `json:"width"`
Height int `json:"height"`
ImageHash string `json:"imageHash"`
BlockNumber int `json:"blockNumber"`
Minter string `json:"minter"`
TokenID int `json:"tokenId"`
Position int `json:"position"`
Width int `json:"width"`
Height int `json:"height"`
ImageHash string `json:"imageHash"`
BlockNumber int `json:"blockNumber"`
Minter string `json:"minter"`
}

func getNFT(w http.ResponseWriter, r *http.Request) {
tokenId := r.URL.Query().Get("tokenId")

var nftData NFTData
rows, err := core.ArtPeaceBackend.Databases.Postgres.Query(context.Background(), "SELECT * FROM nfts WHERE token_id = $1", tokenId)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
defer rows.Close()

err = rows.Scan(&nftData.TokenID, &nftData.Position, &nftData.Width, &nftData.Height, &nftData.ImageHash, &nftData.BlockNumber, &nftData.Minter)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

out, err := json.Marshal(nftData)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(out))
tokenId := r.URL.Query().Get("tokenId")

var nftData NFTData
rows, err := core.ArtPeaceBackend.Databases.Postgres.Query(context.Background(), "SELECT * FROM nfts WHERE token_id = $1", tokenId)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
defer rows.Close()

err = rows.Scan(&nftData.TokenID, &nftData.Position, &nftData.Width, &nftData.Height, &nftData.ImageHash, &nftData.BlockNumber, &nftData.Minter)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

out, err := json.Marshal(nftData)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(out))
}

func getMyNFTs(w http.ResponseWriter, r *http.Request) {
address := r.URL.Query().Get("address")

var nftDatas []NFTData
rows, err := core.ArtPeaceBackend.Databases.Postgres.Query(context.Background(), "SELECT * FROM nfts WHERE minter = $1", address)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
defer rows.Close()

for rows.Next() {
var nftData NFTData
err = rows.Scan(&nftData.TokenID, &nftData.Position, &nftData.Width, &nftData.Height, &nftData.ImageHash, &nftData.BlockNumber, &nftData.Minter)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
nftDatas = append(nftDatas, nftData)
}

w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

out, err := json.Marshal(nftDatas)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(out))
address := r.URL.Query().Get("address")

var nftDatas []NFTData
rows, err := core.ArtPeaceBackend.Databases.Postgres.Query(context.Background(), "SELECT * FROM nfts WHERE minter = $1", address)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
defer rows.Close()

for rows.Next() {
var nftData NFTData
err = rows.Scan(&nftData.TokenID, &nftData.Position, &nftData.Width, &nftData.Height, &nftData.ImageHash, &nftData.BlockNumber, &nftData.Minter)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
nftDatas = append(nftDatas, nftData)
}

w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

out, err := json.Marshal(nftDatas)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(out))
}

func getNFTs(w http.ResponseWriter, r *http.Request) {
// TODO: Pagination & Likes
var nftDatas []NFTData
rows, err := core.ArtPeaceBackend.Databases.Postgres.Query(context.Background(), "SELECT * FROM nfts")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
defer rows.Close()

for rows.Next() {
var nftData NFTData
err = rows.Scan(&nftData.TokenID, &nftData.Position, &nftData.Width, &nftData.Height, &nftData.ImageHash, &nftData.BlockNumber, &nftData.Minter)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
nftDatas = append(nftDatas, nftData)
}

w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

out, err := json.Marshal(nftDatas)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(out))
// TODO: Pagination & Likes
var nftDatas []NFTData
rows, err := core.ArtPeaceBackend.Databases.Postgres.Query(context.Background(), "SELECT * FROM nfts")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
defer rows.Close()

for rows.Next() {
var nftData NFTData
err = rows.Scan(&nftData.TokenID, &nftData.Position, &nftData.Width, &nftData.Height, &nftData.ImageHash, &nftData.BlockNumber, &nftData.Minter)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
nftDatas = append(nftDatas, nftData)
}

w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

out, err := json.Marshal(nftDatas)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(out))
}

// func nftImage(w http.ResponseWriter, r *http.Request) {
// // Get the png image at location "nft-{tokenId}.png"
// tokenId := r.URL.Query().Get("tokenId")
// imageLocation := fmt.Sprintf("nft-%s.png", tokenId)
//
//
// image, err := os.Open(imageLocation)
// if err != nil {
// w.WriteHeader(http.StatusInternalServerError)
// w.Write([]byte(err.Error()))
// return
// }
// defer image.Close()
//
//
// w.Header().Set("Access-Control-Allow-Origin", "*")
// w.Header().Set("Content-Type", "image/png")
// w.WriteHeader(http.StatusOK)
//
//
// io.Copy(w, image)
// }

func mintNFTDevnet(w http.ResponseWriter, r *http.Request) {
reqBody, err := io.ReadAll(r.Body)
if err != nil {
panic(err)
}
var jsonBody map[string]string
err = json.Unmarshal(reqBody, &jsonBody)
if err != nil {
panic(err)
}

position, err := strconv.Atoi(jsonBody["position"])
if err != nil {
panic(err)
}

width, err := strconv.Atoi(jsonBody["width"])
if err != nil {
panic(err)
}

height, err := strconv.Atoi(jsonBody["height"])
if err != nil {
panic(err)
}

shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.MintNFTDevnet
contract := os.Getenv("ART_PEACE_CONTRACT_ADDRESS")

cmd := exec.Command(shellCmd, contract, "mint_nft", strconv.Itoa(position), strconv.Itoa(width), strconv.Itoa(height))
_, err = cmd.Output()
if err != nil {
fmt.Println("Error executing shell command: ", err)
panic(err)
}

w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write([]byte("Minted NFT on devnet"))
reqBody, err := io.ReadAll(r.Body)
if err != nil {
panic(err)
}
var jsonBody map[string]string
err = json.Unmarshal(reqBody, &jsonBody)
if err != nil {
panic(err)
}

position, err := strconv.Atoi(jsonBody["position"])
if err != nil {
panic(err)
}

width, err := strconv.Atoi(jsonBody["width"])
if err != nil {
panic(err)
}

height, err := strconv.Atoi(jsonBody["height"])
if err != nil {
panic(err)
}

shellCmd := core.ArtPeaceBackend.BackendConfig.Scripts.MintNFTDevnet
contract := os.Getenv("ART_PEACE_CONTRACT_ADDRESS")

cmd := exec.Command(shellCmd, contract, "mint_nft", strconv.Itoa(position), strconv.Itoa(width), strconv.Itoa(height))
_, err = cmd.Output()
if err != nil {
fmt.Println("Error executing shell command: ", err)
panic(err)
}

w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write([]byte("Minted NFT on devnet"))
}
Loading

0 comments on commit aaee25d

Please sign in to comment.