I have a basic Go API I created that works and connects locally. However, when I try to connect it from an Azure Web App I get the error "The specified CGI application encountered an error and the server terminated the process."
I am using Gorm
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"gopkg.in/fsnotify.v1"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var DB *gorm.DB
The database connection code is as follows:
DbDriver := os.Getenv("DB_DRIVER")
DbPort := os.Getenv("DB_PORT")
DbHost := os.Getenv("DB_HOST")
DbUser := os.Getenv("DB_USER")
DbPassword := os.Getenv("DB_PASSWORD")
DbName := os.Getenv("DB_NAME")
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", DbUser, DbPassword, DbHost, DbPort, DbName)
DB, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})```
Tried even connecting to an AWS database and encountered the same error. What could be missing on this?
Check the below workaround to Connect to MySQL database via Gorm
Create
main.gofile and add the below code:In Azure portal, run the below commands in Azure cloud shell (Bash)
Run code in VS code and connect to database in Azure
Right click on the
main.gofile and selectOpen in Integrated Terminal. Rungo mod init gotestcommand to install and initialize all the modules.go.modfile will be added.To get our modules, run
go get gorm.io/driver/mysqlcommandRun
go get gorm.io/gormcommandRun our code locally by using
Run- go run main.gocommand.Deploy app to our Azure app service.
Configure Custom Auto heal to know the more details about the issue
In
Azure Portal=> Navigate to yourAzure App Service=>Diagnose and solve problems=> SelectDiagnostic Tools=>Auto Heal=>Custom Auto-Heal RulesReferences taken from GORM