Categorygithub.com/jicodes/go-fullstack-app
repository
0.0.0-20240205024726-31841bf2d5c8
Repository: https://github.com/jicodes/go-fullstack-app.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# README

Rest API with Go, Fiber, Docker and Postgre

Dev environment

Create Dockerfile and docker-compose.yaml

run docker-compose up while docker is running

When working with docker we need to run all the commands in the container bash, run the following command to get into the container bash

docker compose run --service-ports web bash

in the Go container shell, initialize a new Go module in your project using go mod init. This will create a go.mod file that will be used to install and manage dependencies

go mod init github.com/JiCodes/go-fullstack-app

then install the fiber package

go get github.com/gofiber/fiber/v2

create a dir called cmd to hold main.go file

mkdir cmd
touch cmd/main.go

copy the following code into main.go

package main

import "github.com/gofiber/fiber/v2"

func main() {
    app := fiber.New()

    app.Get("/", func(c *fiber.Ctx) error {
        return c.SendString("Hello, Go Rest API!")
    })

    app.Listen(":3000")
}

run the app with binding on localhost

go run cmd/main.go -b 0.0.0.0

check the app is running by visiting http://localhost:3000

modify the Dockerfile


COPY . . // copy all the files from host to the container

RUN go install github.com/cosmtrek/air@latest // install air to watch the changes in the code and restart the app automatically

RUN go mod tidy // to install all the dependencies

** create a new file called .air.toml ** copy the confif from air github repo air example config file https://github.com/cosmtrek/air/blob/master/air_example.toml

with only one change

cmd = "go build -o ./tmp/main ./cmd"

modify the docker-compose.yaml

...
  command: air cmd/main.go -b 0.0.0.0 

allow us to launch the app with docker compose up command from the host machine (now we don't need to get into the container bash to run the app)

rebuild the image

docker compose build

then run the app

docker compose up

update docker-compose yaml file with postgres and add .env file for it

...
  db:
    image: postgres:alpine
    environment:
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASSWORD}
      - POSTGRES_DB=${DB_NAME}
    ports:
      - 5432:5432
    volumes:
      - postgres-db:/var/lib/postgresql/data
  
  volumes:
    postgres-db:

enter the docker shell

docker compose run --service-ports web bash

then run the following command to install the gorm ORM package and postgres driver

go get gorm.io/gorm
go get gorm.io/driver/postgres

create the models dir and models.go file

mkdir models
touch models/models.go

create the database directory and database.go file

mkdir database
touch database/database.go

Routes and Endpoints

touch cmd/routes.go # to hold all the routes
mkdir handlers
touch handlers/handlers.go # to hold all the handlers for the routes

then add some routes and handlers to the files

enable templates

go get github.com/gofiber/template/html/v2

mdkir views

touch views/index.html
docker compose up

create a layout file in layouts dir

mkdir layouts
touch layouts/main.html

Add several views and partials to the layout's main.html file

Add CRUD operations to the app

we use JS fetch API to send http requests to the server (patch and delete)

  1. Remove confirmation page form the handler and redirect to the facts list page

  2. Add ShowFact handler to show a single fact and corresponding route and view

  3. Add js to enable http patch method to update data

  4. Add DeleteFact handler to delete a fact and corresponding route and view