Support MySQL by default

This commit is contained in:
Trevor Slocum 2024-01-15 12:26:31 -08:00
parent e78ebf589a
commit f52606c828
14 changed files with 101 additions and 111 deletions

View file

@ -7,9 +7,6 @@
Sriracha is a multi-board imageboard that may be customized by adding and removing extensions.
Extensions are simply additional pieces of software written in Go. Because of
this, developers have unlimited power to extend and customize Sriracha.
## Install
To install Sriracha at `~/go/bin/sriracha` run the following command:

View file

@ -2,6 +2,11 @@ package sriracha
import "time"
type AttachmentType struct {
MIME string // MIME type.
Extension string // File extension.
}
// Attachment represents an attachment. It may be a normal file or a shortcut
// to an external resource via oEmbed.
type Attachment struct {

View file

@ -1 +1,8 @@
package sriracha
type Board struct {
ID int
Dir string
Name string
Description string
}

View file

@ -3,24 +3,25 @@ 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 specifies the database driver to use. By default the "pq" (PostgreSQL),
// "mysql" and "sqlite" drivers are compiled in. Sriracha may be compiled with
// support for any other database driver by adding it to the list of 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.
//
// See https://pkg.go.dev/github.com/lib/pq for a full list of options.
// When using the "mysql" driver, DataSource may be set as follows:
// username:password@localhost)/dbname?allowFallbackToPlaintext=true
// See https://github.com/go-sql-driver/mysql/#dsn-data-source-name for a full list of options.
//
// When using the "sqlite" driver, Datasource may be set as follows:
// /path/to/private/dir/sriracha.db
// See https://pkg.go.dev/modernc.org/sqlite#Driver.Open for a full list of options.
DataSource string
}

View file

