A simple Go based REST API server built using gorilla/mux HTTP router and PostgreSQL database.
- Basic familiarity with Go and PostgreSQL, and
- That you have working Go and Docker installations. You can use Docker to run a test database easily.
- Start PostgreSQL container using:
$ ./run.sh startdb
- Once started, initialize database using:
$ ./run.sh initdb products_db
- Once initialized, start server using:
$ ./run.sh server -port=<port_number> # default: 8010
- Once started, you'll see a message like this:
Go REST api-server listening on port: 8010 Press Cmd+C / Ctrl+C to stop.
- Now launch your browser and go to
http://localhost:8010/api/products
- Once done, stop PostgreSQL container using:
$ ./run.sh stopdb
- To clean-up database, use:
$ ./run.sh cleanup
To run unit tests, start PostgreSQL container then do:
$ ./run.sh test
=== RUN TestEmptyTable
--- PASS: TestEmptyTable (0.01s)
=== RUN TestGetNonExistentProduct
--- PASS: TestGetNonExistentProduct (0.00s)
=== RUN TestCreateProduct
--- PASS: TestCreateProduct (0.01s)
=== RUN TestGetProduct
--- PASS: TestGetProduct (0.01s)
=== RUN TestUpdateProduct
--- PASS: TestUpdateProduct (0.02s)
=== RUN TestDeleteProduct
--- PASS: TestDeleteProduct (0.02s)
PASS
ok github.com/adwardstark/go-rest-api/app 0.604s
You can use Postman to perform CURD operations using following endpoints:
- GET
/api/products
- POST
/api/product
- GET
/api/product/{id}
- PUT
/api/product/{id}
- DELETE
/api/product/{id}
Apache 2.0. See the LICENSE file for details.