Giter Site home page Giter Site logo

koddr / tutorial-go-fiber-rest-api Goto Github PK

View Code? Open in Web Editor NEW
341.0 4.0 72.0 274 KB

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

Home Page: https://dev.to/koddr/build-a-restful-api-on-go-fiber-postgresql-jwt-and-swagger-docs-in-isolated-docker-containers-475j

License: MIT License

Dockerfile 2.05% Makefile 4.87% Go 93.08%
tutorial tutorials go golang api rest-api fiber fiber-framework jwt swagger

tutorial-go-fiber-rest-api's Introduction

📖 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

P.S.

If you want more articles like this on this blog, then post a comment below and subscribe to me. Thanks! 😘

And, of course, you can support me by donating at LiberaPay. Each donation will be used to write new articles and develop non-profit open-source projects for the community.

Support author at LiberaPay

⚠️ License

MIT © Vic Shóstak & True web artisans.

Building:

https://levelup.gitconnected.com/a-better-way-than-ldflags-to-add-a-build-version-to-your-go-binaries-2258ce419d2d

Windows:

$ set CGO_ENABLED=0 && go build -ldflags="-w -s -X 'main.version=1.0.1'" .

tutorial-go-fiber-rest-api's People

Contributors

akissa avatar dependabot[bot] avatar kamran151199 avatar koddr avatar lospejos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

tutorial-go-fiber-rest-api's Issues

// @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

Required check list:

  • I'm gonna mark the checkboxes like this.
  • I didn't find in the repository's issues section similar bug.
  • 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): 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

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

Required check list:

  • I'm gonna mark the checkboxes like this.
  • I didn't find in the repository's issues section similar bug.
  • 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

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

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

Required check list:

  • I'm gonna mark the checkboxes like this.
  • I didn't find in the repository's issues section similar bug.
  • 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.