diff --git a/README.md b/README.md index 9f44e2a..a61fabc 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/cmd/sriracha/config.go b/cmd/sriracha/config.go new file mode 100644 index 0000000..88a0f8d --- /dev/null +++ b/cmd/sriracha/config.go @@ -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{} diff --git a/cmd/sriracha/main.go b/cmd/sriracha/main.go index eaf1756..81a48ac 100644 --- a/cmd/sriracha/main.go +++ b/cmd/sriracha/main.go @@ -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) diff --git a/extension.go b/extension.go index 7cb1654..9c00761 100644 --- a/extension.go +++ b/extension.go @@ -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 diff --git a/extension_attach.go b/extension_attach.go index a9e92d3..4a4444a 100644 --- a/extension_attach.go +++ b/extension_attach.go @@ -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 diff --git a/extension_database.go b/extension_database.go index 4c9af21..a0846fc 100644 --- a/extension_database.go +++ b/extension_database.go @@ -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 diff --git a/extension_post.go b/extension_post.go index d3097a0..1d6fc1f 100644 --- a/extension_post.go +++ b/extension_post.go @@ -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 } diff --git a/go.mod b/go.mod index 88f275b..f3f0e7f 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 0aa7521..50bbcc7 100644 --- a/go.sum +++ b/go.sum @@ -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=