Changes to the configuration. Will help with setting up DB with sockets
This commit is contained in:
+4
-4
@@ -4,7 +4,7 @@ postgres:
|
|||||||
user: expdbuser
|
user: expdbuser
|
||||||
pass: thepass
|
pass: thepass
|
||||||
db: thedb
|
db: thedb
|
||||||
host: 127.0.0.1
|
host: /home/esilva/Desktop/projetos/expenses/src/pgImage/pgSocket/
|
||||||
port: 5431
|
port: 5432
|
||||||
migrations_path: "file://expenses/repo/pgdb/migrations/"
|
migrationsPath: "file://expenses/repo/pgdb/migrations/"
|
||||||
mig_custom_scripts: "./expenses/repo/pgdb/migrations/custom/"
|
migCustomScripts: "./expenses/repo/pgdb/migrations/custom/"
|
||||||
|
|||||||
@@ -18,13 +18,13 @@ type PostgresConfig struct {
|
|||||||
Pass string `yaml:"pass"`
|
Pass string `yaml:"pass"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configuration defines the base configuration of the expenses app
|
// Configuration defines the base configuration of the expenses app.
|
||||||
type Configuration struct {
|
type Configuration struct {
|
||||||
AssetsDir string `yaml:"assets"`
|
AssetsDir string `yaml:"assets"`
|
||||||
Port string `yaml:"port"`
|
Port string `yaml:"port"`
|
||||||
PgCfg *PostgresConfig `yaml:"postgres"`
|
PgCfg *PostgresConfig `yaml:"postgres"`
|
||||||
MigrationsPath string `yaml:"migrations_path"`
|
MigrationsPath string `yaml:"migrationsPath"`
|
||||||
MigCustomScript string `yaml:"mig_custom_scripts"`
|
MigCustomScript string `yaml:"migCustomScripts"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
+30
-15
@@ -3,8 +3,8 @@ package expenses
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/ESilva15/expenses/config"
|
"github.com/ESilva15/expenses/config"
|
||||||
"github.com/ESilva15/expenses/expenses/repo"
|
"github.com/ESilva15/expenses/expenses/repo"
|
||||||
@@ -55,19 +55,15 @@ func startLuaVM() (*lua.LState, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getPgConnString(cfg *config.Configuration) string {
|
func getPgConnString(cfg *config.Configuration) string {
|
||||||
var s strings.Builder
|
return fmt.Sprintf("postgres://%s:%s@/%s?host=%s&port=%s&sslmode=disable",
|
||||||
|
cfg.PgCfg.User, cfg.PgCfg.Pass, cfg.PgCfg.DB, cfg.PgCfg.Host, cfg.PgCfg.Port)
|
||||||
s.WriteString("port=" + cfg.PgCfg.Port + " ")
|
|
||||||
s.WriteString("host=" + cfg.PgCfg.Host + " ")
|
|
||||||
s.WriteString("user=" + cfg.PgCfg.User + " ")
|
|
||||||
s.WriteString("dbname=" + cfg.PgCfg.DB + " ")
|
|
||||||
s.WriteString("password=" + cfg.PgCfg.Pass)
|
|
||||||
|
|
||||||
return s.String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func runMigrations(luaVM *lua.LState) error {
|
func runMigrations(luaVM *lua.LState) error {
|
||||||
migrator, err := repo.NewPgMigrator()
|
cfg := config.GetInstance()
|
||||||
|
pgStr := getPgConnString(cfg)
|
||||||
|
|
||||||
|
migrator, err := repo.NewPgMigrator(pgStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -84,7 +80,10 @@ func runMigrations(luaVM *lua.LState) error {
|
|||||||
|
|
||||||
// GoToMigration provides a way to force moving to the specified migration.
|
// GoToMigration provides a way to force moving to the specified migration.
|
||||||
func (a *ExpApp) GoToMigration(id uint) error {
|
func (a *ExpApp) GoToMigration(id uint) error {
|
||||||
migrator, err := repo.NewPgMigrator()
|
cfg := config.GetInstance()
|
||||||
|
pgStr := getPgConnString(cfg)
|
||||||
|
|
||||||
|
migrator, err := repo.NewPgMigrator(pgStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -92,12 +91,28 @@ func (a *ExpApp) GoToMigration(id uint) error {
|
|||||||
return migrator.Goto(id)
|
return migrator.Goto(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createPGXPoolConn(ctx context.Context) (*pgxpool.Pool, error) {
|
||||||
|
cfg := config.GetInstance()
|
||||||
|
pgStr := getPgConnString(cfg)
|
||||||
|
|
||||||
|
conn, err := pgxpool.New(ctx, pgStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var result int
|
||||||
|
err = conn.QueryRow(ctx, "SELECT 1").Scan(&result)
|
||||||
|
if err != nil {
|
||||||
|
panic("connection test query failed: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
|
||||||
// StartApp will configure and open the required connections to run this app.
|
// StartApp will configure and open the required connections to run this app.
|
||||||
func StartApp() error {
|
func StartApp() error {
|
||||||
config.SetConfig("./config.yaml")
|
config.SetConfig("./config.yaml")
|
||||||
cfg := config.GetInstance()
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
pgStr := getPgConnString(cfg)
|
|
||||||
|
|
||||||
// Create the LuaVM
|
// Create the LuaVM
|
||||||
luaVM, err := startLuaVM()
|
luaVM, err := startLuaVM()
|
||||||
@@ -112,7 +127,7 @@ func StartApp() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create our final app thing
|
// Create our final app thing
|
||||||
conn, err := pgxpool.New(ctx, pgStr)
|
conn, err := createPGXPoolConn(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to open DB: %v", err)
|
log.Fatalf("Failed to open DB: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package repo
|
package repo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -15,19 +16,20 @@ import (
|
|||||||
lua "github.com/yuin/gopher-lua"
|
lua "github.com/yuin/gopher-lua"
|
||||||
)
|
)
|
||||||
|
|
||||||
const LAST_GO_MIGRATION = 3
|
// LastGoMigration defines the last migration execute with go code. After
|
||||||
|
// that the lua API should be used.
|
||||||
|
const LastGoMigration = 3
|
||||||
|
|
||||||
|
// PgMigrator implements the Migrator interface for the PG connection.
|
||||||
type PgMigrator struct {
|
type PgMigrator struct {
|
||||||
Mig *mig.Migrate
|
Mig *mig.Migrate
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPgMigrator() (Migrator, error) {
|
// NewPgMigrator creates and returns a new migrator.
|
||||||
|
func NewPgMigrator(connStr string) (PgMigrator, error) {
|
||||||
cfg := config.GetInstance()
|
cfg := config.GetInstance()
|
||||||
|
|
||||||
str := "postgres://" + cfg.PgCfg.User + ":" + cfg.PgCfg.Pass + "@" +
|
m, err := mig.New(cfg.MigrationsPath, connStr)
|
||||||
cfg.PgCfg.Host + ":" + cfg.PgCfg.Port + "/" + cfg.PgCfg.DB + "?sslmode=disable"
|
|
||||||
|
|
||||||
m, err := mig.New(cfg.MigrationsPath, str)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return PgMigrator{}, err
|
return PgMigrator{}, err
|
||||||
}
|
}
|
||||||
@@ -37,13 +39,15 @@ func NewPgMigrator() (Migrator, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close closes what is necessary to close on a PgMigrator.
|
||||||
func (p PgMigrator) Close() {
|
func (p PgMigrator) Close() {
|
||||||
p.Mig.Close()
|
p.Mig.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a PgMigrator) Goto(id uint) error {
|
// Goto forces the migrations to a given migrations of id: `id`.
|
||||||
err := a.Mig.Migrate(id)
|
func (p PgMigrator) Goto(id uint) error {
|
||||||
if err != nil && err != mig.ErrNoChange {
|
err := p.Mig.Migrate(id)
|
||||||
|
if err != nil && !errors.Is(err, mig.ErrNoChange) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package pages
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
@@ -148,28 +147,28 @@ func expenseFromForm(ctx context.Context, c *gin.Context) (*mod.Expense, error)
|
|||||||
func fetchExpensesData(c *gin.Context) (map[string]any, error) {
|
func fetchExpensesData(c *gin.Context) (map[string]any, error) {
|
||||||
ctx, err := getLoggedInUserCTX(c)
|
ctx, err := getLoggedInUserCTX(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New(fmt.Sprintf("could not get logged in user - ", err))
|
return nil, fmt.Errorf("could not get logged in user - %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
eFilter := repo.NewExpFilter()
|
eFilter := repo.NewExpFilter()
|
||||||
expenses, err := exp.App.GetAllExpenses(ctx, eFilter)
|
expenses, err := exp.App.GetAllExpenses(ctx, eFilter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New(fmt.Sprintf("could not get expenses - ", err))
|
return nil, fmt.Errorf("could not get expenses - %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
categories, err := exp.App.GetAllCategories(ctx)
|
categories, err := exp.App.GetAllCategories(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New(fmt.Sprintf("could not get categories - ", err))
|
return nil, fmt.Errorf("could not get categories - %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
stores, err := exp.App.GetAllStores(ctx)
|
stores, err := exp.App.GetAllStores(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New(fmt.Sprintf("could not get stores - ", err))
|
return nil, fmt.Errorf("could not get stores - %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
types, err := exp.App.GetAllTypes(ctx)
|
types, err := exp.App.GetAllTypes(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New(fmt.Sprintf("could not get types - ", err))
|
return nil, fmt.Errorf("could not get types - %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return map[string]any{
|
return map[string]any{
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
schema.sql
|
schema.sql
|
||||||
.env
|
.env
|
||||||
pgsql-data-dev/
|
pgsql-data-dev/
|
||||||
|
pgSocket/
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ test: remove-dev-container
|
|||||||
--name $(container)-dev \
|
--name $(container)-dev \
|
||||||
--env-file .env \
|
--env-file .env \
|
||||||
-v ./pgsql-data-dev/:/var/lib/postgresql/data/ \
|
-v ./pgsql-data-dev/:/var/lib/postgresql/data/ \
|
||||||
|
-v ./pgSocket:/run/postgresql \
|
||||||
-it $(registry)/$(repo)/expensesdb:v0.0.91-alpha
|
-it $(registry)/$(repo)/expensesdb:v0.0.91-alpha
|
||||||
|
|
||||||
build_image:
|
build_image:
|
||||||
|
|||||||
Reference in New Issue
Block a user