Giter Club home page Giter Club logo

rest-api's Introduction

jlucktay's RESTful API (jra)

Travis Codecov Go Report Card GoDoc License

Description

This is my implementation of a RESTful HTTP API capable of CRUD operations and persisting resource state to a database.

Implementation guidelines

  • Follow best practices, for example TDD/BDD, with a focus on full-stack testing.
  • Prioritize correctness, robustness, and extensibility over extra features and optimizations.
  • Write code with the quality bar one would expect to see in production.
  • Try to simplify by using open source frameworks and libraries where possible.

Installation

Prerequisites

You should have a working Go environment and have $GOPATH/bin in your $PATH. Mage is being used for various build/run/test tasks, and should also be installed.

Compiling

To download the source, compile, and install the relevant binaries, run:

go get go.jlucktay.dev/rest-api/...

The source code will be located in $GOPATH/src/go.jlucktay.dev/rest-api.

Newly compiled jra and jrams binaries will be in $GOPATH/bin/.

Mage

The Magefile contains targets for various tasks, which can be listed out with mage -l.

Usage

Launching the API server:

Running with Docker

$ docker-compose up
Starting rest-api_web_1   ... done
Starting rest-api_mongo_1 ... done
Attaching to rest-api_mongo_1, rest-api_web_1
web_1    | Connected to MongoDB!
web_1    | Collection 'payments' contains 0 records.
...

The API server will be listening at http://localhost:8080/v1/.

Running the server directly

$ jra
Connected to MongoDB!
Collection 'payments' contains 0 records.

Seeding the server with some sample records:

$ jrams
Continuing will delete ALL payment records in MongoDB (database: rest-api, collection: payments)
Press 'Enter' to continue, or CTRL+C to cancel...
Connected to MongoDB!
Collection 'payments' contains 14 records.
Collection 'payments' dropped.
Disconnected from MongoDB.
Connected to MongoDB!
Collection 'payments' contains 0 records.
Added payment with ID '4ee3a8d8-ca7b-4290-a52c-dd5b6165ec43'.
...

Accessing the server:

$ curl --silent --request GET http://localhost:8080/v1/payments
{
  "data": [
    {
      "attributes": {
...

Documentation

Here is the full design doc for this API, which describes the various endpoints, how to call them, and what to expect in return.

Testing

There is a Mage target to run tests across all packages in the repo:

$ mage test
?       go.jlucktay.dev/rest-api/cmd/jra    [no test files]
?       go.jlucktay.dev/rest-api/internal/cmd/jrams [no test files]
?       go.jlucktay.dev/rest-api/pkg/org    [no test files]
ok      go.jlucktay.dev/rest-api/pkg/server 0.187s
ok      go.jlucktay.dev/rest-api/pkg/storage        0.262s
?       go.jlucktay.dev/rest-api/pkg/storage/inmemory       [no test files]
?       go.jlucktay.dev/rest-api/pkg/storage/mongo  [no test files]
ok      go.jlucktay.dev/rest-api/test       0.128s

For more details on how Go itself discovers and executes tests, and the various flags with which to alter behaviour when doing so, run go help test and go help testflag.

Roadmap

Features and functionality yet to be implemented are captured in the TODO markdown file in this repo as well as on the Trello board.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

rest-api's People

Contributors

dependabot-preview[bot] avatar jlucktay avatar

Watchers

 avatar  avatar

rest-api's Issues

Some functions are too long, per golangci-lint/funlen

Need to tidy up some funcs:

$ golangci-lint run --disable-all --enable funlen
pkg/server/handlers.update_test.go:17: Function 'TestUpdatePayment' has too many statements (42 > 40) (funlen)
func TestUpdatePayment(t *testing.T) {
test/dummy_body_test.go:19: Function 'TestDummyBodyCreateUpdate' is too long (74 > 60) (funlen)
func TestDummyBodyCreateUpdate(t *testing.T) {
test/nil_body_crud_test.go:17: Function 'TestNilBodyCRUD' is too long (110 > 60) (funlen)
func TestNilBodyCRUD(t *testing.T) {
pkg/storage/storage_test.go:16: Function 'TestStorage' is too long (86 > 60) (funlen)
func TestStorage(t *testing.T) {

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.