mirror of
https://github.com/cf-sonr/motr.git
synced 2026-01-12 02:59:13 +00:00
refactor: consolidate database connection logic and configuration
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -90,7 +90,6 @@ bin/
|
||||
.ignore
|
||||
.opencommitignore
|
||||
heighliner*
|
||||
sonr
|
||||
deploy/**/data
|
||||
x/.DS_Store
|
||||
.aider*
|
||||
@@ -101,7 +100,6 @@ pkg/nebula/node_modules
|
||||
configs/logs.json
|
||||
|
||||
mprocs.yaml
|
||||
sonr.wiki
|
||||
|
||||
!devbox.lock
|
||||
!buf.lock
|
||||
@@ -110,7 +108,6 @@ sonr.wiki
|
||||
mprocs.yaml
|
||||
mprocs.log
|
||||
tools-stamp
|
||||
sonr.log
|
||||
deploy/conf
|
||||
|
||||
interchaintest-downloader
|
||||
|
||||
@@ -5,7 +5,6 @@ package app
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/onsonr/motr/internal/database"
|
||||
"github.com/onsonr/motr/internal/serve"
|
||||
"github.com/onsonr/motr/pkg/config"
|
||||
"github.com/onsonr/motr/x/identity"
|
||||
@@ -15,7 +14,7 @@ import (
|
||||
|
||||
type App struct {
|
||||
Config *config.MotrConfig
|
||||
Database *database.Connection
|
||||
Database *config.DBConnection
|
||||
e *echo.Echo
|
||||
}
|
||||
|
||||
|
||||
@@ -6,12 +6,11 @@ package app
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
echomiddleware "github.com/labstack/echo/v4/middleware"
|
||||
"github.com/onsonr/motr/internal/database"
|
||||
"github.com/onsonr/motr/pkg/config"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
conn *database.Connection
|
||||
conn *config.DBConnection
|
||||
cfg *config.MotrConfig
|
||||
mdws []echo.MiddlewareFunc
|
||||
}
|
||||
@@ -43,7 +42,7 @@ func WithConfig(cfg *config.MotrConfig) func(*Options) {
|
||||
}
|
||||
}
|
||||
|
||||
func WithDatabase(conn *database.Connection) func(*Options) {
|
||||
func WithDatabase(conn *config.DBConnection) func(*Options) {
|
||||
return func(o *Options) {
|
||||
o.conn = conn
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
FROM golang:1.18-alpine AS builder
|
||||
|
||||
@@ -8,20 +8,14 @@ import (
|
||||
|
||||
_ "github.com/ncruces/go-sqlite3/driver"
|
||||
_ "github.com/ncruces/go-sqlite3/embed"
|
||||
"github.com/onsonr/motr/pkg/config"
|
||||
"github.com/onsonr/motr/x/identity"
|
||||
"github.com/onsonr/motr/x/portfolio"
|
||||
"github.com/onsonr/motr/x/user"
|
||||
)
|
||||
|
||||
type Connection struct {
|
||||
db *sql.DB
|
||||
Identity identity.Model
|
||||
Portfolio portfolio.Model
|
||||
User user.Model
|
||||
}
|
||||
|
||||
// New returns a new SQLite database instance
|
||||
func New() (*Connection, error) {
|
||||
func New() (*config.DBConnection, error) {
|
||||
db, err := sql.Open("sqlite3", ":memory:")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -40,9 +34,8 @@ func New() (*Connection, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Connection{
|
||||
db: db,
|
||||
return &config.DBConnection{
|
||||
DB: db,
|
||||
Identity: idTable,
|
||||
Portfolio: portTable,
|
||||
User: userTable,
|
||||
|
||||
21
pkg/config/runtime.go
Normal file
21
pkg/config/runtime.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/onsonr/motr/x/identity"
|
||||
"github.com/onsonr/motr/x/portfolio"
|
||||
"github.com/onsonr/motr/x/user"
|
||||
)
|
||||
|
||||
type DBConnection struct {
|
||||
DB *sql.DB
|
||||
Identity identity.Model
|
||||
Portfolio portfolio.Model
|
||||
User user.Model
|
||||
}
|
||||
|
||||
type RuntimeContext interface {
|
||||
GetMotrConfig() *MotrConfig
|
||||
GetDatabaseConnection() *DBConnection
|
||||
}
|
||||
Reference in New Issue
Block a user