Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Create production flag on the backend #35

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions backend/config/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,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 @@ -23,6 +24,7 @@ var DefaultBackendConfig = BackendConfig{
PlacePixelDevnet: "../scripts/place_pixel.sh",
AddTemplateHashDevnet: "../scripts/add_template_hash.sh",
},
Production: false,
}

var DefaultBackendConfigPath = "../configs/backend.config.json"
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") {
b-j-roberts marked this conversation as resolved.
Show resolved Hide resolved
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()
}
10 changes: 9 additions & 1 deletion backend/routes/pixel.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (
func InitPixelRoutes() {
http.HandleFunc("/getPixel", getPixel)
http.HandleFunc("/getPixelInfo", getPixelInfo)
http.HandleFunc("/placePixelDevnet", placePixelDevnet)
if !core.ArtPeaceBackend.BackendConfig.Production {
http.HandleFunc("/placePixelDevnet", placePixelDevnet)
}
http.HandleFunc("/placePixelRedis", placePixelRedis)
}

Expand Down Expand Up @@ -55,6 +57,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
11 changes: 9 additions & 2 deletions backend/routes/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
func InitTemplateRoutes() {
http.HandleFunc("/addTemplateImg", addTemplateImg)
http.HandleFunc("/addTemplateData", addTemplateData)
http.HandleFunc("/addTemplateHashDevnet", addTemplateHashDevnet)
if !core.ArtPeaceBackend.BackendConfig.Production {
http.HandleFunc("/addTemplateHashDevnet", addTemplateHashDevnet)
}
}

// TODO: Add specific location for template images
Expand Down Expand Up @@ -93,7 +95,12 @@ func addTemplateData(w http.ResponseWriter, r *http.Request) {
}

func addTemplateHashDevnet(w http.ResponseWriter, r *http.Request) {
// TODO: Disable this in production
// Disable this in production
if core.ArtPeaceBackend.BackendConfig.Production {
b-j-roberts marked this conversation as resolved.
Show resolved Hide resolved
http.Error(w, "Not available in production", http.StatusNotImplemented)
return
}

reqBody, err := io.ReadAll(r.Body)
if err != nil {
panic(err)
Expand Down
3 changes: 2 additions & 1 deletion configs/backend.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"scripts": {
"place_pixel_devnet": "../tests/integration/local/place_pixel.sh",
"add_template_hash_devnet": "../tests/integration/local/add_template_hash.sh"
}
},
"production": false
}
3 changes: 2 additions & 1 deletion configs/docker-backend.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"scripts": {
"place_pixel_devnet": "/scripts/place_pixel.sh",
"add_template_hash_devnet": "/scripts/add_template_hash.sh"
}
},
"production": false
}
Loading