Add PostgreSQL DB support

main
Leonid Maslakov 2 weeks ago
parent 8d31977a21
commit dec3b77b23
  1. 2
      cmd/main.go
  2. 1
      go.mod
  3. 2
      go.sum
  4. 17
      internal/storage/storage.go
  5. 56
      internal/storage/storage_paste.go

@ -80,7 +80,7 @@ func printHelp(noErrors bool) {
println("")
println(" -addres ADDRES:PORT (default: :80)")
println(" -web-dir Dir with page templates and static content")
println(" -db-driver Only 'sqlite3' is available yet (default: sqlite3)")
println(" -db-driver Currently supported drivers: 'sqlite3' and 'postgres' (default: sqlite3)")
println(" -db-source DB source")
println(" -db-cleanup-period Interval at which the DB is cleared of expired but not yet deleted pastes. (default: 3h)")
println(" -robots-disallow Prohibits search engine crawlers from indexing site using robots.txt file.")

@ -8,4 +8,5 @@ require (
git.lcomrade.su/root/lineend v1.0.0
github.com/alecthomas/chroma v0.10.0
github.com/mattn/go-sqlite3 v1.14.14
github.com/lib/pq v1.10.6
)

@ -7,6 +7,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/lib/pq v1.10.6 h1:jbk+ZieJ0D7EVGJYpL9QTz7/YW6UHbmdnZWYyK5cdBs=
github.com/lib/pq v1.10.6/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-sqlite3 v1.14.14 h1:qZgc/Rwetq+MtyE18WhzjokPD93dNqLGNT3QJuLvBGw=
github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

@ -21,6 +21,7 @@ package storage
import (
"database/sql"
"errors"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
@ -48,14 +49,14 @@ func (dbInfo DB) InitDB() error {
// Create tables
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS "pastes" (
"id" TEXT PRIMARY KEY,
"title" TEXT NOT NULL,
"body" TEXT NOT NULL,
"syntax" TEXT NOT NULL,
"create_time" INTEGER NOT NULL,
"delete_time" INTEGER NOT NULL,
"one_use" BOOL NOT NULL
CREATE TABLE IF NOT EXISTS pastes (
id TEXT PRIMARY KEY,
title TEXT NOT NULL,
body TEXT NOT NULL,
syntax TEXT NOT NULL,
create_time INTEGER NOT NULL,
delete_time INTEGER NOT NULL,
one_use BOOL NOT NULL
);
`)
if err != nil {

@ -57,7 +57,7 @@ func (dbInfo DB) PasteAdd(paste Paste) (string, error) {
// Add
_, err = db.Exec(
`INSERT INTO "pastes" ("id", "title", "body", "syntax", "create_time", "delete_time", "one_use") VALUES (?, ?, ?, ?, ?, ?, ?)`,
`INSERT INTO pastes (id, title, body, syntax, create_time, delete_time, one_use) VALUES ($1, $2, $3, $4, $5, $6, $7)`,
paste.ID, paste.Title, paste.Body, paste.Syntax, paste.CreateTime, paste.DeleteTime, paste.OneUse,
)
if err != nil {
@ -77,7 +77,7 @@ func (dbInfo DB) PasteDelete(id string) error {
// Delete
result, err := db.Exec(
`DELETE FROM "pastes" WHERE id = ?`,
`DELETE FROM pastes WHERE id = $1`,
id,
)
if err != nil {
@ -109,7 +109,7 @@ func (dbInfo DB) PasteGet(id string) (Paste, error) {
// Make query
row := db.QueryRow(
`SELECT "id", "title", "body", "syntax", "create_time", "delete_time", "one_use" FROM "pastes" WHERE "id" = ?`,
`SELECT id, title, body, syntax, create_time, delete_time, one_use FROM pastes WHERE id = $1`,
id,
)
@ -127,7 +127,7 @@ func (dbInfo DB) PasteGet(id string) (Paste, error) {
if paste.DeleteTime < time.Now().Unix() && paste.DeleteTime > 0 {
// Delete expired paste
_, err = db.Exec(
`DELETE FROM "pastes" WHERE id = ?`,
`DELETE FROM pastes WHERE id = $1`,
paste.ID,
)
if err != nil {
@ -141,52 +141,6 @@ func (dbInfo DB) PasteGet(id string) (Paste, error) {
return paste, nil
}
func (dbInfo DB) PasteGetList() ([]Paste, error) {
var pastes []Paste
// Open DB
db, err := dbInfo.openDB()
if err != nil {
return pastes, err
}
defer db.Close()
// Delete expired paste
_, err = db.Exec(
`DELETE FROM "pastes" WHERE delete_time < ? AND delete_time > 0`,
time.Now().Unix(),
)
if err != nil {
return pastes, err
}
// Make query to get paste list
rows, err := db.Query(
`SELECT "id", "title", "body", "syntax", "create_time", "delete_time", "one_use" FROM "pastes"`,
)
if err != nil {
if err == sql.ErrNoRows {
return pastes, ErrNotFoundID
}
return pastes, err
}
// Read query
for rows.Next() {
var paste Paste
err = rows.Scan(&paste.ID, &paste.Title, &paste.Body, &paste.Syntax, &paste.CreateTime, &paste.DeleteTime, &paste.OneUse)
if err != nil {
return pastes, err
}
pastes = append(pastes, paste)
}
return pastes, nil
}
func (dbInfo DB) PasteDeleteExpired() (int64, error) {
// Open DB
db, err := dbInfo.openDB()
@ -197,7 +151,7 @@ func (dbInfo DB) PasteDeleteExpired() (int64, error) {
// Delete
result, err := db.Exec(
`DELETE FROM "pastes" WHERE delete_time < ? AND delete_time > 0`,
`DELETE FROM pastes WHERE (delete_time < $1) AND (delete_time > 0)`,
time.Now().Unix(),
)
if err != nil {

Loading…
Cancel
Save