Skip to content

Commit

Permalink
[feat] Create production flag on the backend (#35)
Browse files Browse the repository at this point in the history
* [feat] backend production flag (#3)

* backend: disable `devnet` routes when production mode is enabled

* Add production to config defaults

---------

Co-authored-by: Brandon Roberts <[email protected]>
  • Loading branch information
ptisserand and b-j-roberts authored Apr 16, 2024
1 parent e04b0eb commit 96d36f6
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
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") {
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 {
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
}

0 comments on commit 96d36f6

Please sign in to comment.