golang_startup

module
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 8, 2022 License: GPL-3.0

README

Golang Basic REST API

Golang basic REST API is a web application structure using an onion architecture approach.

It implements controller, service, database layers, that makes above layers independent of which database (or any other bottom layer type e.g. other API downstream) type is being used. There are cmd implemented examples of the application, for each database type: mongo, mysql and postgres.

It also could be a useful structure for Golang development, having model, config loading, implemented GitHub workflows, Makefile, that are often requirements of a Golang application.

1. Config

Before to run application you may setup .env with Config values. Check out the following tables to know all Config parameters detailed.

Parameter Description Type Default Required
DB_ADDRESS Database URL address. string YES
DB_NAME Name of the initial database to access. string NO
MIGRATIONS_PATH Migrations local path if needed. string NO
HOST HTTP server host number. string NO
PORT HTTP server port number. string YES

2. Run Application

After define env variables values, run the following commands to start web application server.

The different examples of running this application packages and features are available in cmd.

For a REST application with a postgres database layer use:

go run ./cmd/postgres/main.go

For a REST application with MySQL database layer use:

go run ./cmd/mysql/main.go

For a REST application with MongoDB database layer use:

go run ./cmd/mongo/main.go

3. API specifications

API specs could be find in openapi.yml and swagger.yml files.

3.1. Swagger commands

To generate Swagger spec in Unix run te following command:

swagger generate spec -o ./swagger.yml --scan-models

If you're using windows use the following command before swagger code:

docker run --rm -it --env GOPATH=/go -v %CD%:/go/src -w /go/src quay.io/goswagger/swagger <swagger code here>

For information about Swagger installation, check this:

Source: 🔗 Go Swagger

Directories

Path Synopsis
cmd
mongo command
mysql command
postgres command
internal
common
Package common holds useful code to be used across all package and layers.
Package common holds useful code to be used across all package and layers.
config
Package config holds configuration global data to be initialized.
Package config holds configuration global data to be initialized.
controller
Package controller holds application controllers.
Package controller holds application controllers.
database/mongodb
Package mongodb holds MongoDB database and repository methods.
Package mongodb holds MongoDB database and repository methods.
database/mysql
Package mysql holds MySQL database and repository methods.
Package mysql holds MySQL database and repository methods.
database/postgres
Package postgres holds PostgreSQL database and repository methods.
Package postgres holds PostgreSQL database and repository methods.
model
Package model holds domain model structs and methods.
Package model holds domain model structs and methods.
model/request
Package request holds request model structs and methods.
Package request holds request model structs and methods.
server
Package server holds server related struct and methods.
Package server holds server related struct and methods.
service
Package service holds service layer methods and repository/database abstractions.
Package service holds service layer methods and repository/database abstractions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL