api

package module
v0.0.0-...-18a4ca1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2020 License: MIT Imports: 16 Imported by: 0

README

API

Simple proof of concept web-service / API in Go.

  1. API
    1. Requirements
    2. Installation
      1. Database setup
      2. Start the server
    3. Usage
      1. Error model
      2. Create
      3. Lookup
      4. List
      5. Health

Requirements

go: >= 1.15
postgresql >= 11.x

Installation

apt-get install postgresql postgresql-client
sudo -u postgres psql postgres

After dropping into the postgres shell change the password

#\password
Enter new password:
123456
\q
Database setup
export DB_HOST=localhost
export DB_PORT=5432
export DB_USER=postgres
export DB_PASSWORD=123456
export DB_NAME=test

Run go test in the storage package to initialize the database schema (see storage_test.go).

Start the server
make run

or equivalently

go run cmd/api/api.go

Usage

psql -U postgres -d test -a -f storage/00.sql 
Create

Create a product.

Example request

curl --header "Content-Type: application/json" \
	--request POST \
	--data '{"sku":"9a982384y932", "attrs":{"mass":"undefined"}}' \
    http://localhost:8080/product/create
Lookup

Lookup a product by SKU.

Example request

curl "http://localhost:8080/product/get?sku=9a982384y932" | jq
List

List products (paginated).

Example request

curl "http://localhost:8080/product/list?lo=0&hi=10" | jq
Delete

Delete a product

Example request

curl "http://localhost:8080/product/delete?sku=9a982384y932" | jq

### Health
The healthz endpoint returns the server's health status; when the server
is started if backing systems are unreachable, all other endpoints will
respond with `HTTP 503 ServiceUnavailable`

Example request
```sh
curl http://localhost:8080/healthz

Documentation

Overview

Package api contains routines that expose a RESTful API via HTTP

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Main

func Main(addr string)

func Server

func Server(addr string) *http.Server

Server creates an http.Server capable of handling requests against the Product REST API.

Types

type API

type API struct {
	// contains filtered or unexported fields
}
var (
	ProductRepository *API
)

func NewRepository

func NewRepository() (*API, error)

NewRepository returns a new Repository object using the given operations

func (*API) List

func (r *API) List(ctx context.Context, lo, hi int) ([]*Product, error)

func (*API) Lookup

func (r *API) Lookup(ctx context.Context, sku string) (*Product, error)

func (*API) Store

func (r *API) Store(ctx context.Context, prod *Product) error

type Attrs

type Attrs map[string]string

Attrs is a map of key-value pairs associated with a product.

func (*Attrs) Scan

func (a *Attrs) Scan(v interface{}) error

Scan implements the sql.Scanner interface

func (*Attrs) Value

func (a *Attrs) Value() (driver.Value, error)

Value implements the SQL driver.Value interface

type Product

type Product struct {
	SKU   string `json:"sku"`
	Attrs Attrs  `json:"attrs"`
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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