From 96d36f6adad54398a464370de093f623207cf878 Mon Sep 17 00:00:00 2001 From: ptisserand Date: Tue, 16 Apr 2024 03:20:09 +0200 Subject: [PATCH] [feat] Create production flag on the backend (#35) * [feat] backend production flag (#3) * backend: disable `devnet` routes when production mode is enabled * Add production to config defaults --------- Co-authored-by: Brandon Roberts --- backend/config/backend.go | 8 +++++--- backend/main.go | 19 ++++++++++++++++++- backend/routes/pixel.go | 10 +++++++++- backend/routes/templates.go | 11 +++++++++-- configs/backend.config.json | 3 ++- configs/docker-backend.config.json | 3 ++- 6 files changed, 45 insertions(+), 9 deletions(-) diff --git a/backend/config/backend.go b/backend/config/backend.go index d14c7975..788d00ee 100644 --- a/backend/config/backend.go +++ b/backend/config/backend.go @@ -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{ @@ -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" diff --git a/backend/main.go b/backend/main.go index 1b03b991..8be1a9cd 100644 --- a/backend/main.go +++ b/backend/main.go @@ -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) @@ -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() } diff --git a/backend/routes/pixel.go b/backend/routes/pixel.go index 59656372..f1279503 100644 --- a/backend/routes/pixel.go +++ b/backend/routes/pixel.go @@ -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) } @@ -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) diff --git a/backend/routes/templates.go b/backend/routes/templates.go index d1717970..090e58e1 100644 --- a/backend/routes/templates.go +++ b/backend/routes/templates.go @@ -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 @@ -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 { + http.Error(w, "Not available in production", http.StatusNotImplemented) + return + } + reqBody, err := io.ReadAll(r.Body) if err != nil { panic(err) diff --git a/configs/backend.config.json b/configs/backend.config.json index b8f73c23..228cf22b 100644 --- a/configs/backend.config.json +++ b/configs/backend.config.json @@ -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 } diff --git a/configs/docker-backend.config.json b/configs/docker-backend.config.json index 1141700d..16a2e491 100644 --- a/configs/docker-backend.config.json +++ b/configs/docker-backend.config.json @@ -4,5 +4,6 @@ "scripts": { "place_pixel_devnet": "/scripts/place_pixel.sh", "add_template_hash_devnet": "/scripts/add_template_hash.sh" - } + }, + "production": false }