Skip to content

ramirezag/gocleancode

Repository files navigation

Overview

For Developers

Setup

Prerequisite

1. Install GIT 2. Install Go 1.11.4 or later. 3. Set the GOPATH and add go binaries. Below is the sample config in ~/.bash_profile in mac osx.

export GOPATH=$HOME/workspace/go
export PATH=$PATH:$GOPATH/bin

4. Install dep. For Windows users, you can download from releases page. Please take sometime reading the usage of go dep specially in adding new dependencies part.

Steps

1. Checkout the repository

cd $GOPATH/src
git clone https://github.com/ramirezag/gocleancode.git

2. Pull the project dependencies

  $ cd gocleancode
  $ dep ensure

3. Execute gocleancode/db/mysqlSchema.sql to your mysql db.

Configure the APP config

Option 1: Using environment variables

Set the following environment variables

HOST= # For mac users, use localhost due avoid the annoying popup.
APP_PORT= # defaults to 8000 if not set.
DB_HOST=
DB_PORT=
DB_USER=
DB_PASS=
DB_NAME=

Option 2: Using config files

This approach is useful when you want to run the app in commandline and would not like to set the environment variables. Note that environment variables will take precedence over config files.

1. Copy config.<env>.json.sample and name it config.local.json
2. Fill the properties according to your needs.
3. Execute

$ cd $GOPATH/src/gocleancode
$ export ENV=local && go run main.go

3.1. For windows users, manually set ENV environment variable to local. Then execute go run main.go.

Run the server

Execute go run main.go

Run the tests

Execute go test -cover ./...

Development and Unit Test Guidelines

  • Read How to Write Go Code.
  • Prefix tests packages with _test to avoid import-cycle-not-allowed error. Eg, package repository_test
  • Always program to interface so that it replace implementations should we need it - eg, mocking dependencies.
  • use mockery in generating mocks of interfaces.
  • To avoid issues, capitalize the first character of the function in interfaces
  • Steps to generate mocks
    • cd $GOPATH/src/gocleancode/<to_dir_with_interface>
    • mockery -name <interface_name>
  • Example steps to generate mocks
        cd repository
        mockery -name FileRepo
  • As much as possible, don't use log.Fatal or os.Exit(1) unless you're sure that no further clean is needed. Those commands will immediately terminate the app and won't invoke shutdown hooks (eg, in db.go and main.go).

APIs

Below are the list of available APIs exposed by this service.

API Success Response Description
POST /files { "success": true, "message": "Created file with id 1." } Multipart Upload files. Note: Parameter file should be used. Eg, <input type="file" name="file" />
GET /files/{fileId} File Stream Download file by file id. Use a browser to see the file.
DELETE /files/{fileId} { "success": true, "message": "Successfully deleted file with id 1" } Delete file by id.

Sample usage

curl -X GET http://localhost:8000/files/1

About

Sample project for Clean Code using GoLang article

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages