diff --git a/README b/README new file mode 100644 index 0000000000000000000000000000000000000000..83b02624323a738cdcb4c6326743283b30666c52 --- /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 39598179d9d6898d5e60c559436755cf54b908e9..a42b9912593593e1caf30133746d05a79c440bba 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 0000000000000000000000000000000000000000..8a7f73ea68964059930253a7f258a7648fe2900c --- /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 6e5df0e0956a8813bd80f20332963877d0d5f1d6..ef761f99fd429328bc02dc902b583e37f47b650f 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 });