Skip to content

a sample api to build api in golang with buffalo /cc @balmanrawat 🤓

Notifications You must be signed in to change notification settings

thapakazi/buffalo-sample-api

Repository files navigation

Simple buffalo guidelines

create new buffalo api

  1. install buffalo first, make sure your GOPATH is set properly
  2. crate new app
    buffalo new myapi --api --db-type sqlite3
        
  3. 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))
    	...
    }
        
  4. start your app
    ADDR=0.0.0.0 PORT=1234 buffalo dev
        
  5. to see your routes
    buffalo routes
        
  6. to create a resource
    buffalo g resource user first_name:text last_name:text  phone_number:text password:text --skip-templates
        
  7. 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
        

To test your api

  1. Create a sample json
    cat test/create_user.json
        
  2. 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
              

About

a sample api to build api in golang with buffalo /cc @balmanrawat 🤓

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published