Skip to content

jecklgamis/go-webapp-example

Repository files navigation

go-webapp-example

Build

Go Report Card

This is a skeleton HTTP API server app written in Go.

Docker : docker run -p 8080:8080 -it jecklgamis/go-webapp-example:main

Features:

  • Uses gorilla mux request router
  • Uses spf13/viper for config management
  • Exposes Prometheus metrics endpoint and instrumented handlers
  • Ubuntu-based Docker image
  • Exposes /buildInfo, /probe/ready, /probe/live, /metrics handlers
  • Starts HTTP/HTTPS listeners
  • Uses environment specific config in YAML format

Requirements

Building

make build

This will:

  • Run all tests
  • Build server binary in bin (one for the OS you're building on and one for Linux AMD64 platform to be used inside the Docker image)
  • Build Docker image go-webapp-example:<current-branch>

Explore the Makefile or simply type make in the current directory for commonly used tasks.

Running

Run the server using the native binary:

$ bin/server

Run the server using Docker:

make run

Verify the endpoints:

curl http://localhost:8080/
curl http://localhost:8080/buildInfo
curl http://localhost:8080/probe/ready
curl http://localhost:8080/probe/live
curl http://localhost:8080/api
curl http://localhost:8080/metrics

Configuration

The config/config-<env>.yml contains the environment specific configuration. The config file is selected based on the APP_ENV variable and is dev by default.

Docker Image Namespace

The Docker image uses Ubuntu base image. It has the following directory namespace:

  • /app is the base app directory
  • /app/bin contains the server binary
  • /app/config contains the environment-specific config files in YAML format

Developing

The rebuilder directory contains helper scripts for automatically building the app on file changes. It depends on fswatchfor detecting file system changes in pkg and cmd directories.

Install fswatch (Mac OS):

brew install fswatch

Run the rebuilder:

make rebuilder

Linting

make lint

This wil run golint on all the Go sources it can find.

Testing

make test

This will run all *_test.go files it can find. `

Navigating The Sources

  • Program execution starts from cmd/server/server.go, it then starts the server in pkg/server/server.go
  • server.go loads environment-specific configuration under config.

Customizing

  • Replace all go-webapp-example references
  • Replace Go module import names in the Go sources

Example:

import (
	"github.com/<your-user-name>/<your-app-name>/pkg/server"
)

Contributing

Sure, send pull request?

About

An HTTP API server app using Gorilla Mux

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •