You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

github.com/umangraval/go-mongodb-rest-boilerplate

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/umangraval/go-mongodb-rest-boilerplate

v0.0.0-20201109195127-d5616976a7cb
Source
Go
Version published
Created
Source

Go-Mongodb-REST-boilerplate

Build

This repo can be used as a starting point for backend development with Golang. It comes bundled with Docker. The development environment uses docker-compose to start dependent services like mongo.

A few things to note in the project:

  • Github Actions Workflows - Pre-configured Github Actions to run automated builds and publish image to Github Packages
  • Dockerfile - Dockerfile to generate docker builds.
  • docker-compose - Docker compose script to start service in production mode.
  • Containerized Mongo for development - Starts a local mongo container with data persistence across runs.
  • Mongo Driver - MongoDB supported driver for Go.
  • Gorilla Mux - HTTP request multiplexer.
  • jwt-go - Implementation of JWT Tokens.
  • Validator - Package validator implements value validations for structs.

Installation

1. Clone this repo

$ git clone git@github.com:umangraval/Go-Mongodb-REST-boilerplate.git your-app-name
$ cd your-app-name

2. Install dependencies

$ go mod vendor

Development

Start dev server

Starting the dev server also starts MongoDB as a service in a docker container using the compose script at docker-compose.yml.

$ go run main.go routes.go

Running the above commands results in

  • 🌏 API Server running at http://localhost:8080
  • MongoDB running at mongodb://localhost:27017/db

Packaging and Deployment

1. Build and run without Docker

$ go build 

2. Run Tests

$ cd tests
$ go test

3. Run with docker

$ docker build -t api-server .
$ docker run -t -i -p 8080:8080 api-server

4. Run with docker-compose

$ docker-compose up

Environment

To edit environment variables, create a file with name .env and copy the contents from .env.default to start with.

Var NameTypeDefaultDescription
JWT_SECRETstringsecretJWT secret to verify
PORTnumber8080Port to run the API server on
MONGO_URLstringmongodb://localhost:27017/dbURL for MongoDB

Directory Structure

+-- controllers
|   +-- personController.go
+-- db
|   +-- db.go
+-- handlers
|   +-- config.go
|   +-- logs.go
|   +-- response.go
|   +-- verifyJWT.go
+-- models
|   +-- models.go
+-- validators
|   +-- validators.go
+-- tests
|   +-- api_test.go
+-- routes
|   +-- routes.go
+-- uploaded
+-- vendor
+-- nginx
|   +-- dev.conf.d
|   |   +-- nginx.conf
+-- .env
+-- .env.default
+-- .gitignore
+-- docker-compose.yml
+-- Dockerfile
+-- go.mod
+-- go.sum
+-- main.go
+-- README.md

FAQs

Package last updated on 09 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts