From 5716c218f85e6b49e962ad1de7a96b20025dc2be Mon Sep 17 00:00:00 2001 From: meskio <meskio@sindominio.net> Date: Mon, 28 Sep 2020 20:36:39 +0200 Subject: [PATCH] Add deployment setup --- README | 45 +++++++++++++++++++++++++++++++++++++++++++++ main.go | 10 +++++++--- setup.sh | 8 ++++++++ src/App.js | 2 +- 4 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 README create mode 100755 setup.sh diff --git a/README b/README new file mode 100644 index 0000000..83b0262 --- /dev/null +++ b/README @@ -0,0 +1,45 @@ +# Cicer + +Cicer is a web based software to manage the stock, purchases and balance of a consumer association. It has being created for the needs of the [Garbanzo Negro](http://garbanzonegro.org). + +## deploy it + +Build a copy using make: +``` +$ make build +``` + +Create a secret for the authentication tokens: +``` +$ head -c 21 < /dev/urandom | base64 +``` + +Now you can copy the build folder and the cicer binary to your server and run it like: +``` +$ ./cicer -assets 'path/to/build' -secret 'scret' -addr ':8000' +``` +Instead of flags all the params can be passed as env variables see the names between `{` and `}` in: +``` +$ ./cicer -h +``` + +## run for development + +To run it in develop mode build and run the backend: +``` +$ go build +$ ./cicer +``` + +And run the frontend with npm: +``` +$ npm start +``` +This will open a browser pointing to `http://localhost:3000` where you will see any changes you do in the javascript side (the backend side needs recompilation). + +## Initialize data + +When you run cicer it will print an authentication token that we can use to set up some initial data, see `setup.sh`: +``` +$ ./setup.sh the.hexadecimal.token +``` diff --git a/main.go b/main.go index 3959817..a42b991 100644 --- a/main.go +++ b/main.go @@ -12,11 +12,13 @@ import ( func main() { var ( - dbPath = flag.String("db-path", env.String("./test.db", "DB_PATH"), "Path where the sqlite will be located") - addr = flag.String("addr", env.String(":8080", "HTTP_ADDR", "ADDR"), "Address where the http server will bind") - signKey = flag.String("signkey", env.String("", "SIGNKEY"), "Sign key for authentication tokens. DO NOT LEAVE UNSET!!!") + dbPath = flag.String("db-path", env.String("./test.db", "DB_PATH"), "Path where the sqlite will be located {DB_PATH}") + addr = flag.String("addr", env.String(":8080", "HTTP_ADDR", "ADDR"), "Address where the http server will bind {HTTP_ADDR}") + signKey = flag.String("signkey", env.String("", "SIGNKEY"), "Sign key for authentication tokens. DO NOT LEAVE UNSET!!! {SIGNKEY}") + assets = flag.String("assets", env.String("./build", "ASSETS_PATH"), "Path to the assets (js, html, ...) {ASSETS_PATH}") ) flag.Parse() + log.Println("listening in address:", *addr) r := mux.NewRouter() apiRouter := r.PathPrefix("/api/").Subrouter() @@ -25,5 +27,7 @@ func main() { log.Panicln("Can't open the database:", err) } + log.Println("assets:", *assets) + r.PathPrefix("/").Handler(http.FileServer(http.Dir(*assets))) log.Fatal(http.ListenAndServe(*addr, r)) } diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..8a7f73e --- /dev/null +++ b/setup.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +TOKEN="$1" + +curl -H "x-authentication: $TOKEN" -X "POST" -d '{"num": 10, "name": "foo", "email": "foo@example.com", "password": "foo", "balance": 10000}' localhost:8080/api/member +curl -H "x-authentication: $TOKEN" -X "POST" -d '{"code": 234, "name": "aceite", "price": 1700, "stock": 35}' localhost:8080/api/product +curl -H "x-authentication: $TOKEN" -X "POST" -d '{"code": 120, "name": "alubias", "price": 200, "stock": 20}' localhost:8080/api/product +curl -H "x-authentication: $TOKEN" -X "POST" -d '{"code": 302, "name": "esparragos", "price": 300, "stock": 15}' localhost:8080/api/product diff --git a/src/App.js b/src/App.js index 6e5df0e..ef761f9 100644 --- a/src/App.js +++ b/src/App.js @@ -52,7 +52,7 @@ class App extends React.Component { componentDidMount() { const token = localStorage.getItem("token"); - if (token !== "") { + if (token) { const num = localStorage.getItem("num"); const role = localStorage.getItem("role"); this.setState({ isLoged: true, num, role, token }); -- GitLab