GithubHelp home page GithubHelp logo

joserfjuniorllms / rest-go-mux-pq Goto Github PK

View Code? Open in Web Editor NEW

This project forked from amundsenjunior/rest-go-mux-pq

0.0 0.0 0.0 5 KB

REST API example in Go, using PostgreSQL and gorilla/mux, from a SemaphoreCI tutorial

Go 100.00%

rest-go-mux-pq's Introduction

REST API in Go example

  • gorilla/mux for routing
  • lib/pq PostgreSQL driver
  • docker/lib/postgres as database

Source: https://semaphoreci.com/community/tutorials/building-and-testing-a-rest-api-in-go-with-gorilla-mux-and-postgresql

CRUD of products

  • create a new product: POST to /product
  • delete an existing product: DELETE to /product/{id}
  • update an existing product: PUT to /product/{id}
  • fetch an existing product: GET to /product/{id}
  • fetch a list of all existing products: GET to /products

create project workspace $ cd ~/dev/go/github.com/amundsenjunior/rest-go-mux-pq

get Go dependencies $ go get github.com/gorilla/mux github.com/lib/pq

Start PostgreSQL instance and create database & table

$ docker run --name rgmp -d -e POSTGRES_PASSWORD=restgomuxpq -p 5432:5432 postgres:alpine
$ docker run -it --rm --link rgmp:postgres postgres:alpine psql -h postgres -U postgres -W
  password: restgomuxpq
=# \list
=# CREATE DATABASE rgmp;
=# \c rgmp
  password: restgomuxpq
=# CREATE TABLE products (
    id SERIAL,
    name TEXT NOT NULL,
    price NUMERIC(10,2) NOT NULL DEFAULT 0.00,
    CONSTRAINT products_pkey PRIMARY KEY (id)
  );
=# \dt
=# \q

create application structure $ touch app.go main.go main_test.go model.go

define App type to hold application define main package to run service define products database model

start writing tests with test database include pre-testing database setup and post-testing database cleanup

execute TestMain with env vars $ export TEST_DB_USERNAME=postgres TEST_DB_PASSWORD=restgomuxpq TEST_DB_NAME=docker:5432/rgmp TEST_DB_SSLMODE=disable; go test -v

  1. add and execute TestEmptyTable, where expected is a 200 code, but response returns 404
  2. add and execute TestGetNonExistentProduct, where a GET request for a product by id should return error
  3. add and execute TestCreateProduct, where an OK response code should come from a POST request of a new product
  4. add and execute TestGetProduct, where an OK response code should come from a GET request on an existing product
  5. add and execute TestGetAllProducts, where an OK response code and correct length of response body shoudl come from a GET request on all existing products
  6. add and execute TestUpdateProduct, where an OK response code should come from a PUT request on an existing product to update it
  7. add and execute TestDeleteProduct, where an OK response code should come from a DELETE request on removing an existing product

execute application with env vars

$ export APP_DB_USERNAME=postgres APP_DB_PASSWORD=restgomuxpq APP_DB_NAME=docker:5432/rgmp APP_DB_SSLMODE=disable; go run main.go app.go model.go
$ go build; export APP_DB_USERNAME=postgres APP_DB_PASSWORD=restgomuxpq APP_DB_NAME=docker:5432/rgmp APP_DB_SSLMODE=disable; ./rest-go-mux-pq

TODO

  • doesn't error out when connection fails
  • doesn't log address on which it's available

GoDoc

References

rest-go-mux-pq's People

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.