feat: 🎨 enhance storage and logging factories

master
Xeronith 2022-08-19 13:14:39 +04:30
rodzic 56ded965d9
commit 8488e6f657
12 zmienionych plików z 77 dodań i 29 usunięć

Wyświetl plik

@ -6,11 +6,14 @@ import (
"config" "config"
"db" "db"
"fmt" "fmt"
"logging"
"server" "server"
) )
func main() { func main() {
storage := db.NewSqliteStorage() logger := logging.CreateLogger(logging.StdIOLogger)
storage := db.CreateStorage(db.SqliteStorage)
storage.Connect(config.SQLITE_DB) storage.Connect(config.SQLITE_DB)
storage.Migrate( storage.Migrate(
&repos.User{}, &repos.User{},
@ -22,6 +25,7 @@ func main() {
app := server.New() app := server.New()
app.SetStorageProvider(storage) app.SetStorageProvider(storage)
app.SetLogger(logger)
app.Bind( app.Bind(
routes.Root, routes.Root,

Wyświetl plik

@ -1,8 +1,12 @@
package contracts package contracts
type ILogger interface { type (
Info(...any) LoggerType int
Debug(...any)
Error(...any) ILogger interface {
Fatal(...any) Info(...any)
} Debug(...any)
Error(...any)
Fatal(...any)
}
)

Wyświetl plik

@ -91,6 +91,7 @@ type (
Listen(address string) Listen(address string)
SetStorageProvider(IStorage) SetStorageProvider(IStorage)
SetLogger(ILogger)
} }
IRouter interface { IRouter interface {

Wyświetl plik

@ -1,20 +1,24 @@
package contracts package contracts
type IStorage interface { type (
// Connect initiate the database connection StorageType int
Connect(path string)
// Migrate migrates all the database tables
Migrate(...interface{}) error
Prepare(string) IQuery
}
type IQuery interface { IStorage interface {
Param(string) IResult // Connect initiate the database connection
Params(...string) IResult Connect(path string)
} // Migrate migrates all the database tables
Migrate(...interface{}) error
Prepare(string) IQuery
}
type IResult interface { IQuery interface {
Get(string) any Param(string) IResult
Set(string, string) Params(...string) IResult
Length() int }
}
IResult interface {
Get(string) any
Set(string, string)
Length() int
}
)

Wyświetl plik

@ -1 +1,16 @@
package db package db
import "contracts"
const (
SqliteStorage contracts.StorageType = 0
)
func CreateStorage(componentType contracts.StorageType) contracts.IStorage {
switch componentType {
case SqliteStorage:
return NewSqliteStorage()
default:
panic("unknown_storage_type")
}
}

Wyświetl plik

@ -6,7 +6,7 @@ use (
./config ./config
./contracts ./contracts
./db ./db
./log ./logging
./server ./server
./utility ./utility
) )

Wyświetl plik

@ -1 +0,0 @@
package log

Wyświetl plik

@ -1,3 +0,0 @@
module log
go 1.19

Wyświetl plik

@ -0,0 +1,16 @@
package logging
import "contracts"
const (
StdIOLogger contracts.LoggerType = 0
)
func CreateLogger(componentType contracts.LoggerType) contracts.ILogger {
switch componentType {
case StdIOLogger:
return New(true)
default:
panic("unknown_logger_type")
}
}

Wyświetl plik

@ -0,0 +1,3 @@
module logging
go 1.19

Wyświetl plik

@ -1,4 +1,4 @@
package log package logging
import ( import (
"contracts" "contracts"

Wyświetl plik

@ -19,6 +19,7 @@ import (
type httpServer struct { type httpServer struct {
framework *fiber.App framework *fiber.App
storage IStorage storage IStorage
logger ILogger
} }
func authorization(c *fiber.Ctx) error { func authorization(c *fiber.Ctx) error {
@ -87,6 +88,10 @@ func (server *httpServer) SetStorageProvider(storage IStorage) {
server.storage = storage server.storage = storage
} }
func (server *httpServer) SetLogger(logger ILogger) {
server.logger = logger
}
func (server *httpServer) Bind(routes ...IRoute) { func (server *httpServer) Bind(routes ...IRoute) {
for _, route := range routes { for _, route := range routes {
func(route IRoute) { func(route IRoute) {