go-rest-api

command module
v0.0.0-...-c1b2651 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2021 License: Apache-2.0 Imports: 2 Imported by: 0

README

go-rest-api

A simple Go based REST API server built using gorilla/mux HTTP router and PostgreSQL database.

Prerequisites

  • 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.
Run locally

  • 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
    
Test

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
Endpoints

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}
License

Apache 2.0. See the LICENSE file for details.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL