Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

Commit

Permalink
added mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
hexahigh committed Feb 27, 2024
1 parent a0c3f7d commit 67ea083
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
24 changes: 20 additions & 4 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 67ea083

Please sign in to comment.