dev #1
@@ -1,5 +1,13 @@
|
|||||||
package serve
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"scrap/internal/config"
|
||||||
|
"scrap/internal/db"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
config.Setup()
|
||||||
|
|
||||||
|
db.Setup()
|
||||||
|
defer db.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
3
config.json
Normal file
3
config.json
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"sql-tables-dir": "./sqltable/"
|
||||||
|
}
|
||||||
BIN
example.db
Normal file
BIN
example.db
Normal file
Binary file not shown.
2
go.mod
2
go.mod
@@ -1,3 +1,5 @@
|
|||||||
module scrap
|
module scrap
|
||||||
|
|
||||||
go 1.24.4
|
go 1.24.4
|
||||||
|
|
||||||
|
require github.com/mattn/go-sqlite3 v1.14.32
|
||||||
|
|||||||
2
go.sum
Normal file
2
go.sum
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
github.com/mattn/go-sqlite3 v1.14.32 h1:JD12Ag3oLy1zQA+BNn74xRgaBbdhbNIDYvQUEuuErjs=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.32/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||||
25
internal/config/setup.go
Normal file
25
internal/config/setup.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AppConfig struct {
|
||||||
|
SqlTablesDir string `json:"sql-tables-dir"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var appConfigInstance *AppConfig
|
||||||
|
|
||||||
|
func Setup() {
|
||||||
|
file, err := os.ReadFile("config.json")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = json.Unmarshal(file, &appConfigInstance); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAppConfig() *AppConfig { return appConfigInstance }
|
||||||
14
internal/db/irepository.go
Normal file
14
internal/db/irepository.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import "database/sql"
|
||||||
|
|
||||||
|
type ITxRepository interface {
|
||||||
|
// Creates a new tx.
|
||||||
|
Begin() (*sql.Tx, error)
|
||||||
|
|
||||||
|
// Rollbacks tx's data or returns an error to the given error's pointer address.
|
||||||
|
RollbackOnError(*sql.Tx, *error)
|
||||||
|
|
||||||
|
// Applies changes to the database.
|
||||||
|
Commit(*sql.Tx) error
|
||||||
|
}
|
||||||
34
internal/db/repository.go
Normal file
34
internal/db/repository.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TxRepository struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTxRepository(instance *sql.DB) ITxRepository {
|
||||||
|
return &TxRepository{
|
||||||
|
db: instance,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t TxRepository) Begin() (*sql.Tx, error) {
|
||||||
|
tx, err := t.db.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (TxRepository) RollbackOnError(tx *sql.Tx, errObserve *error) {
|
||||||
|
if *errObserve != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (TxRepository) Commit(tx *sql.Tx) error {
|
||||||
|
return tx.Commit()
|
||||||
|
}
|
||||||
65
internal/db/setup.go
Normal file
65
internal/db/setup.go
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"os"
|
||||||
|
"scrap/internal/config"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
)
|
||||||
|
|
||||||
|
var dbInstance *sql.DB
|
||||||
|
|
||||||
|
func Setup() {
|
||||||
|
db, err := sql.Open("sqlite3", "./example.db")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dbInstance = db
|
||||||
|
|
||||||
|
tableFilenames := getTableFilenames()
|
||||||
|
createTablesFromSQLFiles(tableFilenames)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetInstance() *sql.DB { return dbInstance }
|
||||||
|
|
||||||
|
func Close() { dbInstance.Close() }
|
||||||
|
|
||||||
|
func getTableFilenames() []string {
|
||||||
|
appConfig := config.GetAppConfig()
|
||||||
|
|
||||||
|
files, err := os.ReadDir(appConfig.SqlTablesDir)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
filenames := []string{}
|
||||||
|
for _, f := range files {
|
||||||
|
fName := f.Name()
|
||||||
|
if !strings.HasSuffix(fName, ".sql") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
filenames = append(filenames, fName)
|
||||||
|
}
|
||||||
|
|
||||||
|
return filenames
|
||||||
|
}
|
||||||
|
|
||||||
|
func createTablesFromSQLFiles(filenames []string) {
|
||||||
|
appConfig := config.GetAppConfig()
|
||||||
|
|
||||||
|
for _, fName := range filenames {
|
||||||
|
tableBytes, err := os.ReadFile(appConfig.SqlTablesDir + fName)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tableQuery := string(tableBytes)
|
||||||
|
if _, err = dbInstance.Exec(tableQuery); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
5
sqltable/1_articles.sql
Normal file
5
sqltable/1_articles.sql
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS articles(
|
||||||
|
uuid CHAR(36),
|
||||||
|
title VARCHAR(255),
|
||||||
|
content TEXT
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user