diff --git a/backend/go.mod b/backend/go.mod index a17942a..485f4fa 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -3,6 +3,7 @@ module github.com/hexahigh/yapc/backend go 1.21 require ( + github.com/go-sql-driver/mysql v1.7.1 // indirect github.com/klauspost/compress v1.17.4 // indirect github.com/mattn/go-sqlite3 v1.14.19 // indirect ) diff --git a/backend/go.sum b/backend/go.sum index 97db123..c39e142 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -1,3 +1,5 @@ +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/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4= github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= github.com/klauspost/compress v1.17.6 h1:60eq2E/jlfwQXtvZEeBUYADs+BwKBWURIY+Gj2eRGjI= diff --git a/backend/main.go b/backend/main.go index a67d3b8..55a6d66 100644 --- a/backend/main.go +++ b/backend/main.go @@ -22,12 +22,13 @@ import ( "syscall" "time" + _ "github.com/go-sql-driver/mysql" _ "github.com/mattn/go-sqlite3" "github.com/klauspost/compress/zstd" ) -const version = "2.0.0" +const version = "2.1.0" var ( dataDir = flag.String("d", "./data", "Folder to store files") @@ -37,6 +38,11 @@ var ( dbFile = flag.String("db", "./data/yapc.db", "SQLite database file to use for the url shortener") noSpeedtest = flag.Bool("disable-speedtest", true, "Disable speedtest") logging = flag.Bool("log", false, "Enable logging") + mysql = flag.Bool("mysql", false, "Enable MySQL/MariaDB") + mysqlPass = flag.String("mysql-pass", "", "MySQL password (if any)") + mysqlUser = flag.String("mysql-user", "root", "MySQL user") + mysqlHost = flag.String("mysql-host", "localhost:3306", "MySQL host") + mysqlDB = flag.String("mysql-db", "yapc", "MySQL database") ) var downloadSpeeds []float64 @@ -61,9 +67,19 @@ func main() { // Initialize the SQLite database var err error - db, err = sql.Open("sqlite3", fmt.Sprintf("file:%s?cache=shared&mode=rwc", *dbFile)) - if err != nil { - log.Fatal(err) + if *mysql { + db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s", *mysqlUser, *mysqlPass, *mysqlHost, *mysqlDB)) + if err != nil { + log.Fatal(err) + } + db.SetConnMaxLifetime(time.Minute * 3) + db.SetMaxOpenConns(10) + db.SetMaxIdleConns(10) + } else { + db, err = sql.Open("sqlite3", fmt.Sprintf("file:%s?cache=shared&mode=rwc", *dbFile)) + if err != nil { + log.Fatal(err) + } } onStart() initDB()