📖 Build a RESTful API on Go: Fiber, PostgreSQL, JWT and Swagger docs in isolated Docker containers.

📖 Tutorial: Build a RESTful API on Go

Fiber, PostgreSQL, JWT and Swagger docs in isolated Docker containers.

👉 The full article is published on March 22, 2021, on Dev.to: https://dev.to/koddr/build-a-restful-api-on-go-fiber-postgresql-jwt-and-swagger-docs-in-isolated-docker-containers-475j

fiber_cover_gh

Quick start

  1. Rename .env.example to .env and fill it with your environment values.
  2. Install Docker and migrate tool for applying migrations.
  3. Run project by this command:
make docker.run

# Process:
#   - Generate API docs by Swagger
#   - Create a new Docker network for containers
#   - Build and run Docker containers (Fiber, PostgreSQL)
#   - Apply database migrations (using github.com/golang-migrate/migrate)
  1. Go to your API Docs page: 127.0.0.1:5000/swagger/index.html

Screenshot

⚠️ License

MIT © Vic Shóstak & True web artisans.

Owner
Vic Shóstak
A Software Engineer & founder of a friendly outsource company called @truewebartisans, with excellent UX/UI knowledge and over 12 years of practical experience.
Vic Shóstak
Comments
  • Dockerfile: COPY go.mod go.sum ./ error

    Dockerfile: COPY go.mod go.sum ./ error

    Required check list:

    • [x] I'm gonna mark the checkboxes like this.
    • [ ] I didn't find in the repository's issues section similar bug.
    • [x] I understand, this is Open Source and not-for-profit product.
    • [ ] This is not about third-party project, framework, package or technology.

    My environment:

    • OS (uname -a): Windows 10
    • Golang (go version): go1.16.7 windows/amd64

    Describe the bug: => ERROR [builder 3/6] COPY go.mod go.sum ./ 0.0s

    [builder 3/6] COPY go.mod go.sum ./:


    failed to compute cache key: "/go.sum" not found: not found

    Steps to reproduce the behavior:

    1. Create Dockerfile
    2. Go to docker build -t fiber .
    3. See error

    Screenshots: image

  • Select should be used for slices of results

    Select should be used for slices of results

    Using Get with a slice results in the error scannable dest type slice with >1 columns (xx) in result Documentation ref: https://jmoiron.github.io/sqlx/#getAndSelect

  • Error response from daemon: network with name dev-network already exist

    Error response from daemon: network with name dev-network already exist

    Required check list:

    • [x] I'm gonna mark the checkboxes like this.
    • [ ] I didn't find in the repository's issues section similar bug.
    • [x] I understand, this is Open Source and not-for-profit product.
    • [ ] This is not about third-party project, framework, package or technology.

    My environment:

    • OS (uname -a): Windows 10
    • Golang (go version): go1.16.7 windows/amd64

    Describe the bug: make docker.run docker network inspect dev-network >/dev/null 2>&1 ||
    docker network create -d bridge dev-network The system cannot find the path specified. Error response from daemon: network with name dev-network already exists make: *** [Makefile:36: docker.network] Error 1

    Steps to reproduce the behavior:

    1. Go to project
    2. make docker.run
    3. See error

    Screenshots: image

  • // @BasePath /api  如果放在Authorization之后,swag init 无法取到值,需要往前面放才能正确生成doc,不知道是为什么?swag v 1.8.8

    // @BasePath /api 如果放在Authorization之后,swag init 无法取到值,需要往前面放才能正确生成doc,不知道是为什么?swag v 1.8.8

    // @title API // @version 1.0 // @BasePath /api // @description This is an auto-generated API Docs. // @termsOfService http://swagger.io/terms/ // @contact.name API Support // @contact.email [email protected] // @license.name Apache 2.0 // @license.url http://www.apache.org/licenses/LICENSE-2.0.html // @securityDefinitions.apikey ApiKeyAuth // @in header // @name Authorization

    func main() { .... }

  • Error when creating book using swagger & curl

    Error when creating book using swagger & curl

    Required check list:

    • [x] I'm gonna mark the checkboxes like this.
    • [x] I didn't find in the repository's issues section similar bug.
    • [x] I understand, this is Open Source and not-for-profit product.
    • [x] This is not about third-party project, framework, package or technology.

    My environment:

    • OS (uname -a): Linux 89327648a97a 5.4.0-96-generic #109-Ubuntu SMP Wed Jan 12 16:49:16 UTC 2022 x86_64 Linux (inside docker)
    • Golang (go version): go version go1.18.3 linux/amd64

    Describe the bug:

    I followed your article & successfully run it. But when I tried to test the create book API, it fail. I tried using Swagger, it still the same. Do you have any sample JSON for create book ?

    Steps to reproduce the behavior:

    1. Run the app like shown in readme & run the migration. Make sure no records in DB.
    2. Generate token & copy the token using swagger or curl -X 'GET' 'http://127.0.0.1:5000/api/v1/token/new' -H 'accept: application/json'
    3. Put the token at swagger. By click "Authorize" button on top right. Fill with Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NTczMDIzNzF9.0VJubL5Z_49W2-9BHvqQaFn2pN1TxkysFIvzb5SpUow image
    4. Try Swagger POST /v1/book by clicking Try It Out & fill Title, Author, etc. image
    5. Click Execute
    6. See error image

    I tried to change the JSON, it still error, you can try this curl

    curl -X 'POST' \
      'http://127.0.0.1:5000/api/v1/book' \
      -H 'accept: application/json' \
      -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NTcyOTc2NzN9.fCjWNsiCVoUecjY1fhKNB_zHrZWS3SQohTeese5HdqM' \
      -H 'Content-Type: application/json' \
      -d '{
      "author": "Michael Crichton",
      "book_attrs": {
        "description": "A cautionary tale about genetic engineering",
        "picture": "https://upload.wikimedia.org/wikipedia/en/3/33/Jurassicpark.jpg",
        "rating": 8
      },
      "title": "Jurassic Park"
    }
    

    And it show error {"error":true,"msg":{"ID":"Key: 'Book.ID' Error:Field validation for 'ID' failed on the 'uuid' tag","UserID":"Key: 'Book.UserID' Error:Field validation for 'UserID' failed on the 'uuid' tag"}}

    Expected behavior:

    Successfully create book

    Screenshots:

    image

  • make docker.run fails on github.com/swaggo/swag

    make docker.run fails on github.com/swaggo/swag

    This is the error that comes back, also, it doesn't matter the version whether it is your default one or 1.8.0:

    => ERROR [builder 6/6] RUN go build -ldflags="-s -w" -o apiserver .                           0.7s ------
     > [builder 6/6] RUN go build -ldflags="-s -w" -o apiserver .:
    #10 0.672 /go/pkg/mod/github.com/swaggo/[email protected]/field_parser.go:13:2: missing go.sum entry for module providing package github.com/go-openapi/spec (imported by github.com/swaggo/swag); to add:
    #10 0.672       go get github.com/swaggo/[email protected]
    #10 0.672 /go/pkg/mod/github.com/swaggo/[email protected]/ab0x.go:15:2: missing go.sum entry for module providing package golang.org/x/net/webdav (imported by github.com/swaggo/files); to add:
    #10 0.672       go get github.com/swaggo/[email protected]
    #10 0.672 /go/pkg/mod/github.com/gofiber/fiber/[email protected]/internal/isatty/isatty_tcgets.go:6:8: missing go.sum entry for module providing package golang.org/x/sys/unix (imported by github.com/gofiber/fiber/v2/internal/isatty); to add:
    #10 0.672       go get github.com/gofiber/fiber/v2/internal/[email protected]
    #10 0.672 /go/pkg/mod/github.com/go-playground/validator/[email protected]/baked_in.go:21:2: missing go.sum entry for module providing package golang.org/x/text/language (imported by github.com/go-playground/validator/v10); to add:
    #10 0.672       go get github.com/go-playground/validator/[email protected]
    #10 0.672 /go/pkg/mod/github.com/jackc/[email protected]/auth_scram.go:27:2: missing go.sum entry for module providing package golang.org/x/text/secure/precis (imported by github.com/jackc/pgconn); to add:
    #10 0.672       go get github.com/jackc/[email protected]
    #10 0.672 /go/pkg/mod/github.com/swaggo/[email protected]/operation.go:18:2: missing go.sum entry for module providing package golang.org/x/tools/go/loader (imported by github.com/swaggo/swag); to add:
    #10 0.672       go get github.com/swaggo/[email protected]
    ------
    executor failed running [/bin/sh -c go build -ldflags="-s -w" -o apiserver .]: exit code: 1
    make: *** [Makefile:40: docker.fiber.build] Error 1
    
    
