Skip to content

learning-cloud-native-go/myapp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

94 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

buymeacoffee

Learning Cloud Native Go - myapp

🌱 Cloud Native Application Development is one way of speeding up the building of web applications using microservices, containers, and orchestration tools.

This repository shows how to build a Dockerized RESTful API application in Go for a simple bookshelf.

πŸ”‹ Batteries included

  • The idiomatic structure based on the resource-oriented design.
  • The usage of Docker, Docker compose, Alpine images, and linters on development.
  • Healthcheck and CRUD API implementations with OpenAPI specifications.
  • The usage of Goose for the database migrations and GORM as the database ORM.
  • The usage of Zerolog as the centralized Syslog logger.
  • The usage of Validator.v10 as the form validator.
  • The usage of GitHub actions to run tests and linters, generate OpenAPI specifications, and build and push production images to the Docker registry.

πŸš€ Endpoints

Name HTTP Method Route
Health GET /livez
List Books GET /v1/books
Create Book POST /v1/books
Read Book GET /v1/books/{id}
Update Book PUT /v1/books/{id}
Delete Book DELETE /v1/books/{id}

πŸ’‘ swaggo/swag : swag init -g cmd/api/main.go -o .swagger -ot yaml

πŸ—„οΈ Database design

Column Name Datatype Not Null Primary Key
id UUID βœ… βœ…
title TEXT βœ…
author TEXT βœ…
published_date DATE βœ…
image_url TEXT
description TEXT
created_at TIMESTAMP βœ…
updated_at TIMESTAMP βœ…
deleted_at TIMESTAMP

πŸ“¦ Container image sizes

  • DB: 241MB
  • API
    • Development environment: 655MB
    • Production environment: 28MB ; πŸ’‘docker build -f prod.Dockerfile . -t myapp_app

πŸ“ Project structure

myapp
β”œβ”€β”€ cmd
β”‚  β”œβ”€β”€ api
β”‚  β”‚  └── main.go
β”‚  └── migrate
β”‚     └── main.go
β”‚
β”œβ”€β”€ api
β”‚  β”œβ”€β”€ resource
β”‚  β”‚  β”œβ”€β”€ book
β”‚  β”‚  β”‚  β”œβ”€β”€ handler.go
β”‚  β”‚  β”‚  β”œβ”€β”€ model.go
β”‚  β”‚  β”‚  β”œβ”€β”€ repository.go
β”‚  β”‚  β”‚  └── repository_test.go
β”‚  β”‚  β”œβ”€β”€ common
β”‚  β”‚  β”‚  └── err
β”‚  β”‚  β”‚     └── err.go
β”‚  β”‚  └── health
β”‚  β”‚     └── handler.go
β”‚  β”‚
β”‚  └── router
β”‚     β”œβ”€β”€ middleware
β”‚     β”‚  β”œβ”€β”€ request_id.go
β”‚     β”‚  β”œβ”€β”€ request_id_test.go
β”‚     β”‚  β”œβ”€β”€ requestlog
β”‚     β”‚  β”‚  β”œβ”€β”€ handler.go
β”‚     β”‚  β”‚  └── log_entry.go
β”‚     β”‚  β”œβ”€β”€ content_type.go
β”‚     β”‚  └── content_type_test.go
β”‚     └── router.go
β”‚
β”œβ”€β”€ migrations
β”‚  └── 00001_create_books_table.sql
β”‚
β”œβ”€β”€ config
β”‚  └── config.go
β”‚
β”œβ”€β”€ util
β”‚  β”œβ”€β”€ logger
β”‚  β”‚  └── logger.go
β”‚  └── validator
β”‚     └── validator.go
β”‚
β”œβ”€β”€ .env
β”‚
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
β”‚
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ Dockerfile
β”‚
β”œβ”€β”€ prod.Dockerfile
└── k8s
   β”œβ”€β”€ app-configmap.yaml
   β”œβ”€β”€ app-secret.yaml
   β”œβ”€β”€ app-deployment.yaml
   └── app-service.yaml

πŸ“Έ Form validations and logs

Form validation

Logs in app init Logs in crud