postgres init started

This commit is contained in:
lukas 2024-01-10 13:59:15 +01:00
parent 3c7c56c7ae
commit b500d8acbb
6 changed files with 77 additions and 11 deletions

View File

@ -1,9 +0,0 @@
package db
import "git.cmdq.io/meirjann/DataLogue-Backend/log"
var logger = log.New("db")
func Init() {
logger.Println("init db...")
}

67
db/pq/init.go Normal file
View File

@ -0,0 +1,67 @@
package pq
import (
"database/sql"
"errors"
"fmt"
"git.cmdq.io/meirjann/DataLogue-Backend/log"
_ "github.com/lib/pq"
)
type pqClient struct {
c *sql.DB
}
var (
logger = log.New("db")
db = &pqClient{}
)
const (
DB_HOST = "localhost"
DB_NAME = "dataloguewdawd"
DB_PASS = "datalogueawdawd"
DB_USER = "datalogueawdawd"
)
func Init() error {
c, err := initConn()
if err != nil {
return err
}
db.c = c
err = checkOrCreateTables()
if err != nil {
return err
}
return nil
}
func initConn() (*sql.DB, error) {
connStr := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=verify-full", DB_USER, DB_PASS, DB_HOST, DB_NAME)
db, err := sql.Open("postgres", connStr)
if err != nil {
return nil, err
}
logger.Println("connection to postgres db established")
return db, nil
}
func CloseConn() error {
if db.c != nil {
err := db.c.Close()
if err != nil {
return err
}
logger.Println("connection to postgres db closed")
return nil
}
return errors.New("postgres connection nil, could not close")
}
func checkOrCreateTables() error {
return nil
}

0
db/redis/init.go Normal file
View File

2
go.mod
View File

@ -1,3 +1,5 @@
module git.cmdq.io/meirjann/DataLogue-Backend module git.cmdq.io/meirjann/DataLogue-Backend
go 1.21.4 go 1.21.4
require github.com/lib/pq v1.10.9

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=

View File

@ -3,7 +3,7 @@ package main
import ( import (
"git.cmdq.io/meirjann/DataLogue-Backend/api" "git.cmdq.io/meirjann/DataLogue-Backend/api"
"git.cmdq.io/meirjann/DataLogue-Backend/configs" "git.cmdq.io/meirjann/DataLogue-Backend/configs"
"git.cmdq.io/meirjann/DataLogue-Backend/db" "git.cmdq.io/meirjann/DataLogue-Backend/db/pq"
"git.cmdq.io/meirjann/DataLogue-Backend/log" "git.cmdq.io/meirjann/DataLogue-Backend/log"
) )
@ -14,7 +14,11 @@ func main() {
// set all configs // set all configs
configs.Init() configs.Init()
// init db // init db
db.Init() err := pq.Init()
if err != nil {
panic(err)
}
defer pq.CloseConn()
// start api // start api
api.Serve() api.Serve()
} }