Feat: add storage block(first methods to db, migrations, models) add nginx parser with regular expression, add to deps sqlite driver
All checks were successful
CI.yml / build (push) Successful in 1m51s
All checks were successful
CI.yml / build (push) Successful in 1m51s
This commit is contained in:
42
internal/storage/db.go
Normal file
42
internal/storage/db.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/d3m0k1d/BanForge/internal/logger"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
logger *logger.Logger
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewDB(path string) (*DB, error) {
|
||||
db, err := sql.Open("sqlite3", "/var/lib/banforge/storage.db")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &DB{
|
||||
logger: logger.New(false),
|
||||
db: db,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (d *DB) Close() error {
|
||||
d.logger.Info("Closing database connection")
|
||||
err := d.db.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DB) CreateTable() error {
|
||||
_, err := d.db.Exec(CreateTables)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.logger.Info("Created tables")
|
||||
return nil
|
||||
}
|
||||
27
internal/storage/migrations.go
Normal file
27
internal/storage/migrations.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package storage
|
||||
|
||||
const CreateTables = `
|
||||
|
||||
CREATE TABLE IF NOT EXISTS requests (
|
||||
id INTEGER PRIMARY KEY,
|
||||
service TEXT NOT NULL,
|
||||
ip TEXT NOT NULL,
|
||||
path TEXT,
|
||||
method TEXT,
|
||||
status TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS bans (
|
||||
id INTEGER PRIMARY KEY,
|
||||
ip TEXT UNIQUE NOT NULL,
|
||||
reason TEXT,
|
||||
banned_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_service ON requests(service);
|
||||
CREATE INDEX IF NOT EXISTS idx_ip ON requests(ip);
|
||||
CREATE INDEX IF NOT EXISTS idx_status ON requests(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_created_at ON requests(created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_ban_ip ON bans(ip);
|
||||
`
|
||||
15
internal/storage/models.go
Normal file
15
internal/storage/models.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package storage
|
||||
|
||||
type LogEntry struct {
|
||||
Service string
|
||||
IP string
|
||||
Path *string
|
||||
Status *string
|
||||
Method *string
|
||||
Reason *string
|
||||
}
|
||||
|
||||
type Ban struct {
|
||||
IP string
|
||||
Reason *string
|
||||
}
|
||||
Reference in New Issue
Block a user