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/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
|
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
|
||||||
github.com/leodido/go-urn v1.4.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/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // 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/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 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
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 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
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=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
_ "github.com/lib/pq"
|
||||||
"scpq-editor-server/config"
|
"scpq-editor-server/config"
|
||||||
"scpq-editor-server/server"
|
"scpq-editor-server/server"
|
||||||
)
|
)
|
||||||
@@ -9,11 +10,11 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
log.Println("Starting Simply CPQ Editor Service")
|
log.Println("Starting Simply CPQ Editor Service")
|
||||||
log.Println("Reading configuration")
|
log.Println("Reading configuration")
|
||||||
c := config.InitConfig("config")
|
config := config.InitConfig("config")
|
||||||
log.Println("Initializing database")
|
log.Println("Initializing database")
|
||||||
//dbHandler := server.InitDatabase(config)
|
dbHandler := server.InitDatabase(config)
|
||||||
log.Println("Initializing HTTP server")
|
log.Println("Initializing HTTP server")
|
||||||
httpServer := server.InitHttpServer(c)
|
httpServer := server.InitHttpServer(config, dbHandler)
|
||||||
httpServer.Start()
|
httpServer.Start()
|
||||||
|
|
||||||
log.Println("Service Stopped")
|
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 (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"database/sql"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
@@ -11,15 +11,17 @@ import (
|
|||||||
type HttpServer struct {
|
type HttpServer struct {
|
||||||
config *viper.Viper
|
config *viper.Viper
|
||||||
router *gin.Engine
|
router *gin.Engine
|
||||||
|
dbHandler *sql.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitHttpServer(config *viper.Viper) HttpServer {
|
func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
|
||||||
// Service API
|
// Service API
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
router.GET("/test", Test)
|
router.GET("/test", Test)
|
||||||
return HttpServer{
|
return HttpServer{
|
||||||
config: config,
|
config: config,
|
||||||
router: router,
|
router: router,
|
||||||
|
dbHandler: dbHandler,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user