Git Product home page Git Product logo

gloss's Introduction

gloss

gloss (Golang open simple service) provides boilerplate routing, database setup, and Docker files to get a minimal microservice up and running. It includes example code to increment and retrieve counter values from a PostgreSQL database. Ideally, one would fork, or clone + mirror push this repository, then edit the handlers + routes, database queries, and configurations for their own purposes. It uses chi for routing, and pq for its PostgreSQL database driver.

Prerequisites

Running

Using Docker

If you have Docker installed, getting the example stack up and running is as simple as:

./bootstrap_example.sh

This will build the image locally with a gloss:latest tag, then deploy a service stack with a PostgreSQL instance exposed on port 5432, and the example gloss service exposed on port 8080 on the host machine.

Local build

If you don't want to use Docker stack, you can build and run the example service locally.

You must first have a PostgreSQL service exposed on your host machine at port 5432, with the configuration in test.env, for example (using Docker cli for brevity):

docker run -d -p 5432:5432 \
-e POSTGRES_PASSWORD="password" \
-e POSTGRES_USER="test" \
-e POSTGRES_DB="test" \
postgres

then you can build and run the service locally:

# get the dependencies
go get -d ./...

# install to ~/go/bin
go install ./...

# export the test environment variables
while read l; do export $l; done < test.env

# run the service (this binds to port 8080 and waits for requests)
~/go/bin/gloss

Interacting with the example counter service

If you have deployed the example successfully, you can interact with the service at http://localhost:8080/v1

Example interaction:

# @URL: http://localhost:8080/v1/counter/{counterId}
# - POST: to increment/create a counter in the database
# - GET: to get the current counter value, if it exists

# create a counter with ID 4
curl -v http://localhost:8080/v1/counter/4 -X POST

# a counter has now been created with ID 4 and value 0

# increment it a few times
for i in $(seq 1 10); do curl -v http://localhost:8080/v1/counter/4 -X POST; done

# get the counter value
curl -v http://localhost:8080/v1/counter/4
# returns 10

# connect to the database and run some other queries
psql -h localhost -p 5432 -U test -d test

Adapting to your own implementation

  • Change the SQL query in db.go:CreateTables to use your custom schema
  • Replace/change example counter insert and select functions from db.go and db_test.go with your own
  • Replace/change example handlers in handlers.go with your own
  • Update the routes in routes.go to use your handlers

Disclaimer and considerations for deployment

The deployment scripts, configurations, and any defaults included in this repository are not, under any circumstances, to be used in production. They are here to provide a basic example deployment so you can quickly get up and running in a development environment. For a legitimate deployment, you must first edit them, or create your own configurations with secure settings.

As stated above, the responsibility to properly secure your code and deployment is entirely yours, but here are some things you may want to consider:

  • Ensure timeouts and size defaults are configured properly for the http.Server in cmd/gloss/main.go
  • Use SSL for communication with the database
  • Choose routing middleware to fit your needs
  • Properly manage credentials used/shared by the microservice and database

License

This project is licensed under the terms of the MIT license.

gloss's People

Contributors

diffuse 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.