0

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?
Welsh
  • 39
  • 1
  • 12

1 Answers1

0

Check the below workaround to Connect to MySQL database via Gorm

Create main.go file and add the below code:

package main
import (
 "encoding/json"
 "fmt"
 "log"
 "net/http"
 "gorm.io/driver/mysql"
 "gorm.io/gorm"
)
// Database Connection
var  dsn =
", GoDatabaseCreate) log.Fatal"siliconuser:UserName@Password@tcp(siliconmysql.mysql.database.azure.com:3306)/go_test_models?charset=utf8mb4"
var  db, _ = gorm.Open(mysql.Open(dsn), &gorm.Config{})

type  GoTestModel  struct {
Name string
Year string
}
func  main() {
 http.HandleFunc("/createstuff (http.ListenAndServe(":8080", nil))
}
func  GoDatabaseCreate(w http.ResponseWriter, r *http.Request) {
GoTestModel := GoTestModel{
Name: "john",
Year: "2022"}
 db.Create(&GoTestModel)
 if  err := db.Create(&GoTestModel).Error; err != nil {
 log.Fatalln(err)
    }
 json.NewEncoder(w).Encode(GoTestModel)
 fmt.Println("Feilds Added", GoTestModel)
}

In Azure portal, run the below commands in Azure cloud shell (Bash)

$ mysql -h siliconmysql.mysql.database.azure.com -u siliconuser -p

Enter password: Your Pwd

create database go_test_models;
use go_test_models
create table go_test_models(Name VARCHAR(100), Year VARCHAR(100));

Run code in VS code and connect to database in Azure

Right click on the main.go file and select Open in Integrated Terminal. Run go mod init gotest command to install and initialize all the modules.

enter image description here

  • go.mod file will be added. enter image description here

  • To get our modules, run go get gorm.io/driver/mysql command enter image description here

  • Run go get gorm.io/gorm command

  • Run our code locally by using Run- go run main.go command.

  • Deploy app to our Azure app service. enter image description here

enter image description here

"The specified CGI application encountered an error and the server terminated the process."

Configure Custom Auto heal to know the more details about the issue

In Azure Portal => Navigate to your Azure App Service => Diagnose and solve problems => Select Diagnostic Tools => Auto Heal => Custom Auto-Heal Rules enter image description here

enter image description here

References taken from GORM

Harshitha
  • 3,784
  • 2
  • 4
  • 9