You've already forked speedtest-go
cd20f44d20
- IP detection: Cloudflare IPv6, ULA IPv6, proxy header chain, offline GeoIP DB - Database: add SQLite (pure Go, no CGo) and MSSQL backends - API: add JSON result sharing endpoint and ID obfuscation - Frontend: add modern CSS design, design switcher, favicon - Compatibility: ?cors parameter support, human-friendly distance rounding - Update Go to 1.21, add modernc.org/sqlite and maxminddb deps
47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
package database
|
|
|
|
import (
|
|
"github.com/librespeed/speedtest-go/config"
|
|
"github.com/librespeed/speedtest-go/database/bolt"
|
|
"github.com/librespeed/speedtest-go/database/memory"
|
|
"github.com/librespeed/speedtest-go/database/mssql"
|
|
"github.com/librespeed/speedtest-go/database/mysql"
|
|
"github.com/librespeed/speedtest-go/database/none"
|
|
"github.com/librespeed/speedtest-go/database/postgresql"
|
|
"github.com/librespeed/speedtest-go/database/schema"
|
|
"github.com/librespeed/speedtest-go/database/sqlite"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var (
|
|
DB DataAccess
|
|
)
|
|
|
|
type DataAccess interface {
|
|
Insert(*schema.TelemetryData) error
|
|
FetchByUUID(string) (*schema.TelemetryData, error)
|
|
FetchLast100() ([]schema.TelemetryData, error)
|
|
}
|
|
|
|
func SetDBInfo(conf *config.Config) {
|
|
switch conf.DatabaseType {
|
|
case "postgresql":
|
|
DB = postgresql.Open(conf.DatabaseHostname, conf.DatabaseUsername, conf.DatabasePassword, conf.DatabaseName)
|
|
case "mysql":
|
|
DB = mysql.Open(conf.DatabaseHostname, conf.DatabaseUsername, conf.DatabasePassword, conf.DatabaseName)
|
|
case "bolt":
|
|
DB = bolt.Open(conf.DatabaseFile)
|
|
case "sqlite":
|
|
DB = sqlite.Open(conf.DatabaseFile)
|
|
case "mssql":
|
|
DB = mssql.Open(conf.DatabaseHostname, conf.DatabaseUsername, conf.DatabasePassword, conf.DatabaseName, conf.DatabasePort)
|
|
case "memory":
|
|
DB = memory.Open("")
|
|
case "none":
|
|
DB = none.Open("")
|
|
default:
|
|
log.Fatalf("Unsupported database type: %s", conf.DatabaseType)
|
|
}
|
|
}
|