tempestas

A REST API for processing sensor.community data
git clone https://git.bracken.jp/tempestas.git
Log | Files | Refs | README | LICENSE

commit bf6e0960f66d73ba2368dfff55328862422a80d2
parent 9771ddbe35f4e955a46f102d80ab70743b875b4a
Author: Chris Bracken <chris@bracken.jp>
Date:   Thu, 11 Nov 2021 15:14:06 -0800

Add NullDataStore

Extracts a DataStore interface. Adds NullDataStore, which is useful for
writing tests and running a test instance without a database connection.

Diffstat:
Mhttp/http.go | 4++--
Mmain.go | 2+-
Mstorage/storage.go | 28+++++++++++++++++++++++-----
3 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/http/http.go b/http/http.go @@ -13,10 +13,10 @@ import ( ) type HttpServer struct { - ds *storage.DataStore + ds storage.DataStore } -func CreateServer(ds *storage.DataStore) *HttpServer { +func CreateServer(ds storage.DataStore) *HttpServer { s := new(HttpServer) s.ds = ds return s diff --git a/main.go b/main.go @@ -15,7 +15,7 @@ func main() { dbname := os.Getenv("TEMPESTAS_DB") dbuser := os.Getenv("TEMPESTAS_USER") dbpass := os.Getenv("TEMPESTAS_PASS") - ds := storage.Connect(dbname, dbuser, dbpass) + ds := storage.ConnectPostgres(dbname, dbuser, dbpass) defer ds.Close() fmt.Println("Waiting for requests") diff --git a/storage/storage.go b/storage/storage.go @@ -15,12 +15,17 @@ const ( "VALUES($1, $2, $3, $4, $5)" ) -type DataStore struct { +type DataStore interface { + Close() + StoreReport(report *airrohr.Report, t time.Time) +} + +type PostgresDataStore struct { db *sql.DB } -func Connect(dbname string, user string, password string) *DataStore { - var s = new(DataStore) +func ConnectPostgres(dbname string, user string, password string) *PostgresDataStore { + var s = new(PostgresDataStore) var err error // Create the database object. A nil error does not guarantee the DB is connected. @@ -39,11 +44,11 @@ func Connect(dbname string, user string, password string) *DataStore { return s } -func (s DataStore) Close() { +func (s *PostgresDataStore) Close() { s.db.Close() } -func (s DataStore) StoreReport(report *airrohr.Report, t time.Time) { +func (s *PostgresDataStore) StoreReport(report *airrohr.Report, t time.Time) { if s.db == nil { panic("Not connected to database") } @@ -55,3 +60,16 @@ func (s DataStore) StoreReport(report *airrohr.Report, t time.Time) { } } } + +type NullDataStore struct { +} + +func ConnectNull() *NullDataStore { + return new(NullDataStore) +} + +func (s *NullDataStore) Close() { +} + +func (s *NullDataStore) StoreReport(report *airrohr.Report, t time.Time) { +}