Skip to content

Commit

Permalink
Resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
b-j-roberts committed Apr 22, 2024
2 parents aaee25d + 3fa4ac8 commit 2b4e0af
Show file tree
Hide file tree
Showing 45 changed files with 1,647 additions and 352 deletions.
27 changes: 27 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,33 @@
"contributions": [
"code"
]
},
{
"login": "ptisserand",
"name": "ptisserand",
"avatar_url": "https://avatars.githubusercontent.com/u/544314?v=4",
"profile": "https://github.com/ptisserand",
"contributions": [
"code"
]
},
{
"login": "mubarak23",
"name": "Mubarak Muhammad Aminu",
"avatar_url": "https://avatars.githubusercontent.com/u/7858376?v=4",
"profile": "http://mubarak23.github.io/",
"contributions": [
"code"
]
},
{
"login": "thomas192",
"name": "0xK2",
"avatar_url": "https://avatars.githubusercontent.com/u/65908739?v=4",
"profile": "https://github.com/thomas192",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ docker compose build

```bash
# Must install all the dependencies first
# Use npm install inside the `frontend` directory
# Change the user on `configs/database.config.json` for postgres
make integration-test-local
```
Expand Down Expand Up @@ -112,6 +113,11 @@ Thanks goes to these wonderful people. Follow the [contributors guide](https://g
<td align="center" valign="top" width="14.28%"><a href="https://nodeguardians.io/character/98995858fd55"><img src="https://avatars.githubusercontent.com/u/122918260?v=4?s=100" width="100px;" alt="Tristan"/><br /><sub><b>Tristan</b></sub></a><br /><a href="https://github.com/keep-starknet-strange/art-peace/commits?author=TAdev0" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Ayoazeez26"><img src="https://avatars.githubusercontent.com/u/44169294?v=4?s=100" width="100px;" alt="Abdulhakeem Abdulazeez Ayodeji"/><br /><sub><b>Abdulhakeem Abdulazeez Ayodeji</b></sub></a><br /><a href="https://github.com/keep-starknet-strange/art-peace/commits?author=Ayoazeez26" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/tekkac"><img src="https://avatars.githubusercontent.com/u/98529704?v=4?s=100" width="100px;" alt="Trunks @ Carbonable"/><br /><sub><b>Trunks @ Carbonable</b></sub></a><br /><a href="https://github.com/keep-starknet-strange/art-peace/commits?author=tekkac" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/ptisserand"><img src="https://avatars.githubusercontent.com/u/544314?v=4?s=100" width="100px;" alt="ptisserand"/><br /><sub><b>ptisserand</b></sub></a><br /><a href="https://github.com/keep-starknet-strange/art-peace/commits?author=ptisserand" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="http://mubarak23.github.io/"><img src="https://avatars.githubusercontent.com/u/7858376?v=4?s=100" width="100px;" alt="Mubarak Muhammad Aminu"/><br /><sub><b>Mubarak Muhammad Aminu</b></sub></a><br /><a href="https://github.com/keep-starknet-strange/art-peace/commits?author=mubarak23" title="Code">💻</a></td>
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/thomas192"><img src="https://avatars.githubusercontent.com/u/65908739?v=4?s=100" width="100px;" alt="0xK2"/><br /><sub><b>0xK2</b></sub></a><br /><a href="https://github.com/keep-starknet-strange/art-peace/commits?author=thomas192" title="Code">💻</a></td>
</tr>
</tbody>
<tfoot>
Expand Down
8 changes: 5 additions & 3 deletions backend/config/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ type BackendScriptsConfig struct {
}

type BackendConfig struct {
Host string `json:"host"`
Port int `json:"port"`
Scripts BackendScriptsConfig `json:"scripts"`
Host string `json:"host"`
Port int `json:"port"`
Scripts BackendScriptsConfig `json:"scripts"`
Production bool `json:"production"`
}

var DefaultBackendConfig = BackendConfig{
Expand All @@ -27,6 +28,7 @@ var DefaultBackendConfig = BackendConfig{
AddTemplateDevnet: "../scripts/add_template.sh",
MintNFTDevnet: "../scripts/mint_nft.sh",
},
Production: false,
}

var DefaultBackendConfigPath = "../configs/backend.config.json"
Expand Down
3 changes: 2 additions & 1 deletion backend/core/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
)

type Backend struct {
Databases *Databases
Databases *Databases
// TODO: Is this thread safe?
WSConnections []*websocket.Conn

CanvasConfig *config.CanvasConfig
Expand Down
1 change: 1 addition & 0 deletions backend/core/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func NewDatabases(databaseConfig *config.DatabaseConfig) *Databases {

// Connect to Postgres
postgresConnString := "postgresql://" + databaseConfig.Postgres.User + ":" + os.Getenv("POSTGRES_PASSWORD") + "@" + databaseConfig.Postgres.Host + ":" + strconv.Itoa(databaseConfig.Postgres.Port) + "/" + databaseConfig.Postgres.Database
// TODO: crd_audit?sslmode=disable
pgPool, err := pgxpool.New(context.Background(), postgresConnString)
if err != nil {
panic(err)
Expand Down
19 changes: 18 additions & 1 deletion backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@ import (
"github.com/keep-starknet-strange/art-peace/backend/routes"
)

func isFlagSet(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}

func main() {
canvasConfigFilename := flag.String("canvas-config", config.DefaultCanvasConfigPath, "Canvas config file")
databaseConfigFilename := flag.String("database-config", config.DefaultDatabaseConfigPath, "Database config file")
backendConfigFilename := flag.String("backend-config", config.DefaultBackendConfigPath, "Backend config file")
production := flag.Bool("production", false, "Production mode")

flag.Parse()

canvasConfig, err := config.LoadCanvasConfig(*canvasConfigFilename)
Expand All @@ -29,11 +41,16 @@ func main() {
panic(err)
}

if isFlagSet("production") {
backendConfig.Production = *production
}

databases := core.NewDatabases(databaseConfig)
defer databases.Close()

core.ArtPeaceBackend = core.NewBackend(databases, canvasConfig, backendConfig)

routes.InitRoutes()

core.ArtPeaceBackend = core.NewBackend(databases, canvasConfig, backendConfig)
core.ArtPeaceBackend.Start()
}
34 changes: 19 additions & 15 deletions backend/routes/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,26 @@ func InitCanvasRoutes() {
}

func initCanvas(w http.ResponseWriter, r *http.Request) {
// TODO: Check if canvas already exists
totalBitSize := core.ArtPeaceBackend.CanvasConfig.Canvas.Width * core.ArtPeaceBackend.CanvasConfig.Canvas.Height * core.ArtPeaceBackend.CanvasConfig.ColorsBitWidth
totalByteSize := (totalBitSize / 8)
if totalBitSize%8 != 0 {
// Round up to nearest byte
totalByteSize += 1
if core.ArtPeaceBackend.Databases.Redis.Exists(context.Background(), "canvas").Val() == 0 {
totalBitSize := core.ArtPeaceBackend.CanvasConfig.Canvas.Width * core.ArtPeaceBackend.CanvasConfig.Canvas.Height * core.ArtPeaceBackend.CanvasConfig.ColorsBitWidth
totalByteSize := (totalBitSize / 8)
if totalBitSize%8 != 0 {
// Round up to nearest byte
totalByteSize += 1
}

// Create canvas
canvas := make([]byte, totalByteSize)
ctx := context.Background()
err := core.ArtPeaceBackend.Databases.Redis.Set(ctx, "canvas", canvas, 0).Err()
if err != nil {
panic(err)
}

fmt.Println("Canvas initialized")
} else {
fmt.Println("Canvas already initialized")
}

canvas := make([]byte, totalByteSize)
ctx := context.Background()
err := core.ArtPeaceBackend.Databases.Redis.Set(ctx, "canvas", canvas, 0).Err()
if err != nil {
panic(err)
}

fmt.Println("Canvas initialized")
}

func getCanvas(w http.ResponseWriter, r *http.Request) {
Expand Down
127 changes: 127 additions & 0 deletions backend/routes/colors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package routes

import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"

"github.com/jackc/pgx/v5"
"github.com/keep-starknet-strange/art-peace/backend/core"
)

type Colors struct {
Hex string `json:"hex"`
}

func InitColorsRoutes() {
http.HandleFunc("/get-colors", GetAllColors)
http.HandleFunc("/get-color", GetSingleColor)
http.HandleFunc("/init-colors", InitColors)
}

func GetAllColors(w http.ResponseWriter, r *http.Request) {

var colors []Colors
rows, err := core.ArtPeaceBackend.Databases.Postgres.Query(context.Background(), "SELECT hex FROM colors")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

defer rows.Close()

for rows.Next() {
var c Colors
err := rows.Scan(&c.Hex)
if err != nil {
log.Fatalf("Scan failed: %v\n", err)
}
colors = append(colors, c)
}
if err := rows.Err(); err != nil {
log.Fatalf("Error retrieving data: %v\n", err)
}

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

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

func GetSingleColor(w http.ResponseWriter, r *http.Request) {

colorKey := r.URL.Query().Get("id")
if colorKey == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("ID not provided"))
return
}

var c Colors
row := core.ArtPeaceBackend.Databases.Postgres.QueryRow(context.Background(), "SELECT hex FROM colors WHERE key = $1", colorKey)
err := row.Scan(&c.Hex)
if err != nil {
if err == pgx.ErrNoRows {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Color not found"))
} else {
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(c)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(out))
}

func InitColors(w http.ResponseWriter, r *http.Request) {
// TODO: Add authentication and/or check if colors already exist
reqBody, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}

var colors []string
err = json.Unmarshal(reqBody, &colors)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}

for _, color := range colors {
_, err = core.ArtPeaceBackend.Databases.Postgres.Exec(context.Background(), "INSERT INTO colors (hex) VALUES ($1)", color)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("Colors initialized"))
fmt.Println("Colors initialized")
}
1 change: 1 addition & 0 deletions backend/routes/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const (
)

// TODO: User might miss some messages between loading canvas and connecting to websocket?
// TODO: Check thread safety of these things
func consumeIndexerMsg(w http.ResponseWriter, r *http.Request) {
requestBody, err := io.ReadAll(r.Body)
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions backend/routes/pixel.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import (
func InitPixelRoutes() {
http.HandleFunc("/getPixel", getPixel)
http.HandleFunc("/getPixelInfo", getPixelInfo)
http.HandleFunc("/placePixelDevnet", placePixelDevnet)
http.HandleFunc("/placeExtraPixelsDevnet", placeExtraPixelsDevnet)
if !core.ArtPeaceBackend.BackendConfig.Production {
http.HandleFunc("/placePixelDevnet", placePixelDevnet)
http.HandleFunc("/placeExtraPixelsDevnet", placeExtraPixelsDevnet)
}
http.HandleFunc("/placePixelRedis", placePixelRedis)
}

Expand Down Expand Up @@ -56,6 +58,12 @@ func getPixelInfo(w http.ResponseWriter, r *http.Request) {
}

func placePixelDevnet(w http.ResponseWriter, r *http.Request) {
// Disable this in production
if core.ArtPeaceBackend.BackendConfig.Production {
http.Error(w, "Not available in production", http.StatusNotImplemented)
return
}

reqBody, err := io.ReadAll(r.Body)
if err != nil {
panic(err)
Expand Down
Loading

0 comments on commit 2b4e0af

Please sign in to comment.