refactor: always ensure the db connection closes by using a defer

This commit is contained in:
Derrick Hammer 2023-08-05 17:17:26 -04:00
parent f11b285d4e
commit 18e102cc8a
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 27 additions and 7 deletions

34
main.go
View File

@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"embed" "embed"
"fmt"
"git.lumeweb.com/LumeWeb/portal/config" "git.lumeweb.com/LumeWeb/portal/config"
"git.lumeweb.com/LumeWeb/portal/controller" "git.lumeweb.com/LumeWeb/portal/controller"
"git.lumeweb.com/LumeWeb/portal/db" "git.lumeweb.com/LumeWeb/portal/db"
@ -13,15 +14,18 @@ import (
"git.lumeweb.com/LumeWeb/portal/service/files" "git.lumeweb.com/LumeWeb/portal/service/files"
"git.lumeweb.com/LumeWeb/portal/shared" "git.lumeweb.com/LumeWeb/portal/shared"
"git.lumeweb.com/LumeWeb/portal/tus" "git.lumeweb.com/LumeWeb/portal/tus"
nriris "github.com/iris-contrib/middleware/newrelic"
"github.com/iris-contrib/swagger" "github.com/iris-contrib/swagger"
"github.com/iris-contrib/swagger/swaggerFiles" "github.com/iris-contrib/swagger/swaggerFiles"
"github.com/kataras/iris/v12" "github.com/kataras/iris/v12"
irisContext "github.com/kataras/iris/v12/context" irisContext "github.com/kataras/iris/v12/context"
"github.com/kataras/iris/v12/middleware/cors" "github.com/kataras/iris/v12/middleware/cors"
"github.com/kataras/iris/v12/mvc" "github.com/kataras/iris/v12/mvc"
"github.com/spf13/viper"
"go.uber.org/zap" "go.uber.org/zap"
"log" "log"
"net/http" "net/http"
"os"
) )
// Embed a directory of static files for serving from the app's root path // Embed a directory of static files for serving from the app's root path
@ -48,6 +52,13 @@ func main() {
// Initialize the database connection // Initialize the database connection
db.Init() db.Init()
defer func() {
err := db.Close()
if err != nil {
logger.Get().Error("Failed to close db connection", zap.Error(err))
}
}()
logger.Init() logger.Init()
files.Init() files.Init()
auth.Init() auth.Init()
@ -66,6 +77,21 @@ func main() {
tusHandler := tus.Init() tusHandler := tus.Init()
if viper.IsSet("newrelic.license") {
nrAapp, err := newrelic.NewApplication(
newrelic.ConfigAppName(viper.GetString("newrelic.appname")),
newrelic.ConfigLicense(viper.GetString("newrelic.license")),
newrelic.ConfigAppLogForwardingEnabled(true),
)
if nil != err {
fmt.Println(err)
os.Exit(1)
}
app.Use(nriris.New(nrAapp))
}
// Register the AccountController with the MVC framework and attach it to the "/api/account" path // Register the AccountController with the MVC framework and attach it to the "/api/account" path
mvc.Configure(v1.Party("/account"), func(app *mvc.Application) { mvc.Configure(v1.Party("/account"), func(app *mvc.Application) {
app.Handle(new(controller.AccountController)) app.Handle(new(controller.AccountController))
@ -116,12 +142,6 @@ func main() {
}) })
if err != nil { if err != nil {
logger.Get().Error("Failed starting webserver proof", zap.Error(err)) logger.Get().Error("Failed starting webserver", zap.Error(err))
}
err = db.Close()
if err != nil {
logger.Get().Error("Failed to close db connection", zap.Error(err))
} }
} }