Skip to content

Commit

Permalink
feat: add azure support and workflow
Browse files Browse the repository at this point in the history
feat: add azure support and workflow
  • Loading branch information
HyggeHalcyon authored Jan 8, 2024
2 parents 8929afd + 8089074 commit 03bbf38
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ DB_NAME = <your database name>
DB_PORT = 5432

NGINX_PORT=8080
GOLANG_PORT=8888
HTTP_PLATFORM_PORT=8888

SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/azure_dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Build Go app and push to Azure (Development)

on:
push:
branches: [ main ]

jobs:

build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18

- name: Build
run: go build -o build/go-app.exe -v

- name: Deploy to Azure
uses: Azure/webapps-deploy@v2
with:
# Name of the Azure Web App
app-name: tedxits-dev
# Applies to Web App only: Path to package or folder. *.zip, *.war, *.jar or a folder to deploy
package: build/
publish-profile: ${{ secrets.AZUREWEBAPPPUBLISHPROFILEDEVELOPMENT }}
10 changes: 10 additions & 0 deletions build/web.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpplatformhandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
</handlers>
<httpPlatform processPath="D:\home\site\wwwroot\go-app.exe" startupTimeLimit="60">
</httpPlatform>
</system.webServer>
</configuration>
4 changes: 2 additions & 2 deletions constants/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const (
ENUM_ROLE_ADMIN = "admin"
ENUM_ROLE_USER = "user"

ENUM_RUN_PRODUCTION = "production"
ENUM_RUN_TESTING = "testing"
ENUM_RUN_PRODUCTION = "production"
ENUM_RUN_DEVELOPMENT = "development"

ENUM_PAGINATION_LIMIT = 10
ENUM_PAGINATION_PAGE = 1
Expand Down
43 changes: 6 additions & 37 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,10 @@
version: '3.9'

services:
postgres:
hostname: postgres
image: postgres:latest
ports:
- ${DB_PORT}:5432
volumes:
- ./volumes/postgres:/var/lib/postgresql/data
container_name: tedxits2024
image: postgres
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASS}
- POSTGRES_DB=${DB_NAME}
networks:
- app-network

app:
hostname: app
container_name: golang-clean-template
build:
context: .
dockerfile: Dockerfile
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
- POSTGRES_DB=tedxits2024
ports:
- ${GOLANG_PORT}:${GOLANG_PORT}
restart: always
volumes:
- ./:/app
depends_on:
- postgres
env_file:
- .env
networks:
- app-network

volumes:
app_vol:

networks:
app-network:
driver: bridge
- 5432:5432
14 changes: 13 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/TEDxITS/website-backend-2024/repository"
"github.com/TEDxITS/website-backend-2024/routes"
"github.com/TEDxITS/website-backend-2024/service"
"github.com/TEDxITS/website-backend-2024/utils/azure"

"github.com/gin-gonic/gin"
"gorm.io/gorm"
Expand Down Expand Up @@ -45,7 +46,7 @@ func main() {
log.Fatalf("error migration seeder: %v", err)
}

port := os.Getenv("GOLANG_PORT")
port := os.Getenv("HTTP_PLATFORM_PORT")
if port == "" {
port = "8888"
}
Expand All @@ -54,4 +55,15 @@ func main() {
if err := server.Run(":" + port); err != nil {
log.Fatalf("error running server: %v", err)
}

/*
Deployed on Azure App Service with .NET Stack.
The workflow will failed to deploy on updates
because the server is already running and it
wont lets us replace it. Normally in .NET apps
on azure, it will create a file called "app_offline.htm"
and the ASP .NET will notice it the file is created
and stop the application. This replicate said behavior.
*/
azure.StopOnNewDeployment()
}
39 changes: 39 additions & 0 deletions utils/azure/azure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package azure

import (
"fmt"
"os"
"strings"

"github.com/fsnotify/fsnotify"
)

func StopOnNewDeployment() {
// creates a new file watcher for App_offline.htm
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Println("ERROR", err)
}
defer watcher.Close()

// watch for App_offline.htm and exit the program if present
// This allows continuous deployment on App Service as the .exe will not be
// terminated otherwise
go func() {
for {
select {
case event := <-watcher.Events:
if strings.HasSuffix(event.Name, "app_offline.htm") {
fmt.Println("Exiting due to app_offline.htm being present")
os.Exit(0)
}
}
}
}()

// get the current working directory and watch it
currentDir, err := os.Getwd()
if err := watcher.Add(currentDir); err != nil {
fmt.Println("ERROR", err)
}
}

0 comments on commit 03bbf38

Please sign in to comment.