Skip to content

Latest commit

 

History

History
172 lines (143 loc) · 4.14 KB

readme.md

File metadata and controls

172 lines (143 loc) · 4.14 KB

Simple Go API

Simple Go API

  • Uses Posgresql (persistent storage)
  • Uses Redis (caching)
  • Uses JWT
  • Heroku based config

Tech

Simple Go API uses a number of open source projects to work properly:

  • Go - Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
  • Heroku - Heroku is a cloud platform that lets companies build, deliver, monitor and scale apps — we're the fastest way to go from idea to URL, bypassing all those infrastructure headaches.
  • Heroku Posgresql - Managed SQL Database as a Service for all developers.
  • Heroku Redis - The key-value data store you love, with the developer experience you deserve.
  • go-pg - Golang ORM with focus on PostgreSQL features and performance.
  • redigo - Redigo is a Go client for the Redis database.
  • JWT - JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.

Check Security Issues using GoSpec

$ cd github.com/jmilagroso
$ ./check_issues.sh

# Output
...
[gosec] 2019/02/11 14:56:25 Checking package: tests
{
        "Issues": [],
        "Stats": {
                "files": 19,
                "lines": 662,
                "nosec": 0,
                "found": 0
        }
}%

Running on Local

Postgresql Add-on

$ heroku addons:create heroku-postgresql:hobby-dev

Redis Add-on

$ heroku addons:create heroku-redis:hobby-dev

Using heroku local

$ cd github.com/jmilagroso
$ go install ./...
$ api

OR

$ heroku local -f Procfile

Setup .env

$ heroku config:get DATABASE_URL -s >> .env
$ heroku config:get REDIS_URL -s >> .env

Running with .env and different port

$ heroku local web -e .env -p 3001

API Usage

# Authenticate/Generate request token
curl -X POST \
  http://jaymilagroso-goapi.herokuapp.com/auth \
  -H 'cache-control: no-cache' \
  -F username=<username> \
  -F password=<password>


# Output
{
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NTAxMjU0NTUsImlhdCI6MTU0OTg2NjI1NSwic3ViIjoiMTAwMDgifQ.XsH7pA1O8nDlD3yllFk19_eN6DWkLhV5X2xHbRSn0Ks",
    "expiration": 1550125455
}
# List Users Paginated (Token required)
curl -X GET \
  https://jaymilagroso-goapi.herokuapp.com/users/1/5 \
  -H 'X-Token: <token>' \
  -H 'cache-control: no-cache'

# Output
[
    {
        "id": "10008",
        "username": "jaymilagroso2",
        "email": "[email protected]"
    },
    {
        "id": "10007",
        "username": "jaymilagroso1",
        "email": "[email protected]"
    },
    {
        "id": "10003",
        "username": "johndoe10000",
        "email": "jonhdoe@[email protected]"
    },
    {
        "id": "10002",
        "username": "johndoe9999",
        "email": "jonhdoe@[email protected]"
    },
    {
        "id": "10001",
        "username": "johndoe9998",
        "email": "jonhdoe@[email protected]"
    }
]
# Creates new User record (Token not required)
curl -X POST \
  http://jaymilagroso-goapi.herokuapp.com/user \
  -H 'cache-control: no-cache' \
  -F email=<email> \
  -F username=<username> \
  -F password=<password>

# Output
{
    "id": "",
    "username": "johndoe2019",
    "email": "[email protected]"
}
# Get user by id (Token required)
curl -X GET \
  https://jaymilagroso-goapi.herokuapp.com/users/<user_id> \
  -H 'X-Token: <token>' \
  -H 'cache-control: no-cache'

# Output
{
    "id": "9999",
    "username": "johndoe9996",
    "email": "jonhdoe@[email protected]"
}

References

License

This code is distributed using the Apache license, Version 2.0.

Author

Jay Milagroso [email protected]