Code Monkey home page Code Monkey logo

restapi's Introduction

theme transition highlightTheme
white
zoom
darkula

REST API in Go

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


Dependencies

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

Code Organization

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.


Build

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.


Testing

To execute the unit tests, run:

make test

Running

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.


Docker

Docker Build

The Makefile has a target to build a docker image.

make docker

Docker Run

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

Docker Publish

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.


API - REST Endpoints

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

restapi's People

Contributors

dealako avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.