speedtest-go/database/database.go
2021-09-18 02:19:14 +08:00

41 lines
1.1 KiB
Go

package database
import (
"github.com/librespeed/speedtest/config"
"github.com/librespeed/speedtest/database/bolt"
"github.com/librespeed/speedtest/database/memory"
"github.com/librespeed/speedtest/database/mysql"
"github.com/librespeed/speedtest/database/none"
"github.com/librespeed/speedtest/database/postgresql"
"github.com/librespeed/speedtest/database/schema"
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 "memory":
DB = memory.Open("")
case "none":
DB = none.Open("")
default:
log.Fatalf("Unsupported database type: %s", conf.DatabaseType)
}
}