Skip to content

Commit

Permalink
Merge pull request #29 from iquzart/hexagonal_structure
Browse files Browse the repository at this point in the history
Restructure
  • Loading branch information
iquzart authored Jul 24, 2023
2 parents 93caaa1 + 286168c commit af3c0b5
Show file tree
Hide file tree
Showing 1,218 changed files with 399,480 additions and 53,711 deletions.
18 changes: 8 additions & 10 deletions Containerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Start from the latest golang base image
FROM golang:1.16-alpine as build-env
FROM golang:1.20-alpine as build-env

# Set the Current Working Directory inside the container
WORKDIR /src
Expand All @@ -14,7 +14,7 @@ RUN go mod download
COPY . .

# Build the Go app
RUN go build -o app .
RUN go build -o app cmd/app/main.go

# Final stage
FROM alpine:latest
Expand All @@ -25,14 +25,12 @@ RUN apk update && rm -rf /var/cache/apk/*
# Maintainer Info
LABEL maintainer="Muhammed Iqbal <iquzart@hotmail.com>"

# Set GIN Mode as Release
ENV GIN_MODE=release

# Set environment variable default value
ENV BANNER="Hello from Go App"

# Container PORT
ENV PORT="8080"
# Set Environement variables
ENV \
PORT="8080" \
SERVICE_NAME="go-app" \
BANNER="Hello from Go App" \
GIN_MODE="release"

# Create non-root account to run the container
RUN adduser go -h /app -u 1000 -D
Expand Down
40 changes: 40 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.DEFAULT_GOAL := help
.PHONY: help clean build run docker-build docker-run docker-stop

# Define variables
BINARY_NAME := go-app
DOCKER_IMAGE := make-go-app
DOCKER_CONTAINER := make-go-app
MAIN_FILE := cmd/app/main.go
LDFLAGS := -ldflags="-s -w"

help: ## Display this help message
@echo "Usage: make <command>"
@echo ""
@echo "Commands:"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

clean: ## Remove binary file and container resources
rm -f $(BINARY_NAME)
docker stop $(DOCKER_CONTAINER) || true
docker rm $(DOCKER_CONTAINER) || true
docker rmi $(DOCKER_IMAGE) || true

build: ## Build the binary file
go build $(LDFLAGS) -o $(BINARY_NAME) $(MAIN_FILE)

run: ## Build and run the binary file
go run $(LDFLAGS) $(MAIN_FILE)

docker-build: ## Build Docker image
docker build -t $(DOCKER_IMAGE) . -f ./Containerfile

docker-run: ## Run Docker container
@if ! docker image inspect $(DOCKER_IMAGE) >/dev/null 2>&1; then \
make docker-build; \
fi
docker run -d -p 8080:8080 --name $(DOCKER_CONTAINER) $(DOCKER_IMAGE)


docker-stop: ## Stop Docker container
docker stop $(DOCKER_CONTAINER)
8 changes: 8 additions & 0 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import "go-app/pkg/server"

func main() {
// Start the server
server.Run()
}
39 changes: 39 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package config

import (
"fmt"
"os"
"strconv"
)

// Server contains the HTTP server configurations.
type ServerConfigs struct {
Port string
GracefulShutdown bool
ServiceName string
}

// NewServer creates a new Server instance with default values.
func GetServerConfigs() *ServerConfigs {
return &ServerConfigs{
Port: fmt.Sprintf(":%s", getEnvOrDefault("PORT", "8080")),
GracefulShutdown: getEnvOrDefaultBool("ENABLE_GRACEFUL_SHUTDOWN", true),
ServiceName: getEnvOrDefault("SERVICE_NAME", "go-app"),
}
}

func getEnvOrDefault(key, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}

func getEnvOrDefaultBool(key string, defaultValue bool) bool {
value, err := strconv.ParseBool(os.Getenv(key))
if err != nil {
return defaultValue
}
return value
}
24 changes: 0 additions & 24 deletions controllers/api_test.go

This file was deleted.

66 changes: 0 additions & 66 deletions controllers/app.go

This file was deleted.

76 changes: 0 additions & 76 deletions controllers/app_test.go

This file was deleted.

39 changes: 0 additions & 39 deletions controllers/common_test.go

This file was deleted.

24 changes: 0 additions & 24 deletions controllers/status_test.go

This file was deleted.

Loading

0 comments on commit af3c0b5

Please sign in to comment.