- install buffalo first, make sure your GOPATH is set properly
- crate new app
buffalo new myapi --api --db-type sqlite3
- go inside app and make some adjustments on
actions/app.go
Add the middleware line, its missing there, weird though
import ( "github.com/gobuffalo/buffalo-pop/pop/popmw" ... ) func App() *buffalo.App { if app == nil { ... } ... app.Use(popmw.Transaction(models.DB)) ... }
- start your app
ADDR=0.0.0.0 PORT=1234 buffalo dev
- to see your routes
buffalo routes
- to create a resource
buffalo g resource user first_name:text last_name:text phone_number:text password:text --skip-templates
- check the pending migration and migrate the db changes
buffalo db migrate status buffalo db migrate up
In case of postgresql you might need to create a database by self
my_api_db_name
psql -U postgres # assuming no password or funky pg setup ... create databse my_api_db_name
- Create a sample json
cat test/create_user.json
- Install
httpie:
https://github.com/jakubroztocil/httpie and test your endpoint- Create a user
http POST localhost:3000/users < ../myapi/test/create_user.json |jq .
- list all users:
curl -H Content-type:'application/json' http://localhost:3000/users/ |jq .
- get a user
curl -H Content-type:'application/json' localhost:3000/users/0d51c2c1-4aef-431e-885d-83016f193a5a |jq .
- delete a user:
curl -X DELETE localhost:3000/users/bc43a818-5801-472d-8ee3-7e2b21bb196c
- Create a user