Refactor extensions

This commit is contained in:
Trevor Slocum 2023-04-29 20:17:56 -07:00
parent aa27ad084c
commit df818c0a17
9 changed files with 65 additions and 20 deletions

View File

@ -1,4 +1,5 @@
# Sriracha - Extensible imageboard
[![GoDoc](https://code.rocketnine.space/tslocum/godoc-static/raw/branch/master/badge.svg)](https://docs.rocketnine.space/code.rocketnine.space/tslocum/sriracha)
[![Donate via LiberaPay](https://img.shields.io/liberapay/receives/rocketnine.space.svg?logo=liberapay)](https://liberapay.com/rocketnine.space)
[![Donate via Patreon](https://img.shields.io/badge/dynamic/json?color=%23e85b46&label=Patreon&query=data.attributes.patron_count&suffix=%20patrons&url=https%3A%2F%2Fwww.patreon.com%2Fapi%2Fcampaigns%2F5252223)](https://www.patreon.com/rocketnine)

27
cmd/sriracha/config.go Normal file
View File

@ -0,0 +1,27 @@
package main
// AppConfig defines the database configuration. All other configuration
// options are stored within the database.
type AppConfig struct {
// Driver specifies the database driver to use. By default, the "sqlite"
// and "pq" (PostgreSQL) drivers are available. Sriracha may be compiled
// with support for any other database driver by adding it to the imports.
Driver string
// DataSource defines the where and how to interface with the database. Its
// contents depend on which database driver is in use.
//
// When using the "sqlite" driver, Datasource may be set to a file path,
// typically ending in ".db".
//
// See https://pkg.go.dev/modernc.org/sqlite#Driver.Open for a full list of options.
//
// When using the "pq" driver, DataSource may be set as follows:
//
// postgres://username:password@localhost:5432/database?sslmode=disable
//
// See https://pkg.go.dev/github.com/lib/pq for a full list of options.
DataSource string
}
var config = &AppConfig{}

View File

@ -2,30 +2,33 @@ package main
import (
"log"
"path/filepath"
"code.rocketnine.space/tslocum/sriracha"
"code.rocketnine.space/tslocum/sriracha/extension"
// Load SQLite database driver. Multiple drivers may be loaded at the same time.
// Load database drivers.
_ "github.com/lib/pq"
_ "modernc.org/sqlite"
)
func main() {
// Register extensions.
// Extensions are processed in the order they are added.
databaseName := filepath.Join(".", "db")
sqlDatabase, err := extension.DatabaseSQL("sqlite", databaseName)
// Register database extension.
sqlDatabase, err := extension.DatabaseSQL(config.Driver, config.DataSource)
if err != nil {
log.Fatal(err)
}
sriracha.AddExtension(sqlDatabase)
// Register file attachment extensions.
sriracha.AddExtension(extension.AttachJPG())
// Register post handling extensions.
sriracha.AddExtension(extension.RenderPost())
// Run Sriracha.
err = sriracha.Run()
if err != nil {
log.Fatal(err)

View File

@ -39,23 +39,28 @@ func extensionName(e Extension) string {
func attach(file io.Reader, size int64, mime string) (*Attachment, error) {
for i, ext := range allExtensions {
log.Println("DEBUG ATTACH", allExtensionNames[i])
a, err := ext.Attach(file, size, mime)
if err != nil {
return nil, err
} else if a != nil {
return a, nil
if extension, ok := ext.(ExtensionAttach); ok {
log.Println("DEBUG ATTACH", allExtensionNames[i])
a, err := extension.Attach(file, size, mime)
if err != nil {
return nil, err
} else if a != nil {
return a, nil
}
}
}
return nil, nil
}
func post(p *Post) error {
for i, ext := range allExtensions {
log.Println("DEBUG POST", allExtensionNames[i])
err := ext.CreatePost(p)
if err != nil {
return err
if extension, ok := ext.(ExtensionPost); ok {
log.Println("DEBUG POST", allExtensionNames[i])
err := extension.Post(p)
if err != nil {
return err
}
}
}
return nil

View File

@ -2,6 +2,8 @@ package sriracha
import "io"
// ExtensionAttach defines the interface for extensions that handle attaching
// files when creating a post.
type ExtensionAttach interface {
Extension

View File

@ -1,5 +1,7 @@
package sriracha
// ExtensionDatabase defines the interface for extensions that handle storing and
// retrieving information within a database.
type ExtensionDatabase interface {
Extension

View File

@ -1,11 +1,11 @@
package sriracha
// ExtensionPost defines the interface for extensions that handle creating a post.
type ExtensionPost interface {
Extension
InsertPost(post *Post) error
DeletePost(post *Post) error
RenderPost(post *Post) ([]byte, error)
// Post is called when a new post is created. Extensions may modify the
// post and apply formatting or other operations on it. All text is
// initially HTML-escaped.
Post(post *Post) error
}

5
go.mod
View File

@ -2,7 +2,10 @@ module code.rocketnine.space/tslocum/sriracha
go 1.19
require modernc.org/sqlite v1.21.2
require (
github.com/lib/pq v1.10.9
modernc.org/sqlite v1.21.2
)
require (
github.com/dustin/go-humanize v1.0.1 // indirect

2
go.sum
View File

@ -6,6 +6,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=