The purpose of this project is to learn about go-swagger.

learn-go-swagger The purpose of this project is to learn about go-swagger. Requirements Go go-swagger Setup Run ./scripts/gen-swagger to generate swag

Nov 20, 2021
This project is a GO Restful API service with Gin framework and Gorm SQLite with authorization

GO Restful API service with Gin framework and Gorm SQLite Template Structure Gin is a web framework written in Go (Golang). It features a martini-like

Oct 24, 2022
Book Catalogue, Order RESTful API

Book Catalogue, Order RESTful API try on heroku: https://pacific-island-57943.herokuapp.com Note: '/' endpoint redirect to github repository for docum

Dec 13, 2021
📁 Examples for 🚀 Fiber - Express inspired web framework written in Go

?? Examples for ?? Fiber - Express inspired web framework written in Go

Dec 29, 2022
Exercise project written in Go that I did on my own during the course "gRPC [Golang] Master Class: Build Modern API & Microservices" taught by Stephane Maarek on Udemy

calculator Exercise project written in Go that I did on my own during the course "gRPC [Golang] Master Class: Build Modern API & Microservices" taught

Nov 9, 2022
A course to build distributed key-value service based on TiKV model
A course to build distributed key-value service based on TiKV model

The TinyKV Course This is a series of projects on a key-value storage system built with the Raft consensus algorithm

