Coder Social home page Coder Social logo

avik1993 / wire-web-demo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fsufitch/wire-web-demo

0.0 1.0 0.0 38 KB

A basic visitor counter website, with a modular Wire-based framework

Home Page: https://medium.com/@fsufitch/dependency-injection-and-testability-in-a-go-webservice-a91d0e5469dd

License: MIT License

Go 99.45% Dockerfile 0.55%

wire-web-demo's Introduction

Wire Web Application Demo

What is this? This is a simple webserver that just serves a visitor counter and its uptime seconds. It is built using Go, Postgres, and structured using dependency injection via Google Wire.

Buy why though? Because it serves as a good example for the future, for myself and others. The DI "modules" (config, log, db, etc) are fully-fledged and feature good unit testing and environment-based setup. They are ready to be copy-pasted to other projects, or used as reference points.

Some rationale and discussion behind how/why this was put together can be found in a blog post here.

Setup and running

Building. The code is built using Go 1.12 and Go modules. All you need to do to build it is:

go build

To re-generate wire_gen.go, use:

go generate

Testing. To run the unit tests and see coverage:

go test ./... -cover

On Windows, ignore any UAC port opening warning that comes up.

Environment. The runtime is parameterized via environment variables:

  • PORT - the port to serve the webserver on (optional; default=8080)
  • DATABASE - the PostgreSQL connection string for its database (required)
  • DEBUG - true/false toggle for debug logging (optional; default=false)

Database setup. The code bootstraps its own schema, so you just need to provide it with a database. An easy Docker-based way to do this would be:

docker run -p 5432:5432 postgres:latest
docker run --net=host -it postgres:latest psql postgres://postgres@localhost:5432
> CREATE DATABASE demo;

This would result in this connection string to be set as DATABASE (note the sslmode parameter):

postgres://postgres@localhost:5432/demo?sslmode=disable

Endpoints.

  • /uptime - displays the seconds since the server got started
  • /counter - increments and displays a visitor counter from the database

wire-web-demo's People

Contributors

fsufitch avatar

Watchers

James Cloos 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.