theme | transition | highlightTheme |
---|---|---|
white |
zoom |
darkula |
This is a sample Go project that illustrates a REST service. The code example is a walkthru from a Traversy Media video on YouTube. The application includes a simple REST application with the usual crate, read, update, and delete (CRUD) functions.
Created by David Deal / @dealako
To build and run the application, the mux
router package and the logrus
logging package must be installed.
The Makefile
in the root folder includes a target to install these:
# Install dependencies
make install-deps
Otherwise, you can install them manually:
# Manually install dependencies
go get -u github.com/gorilla/mux
go get -u github.com/sirupsen/logrus
go get -u github.com/spf13/cobra
go get -u gopkg.in/alexcesaro/statsd.v2
The models for the sample REST application are in the models
folder which
consist of a Book, Author and Address objects.
The utils
folder contains the command line ascii artwork.
The main business logic is in the cmd
folder.
To build the application, run one of the following commands:
# Using make - will include additional metadata in the binary such as BUILD_TIME, VERSION, git commit/branch, and app name
make
or
make restapi
# Using go build which doesn't include additional metadata
go build
This will generate a binary for the current computer architecture.
To execute the unit tests, run:
make test
To run the application, run the following command. The default port is 8000
:
./restapi
or specify a different HTTP port:
./restapi -p 8080
Once the application is running, connect on port 8000 with a web browser, your favorite REST client such as Postman, or other HTTP clients such as cURL or even resty.
The Makefile
has a target to build a docker image.
make docker
To run the docker image, simply run:
docker run -it -p 8000:8000 dealako/restapi:<git hash>
for example:
docker run -it -p 8000:8000 dealako/restapi:38d8dff
To publish the docker image to hub.docker.com under the dealako project, run:
make docker-push
You will need permissions to push the docker image unless you change the image tag/path.
Method | REST Endpoint | Description |
---|---|---|
GET | /api/books | Retrives all the books |
GET | /api/books/{id} | Retrievs a specific book by ID |
POST | /api/books | Adds a book |
PUT | /api/books/{id} | Updates a book based on the book ID |
DELETE | /api/books/{id} | Deletes a specific book based on the ID |