@ -10,6 +10,7 @@ import (
// Load database drivers. SQLite and PostgreSQL are supported by default.
_ "github.com/glebarez/go-sqlite"
_ "github.com/glebarez/go-sqlite/compat"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
)
@ -23,10 +24,10 @@ func main() {
// Extensions are processed in the order they are added.
// Register file attachment extensions.
sriracha.AddExtension(extension.AttachJPG())
sriracha.AddExtension(extension.NewAttachImage())
// Register post handling extensions.
sriracha.AddExtension(extension.RenderPost())
sriracha.AddExtension(extension.NewHelloWorld())
// Run Sriracha.
err := sriracha.Run(config.Driver, config.DataSource)

View file

@ -7,11 +7,20 @@ import (
"strings"
)
type Extension interface {
Description() string
type ExtensionSettings struct {
// Hidden fields
}
Data() interface{} // TODO reflect to see desired data types and default values
// populate like in gohan
func (s *ExtensionSettings) Get(name string) string {
return ""
}
func (s *ExtensionSettings) Set(name string, value string) {
return
}
type Extension interface {
Init(settings *ExtensionSettings) (description string)
}
var (
@ -34,9 +43,7 @@ func extensionName(e Extension) string {
if name[0] == '*' {
name = name[1:]
}
if strings.HasPrefix(name, "extension.") {
name = name[10:]
}
name = strings.TrimPrefix(name, "extension.")
return strings.ToUpper(name[0:1]) + name[1:]
}

42
extension/attach_image.go Normal file
View file

@ -0,0 +1,42 @@
package extension
import (
"io"
"log"
"code.rocket9labs.com/tslocum/sriracha"
)
type attachImage struct {
}
var _ sriracha.ExtensionAttach = &attachImage{}
func NewAttachImage() *attachImage {
return &attachImage{}
}
func (a *attachImage) Init(settings *sriracha.ExtensionSettings) string {
return "Handles attaching images."
}
func (a *attachImage) Types() []*sriracha.AttachmentType {
return []*sriracha.AttachmentType{
{
MIME: "image/jpeg",
Extension: "jpg",
}, {
MIME: "image/gif",
Extension: "gif",
}, {
MIME: "image/x-png",
Extension: "png",
},
}
}
func (a *attachImage) Attach(file io.Reader, size int64, mime string) (*sriracha.Attachment, error) {
// TODO import libs, use generic reader and scaler
log.Println("Hello, Image!")
return nil, nil
}

View file

@ -1,42 +0,0 @@
package extension
import (
"io"
"log"
"code.rocket9labs.com/tslocum/sriracha"
)
type attachJPGData struct {
Test string
}
type attachJPG struct {
data *attachJPGData
}
var _ sriracha.ExtensionAttach = &attachJPG{
data: &attachJPGData{},
}
func AttachJPG() *attachJPG {
return &attachJPG{}
}
func (a *attachJPG) Description() string {
//TODO implement me
panic("implement me")
}
func (a *attachJPG) Data() interface{} {
return a.data
}
func (a *attachJPG) Attach(file io.Reader, size int64, mime string) (*sriracha.Attachment, error) {
if mime != "image/jpeg" {
return nil, nil
}
log.Println("Hello, JPG!")
return nil, nil
}

View file

@ -10,10 +10,14 @@ import (
type helloWorld struct {
}
func HelloWorld() *helloWorld {
func NewHelloWorld() *helloWorld {
return &helloWorld{}
}
func (h *helloWorld) Init(settings *sriracha.ExtensionSettings) string {
return "Prints hello world."
}
func (h *helloWorld) Attach(file io.Reader, size int64, mime string) (*sriracha.Attachment, error) {
log.Println("Hello, world!")
return nil, nil

View file

@ -1,42 +0,0 @@
package extension
import (
"io"
"log"
"code.rocket9labs.com/tslocum/sriracha"
)
type renderPost struct {
}
func RenderPost() *renderPost {
return &renderPost{}
}
func (r *renderPost) RenderPost(post *sriracha.Post) ([]byte, error) {
return []byte("POST " + post.Subject + "-" + post.Message), nil
return nil, nil
}
func (r *renderPost) Description() string {
//TODO implement me
panic("implement me")
}
func (r *renderPost) Data() interface{} {
return nil
}
func (r *renderPost) Attach(file io.Reader, size int64, mime string) (*sriracha.Attachment, error) {
return nil, nil
}
func (r *renderPost) InsertPost(post *sriracha.Post) error {
log.Println("ATTACH", post.Subject)
return nil
}
func (r *renderPost) DeletePost(post *sriracha.Post) error {
return nil
}

View file

@ -7,6 +7,9 @@ import "io"
type ExtensionAttach interface {
Extension
// Types returns a list of attachment types handled by this extension.
Types() []*AttachmentType
// Attach handles an uploaded file.
Attach(file io.Reader, size int64, mime string) (*Attachment, error)
}

3
go.mod
View file

@ -4,6 +4,7 @@ go 1.19
require (
github.com/glebarez/go-sqlite v1.22.0
github.com/go-sql-driver/mysql v1.7.1
github.com/lib/pq v1.10.9
)
@ -13,7 +14,7 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/sys v0.16.0 // indirect
modernc.org/libc v1.38.0 // indirect
modernc.org/libc v1.40.3 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.7.2 // indirect
modernc.org/sqlite v1.28.0 // indirect

6
go.sum
View file

@ -2,6 +2,8 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec+ruQ=
github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
@ -14,8 +16,8 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qq
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
modernc.org/libc v1.38.0 h1:o4Lpk0zNDSdsjfEXnF1FGXWQ9PDi1NOdWcLP5n13FGo=
modernc.org/libc v1.38.0/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE=
modernc.org/libc v1.40.3 h1:JA242ofxBKFZ1aq4CZSLoJfN7T7fZbgXf9/ML60jvu0=
modernc.org/libc v1.40.3/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE=
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E=

View file

@ -33,11 +33,15 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
}
func Run(driver string, dataSource string) error {
log.Println("Connecting to database...")
err := connectDB(driver, dataSource)
if err != nil {
return err
}
log.Println("Connected to database successfully")
listenAddress := ":8080" // TODO
log.Printf("Listening on %s...", listenAddress)
http.HandleFunc("/imgboard", handleRequest)
return http.ListenAndServe(":8080", nil)
return http.ListenAndServe(listenAddress, nil)
}