Datenbank Init hinzugefügt
This commit is contained in:
@@ -20,6 +20,7 @@ require (
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/lib/pq v1.12.3 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
|
||||
@@ -35,6 +35,8 @@ github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzh
|
||||
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/lib/pq v1.12.3 h1:tTWxr2YLKwIvK90ZXEw8GP7UFHtcbTtty8zsI+YjrfQ=
|
||||
github.com/lib/pq v1.12.3/go.mod h1:/p+8NSbOcwzAEI7wiMXFlgydTwcgTr3OSKMsD2BitpA=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
_ "github.com/lib/pq"
|
||||
"scpq-editor-server/config"
|
||||
"scpq-editor-server/server"
|
||||
)
|
||||
@@ -9,11 +10,11 @@ import (
|
||||
func main() {
|
||||
log.Println("Starting Simply CPQ Editor Service")
|
||||
log.Println("Reading configuration")
|
||||
c := config.InitConfig("config")
|
||||
config := config.InitConfig("config")
|
||||
log.Println("Initializing database")
|
||||
//dbHandler := server.InitDatabase(config)
|
||||
dbHandler := server.InitDatabase(config)
|
||||
log.Println("Initializing HTTP server")
|
||||
httpServer := server.InitHttpServer(c)
|
||||
httpServer := server.InitHttpServer(config, dbHandler)
|
||||
httpServer.Start()
|
||||
|
||||
log.Println("Service Stopped")
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"log"
|
||||
"database/sql"
|
||||
"github.com/spf13/viper"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
func InitDatabase(config *viper.Viper) *sql.DB {
|
||||
connectionString := config.GetString("database.connection_string")
|
||||
driverName := config.GetString("database.driver_name")
|
||||
if connectionString == "" {
|
||||
log.Fatalf("Database connection string is missing")
|
||||
}
|
||||
dbHandler, err := sql.Open(driverName, connectionString)
|
||||
if err != nil {
|
||||
log.Fatalf("Error while initializing database: %v", err)
|
||||
}
|
||||
err = dbHandler.Ping()
|
||||
if err != nil {
|
||||
dbHandler.Close()
|
||||
log.Fatalf("Error while validating database: %v", err)
|
||||
}
|
||||
return dbHandler
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package server
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"database/sql"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
@@ -11,15 +11,17 @@ import (
|
||||
type HttpServer struct {
|
||||
config *viper.Viper
|
||||
router *gin.Engine
|
||||
dbHandler *sql.DB
|
||||
}
|
||||
|
||||
func InitHttpServer(config *viper.Viper) HttpServer {
|
||||
func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
|
||||
// Service API
|
||||
router := gin.Default()
|
||||
router.GET("/test", Test)
|
||||
return HttpServer{
|
||||
config: config,
|
||||
router: router,
|
||||
dbHandler: dbHandler,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user