postgres init started
This commit is contained in:
parent
3c7c56c7ae
commit
b500d8acbb
@ -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
67
db/pq/init.go
Normal 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
0
db/redis/init.go
Normal file
2
go.mod
2
go.mod
@ -1,3 +1,5 @@
|
||||
module git.cmdq.io/meirjann/DataLogue-Backend
|
||||
|
||||
go 1.21.4
|
||||
|
||||
require github.com/lib/pq v1.10.9
|
||||
|
2
go.sum
Normal file
2
go.sum
Normal 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=
|
8
main.go
8
main.go
@ -3,7 +3,7 @@ package main
|
||||
import (
|
||||
"git.cmdq.io/meirjann/DataLogue-Backend/api"
|
||||
"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"
|
||||
)
|
||||
|
||||
@ -14,7 +14,11 @@ func main() {
|
||||
// set all configs
|
||||
configs.Init()
|
||||
// init db
|
||||
db.Init()
|
||||
err := pq.Init()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer pq.CloseConn()
|
||||
// start api
|
||||
api.Serve()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user