Skip to content

skport/go-api-server-echo

Repository files navigation

go-api-server-echo

An API server example using Echo with Swagger.

Dependencies

Detail : go.mod

Architecture

Layered Architecture

Note : The dependency of repository on domain should be fixed.

flowchart LR
    subgraph Presentation
    id1["handlers/"]
    end
    subgraph Application
    services/
    end
    subgraph Domain
    domains/
    end
    subgraph Repository
    repositorys/
    end
    Presentation --> Application
    Application --> Domain
    Application --> Repository
    Repository --> Domain
Loading

Directory

key value
handlers controller, routing
services usecase, Application Logic
repository data store
docs API documents to generated with Swagger
⚠️Note: Don't change directly. Must use swagger cmd.
configs Not in use now

Installation

Create go.sum:

go mod tidy

Usage

  1. Run go run . to start server.
  2. Open http://localhost:3000 with your browser to see the result.
  3. Open http://localhost:3000/swagger/index.html, you can see Swagger Api documents.

Get albums

Request (GET):

curl 'http://localhost:3000/albums'

Response body:

[
  {
    "id": 1,
    "title": "Blue Train",
    "artist": "John Coltrane",
    "price": 56.99
  },
  {
    "id": 2,
    "title": "Jeru",
    "artist": "Gerry Mulligan",
    "price": 17.99
  },
  {
    "id": 3,
    "title": "Sarah Vaughan and Clifford Brown",
    "artist": "Sarah Vaughan",
    "price": 39.99
  }
]

Post album

Request (POST):

curl -X POST 'http://localhost:3000/albums' -H 'Content-Type: application/json' -d '{"id":4, "title":"Sun", "artist":"Apple", "price":10.12}'

Response body:

Accepted

Environment

The environment is determined by .env.

development: (.env .development):

APP_ENV=development

other: (.env .inmemory):

APP_ENV=inmemory

If "development" is selected, the datastore is a MySQL server. and a Docker container must be started for MySQL. =>

Otherwise, the datastore is in-memory and no DB server is required.

Note: .env should normally be added to .gitignore.

Generate API documents using Swagger

  1. Install Swag
go install github.com/swaggo/swag/cmd/swag@lates
  1. Write API annotations to go file. (Swagger Document)

  2. Run swag init and generate API documents in /docs directory.

swag init
  1. Restart Server.

Create Containers for MySQL & Adminer

docker-compose up -d
key host port view
app 127.0.0.1 3000 http://localhost:3000/
db 127.0.0.1 3306
adminer 127.0.0.1 8080 http://localhost:8080/

If you want a go execution environment container, use the app container. After the container shell is started, the prescribed flow is simply performed within the container.

  1. Installation
  2. Usage

Testing

go test ./handlers/ -v

In the go test command for handlers, the data store is always in-memory.

ToDo

More Code Design.

About

An API server example using Echo with Swagger.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published