Jan 7, 2023
An open source programming language that makes it easy to build simple
An open source programming language that makes it easy to build simple

The Go Programming Language Go is an open source programming language that makes it easy to build simple, reliable, and efficient software. Gopher ima

Oct 15, 2021
Comparison of Pixel and Ebiten API on the trees tutorial

Rewriting the trees tutorial of Pixel with Ebiten for API comparison I tried Pixel and really liked the clean API but the dev seems to be on pause sin

Dec 7, 2021
Simple boilerplate code to get started with building and deploying a serverless CRUD API

Simple boilerplate code to get started with building and deploying a serverless CRUD API with Go, MongoDB and Netlify

Jan 20, 2022
🚀✨🚀 REST API with Golang

REST API Statistics This repository is a completed test task for the Backend trainee position. Task text Technologies: Golang 1.17.1 Web framework - g

Dec 10, 2021
A Udemy course on how to create an industry standard REST API

go-rest-industry-standard This repository is for a Udemy course on how to create an industry standard REST API. It applies the MVC pattern, routing, a

Nov 23, 2021
Api Rest for Blog post

blog-post-api-rest Api Rest for Blog post To launch the application with it's dependencies run: sudo docker-compose up -d To stop the application run

Nov 24, 2021
Tool (in Go!) to compare and diff container and host environments. Dinosaur fun!

Compe compare environments and other things between containers, and host ??️ This is a simple tool to compare environments and other features of conta

Sep 24, 2022
Go Cheat Sheet - An overview of Go syntax and features.

Go Cheat Sheet - An overview of Go syntax and features.

Dec 31, 2022
An online book focusing on Go syntax/semantics and runtime related things

Go 101 is a book focusing on Go syntax/semantics and all kinds of runtime related things. It tries to help gophers gain a deep and thorough understanding of Go. This book also collects many details of Go and in Go programming. The book is expected to be helpful for both beginner and experienced Go programmers.

Dec 29, 2022
This slide deck and supporting material is part of the Introduction to Go training course by Dave Cheney

This slide deck and supporting material is part of the Introduction to Go training course by Dave Cheney.

Nov 14, 2022
Assert your Go code is inlined and bounds-check eliminated

gcassert gcassert is a program for making assertions about compiler decisions in Golang programs, via inline comment directives like //gcassert:inline

Oct 27, 2022
1000+ Hand-Crafted Go Examples, Exercises, and Quizzes

A Huge Number of Go Examples, Exercises and Quizzes Best way of learning is doing. Inside this repository, you will find thousands of Go examples, exe

Jan 1, 2023
Official provider for VMware desktop products: Fusion, Player, and Workstation.

Vagrant VMware Desktop Providers This is the common codebase for the official providers for VMware desktop products: Fusion, Player, and Workstation.

Jan 7, 2023