-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from aerogear/db-implementation
Database Integration
- Loading branch information
Showing
23 changed files
with
878 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
vendor/ | ||
.idea/ | ||
metrics | ||
dist/ | ||
dist/ | ||
coverage*.out |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,51 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"github.com/aerogear/aerogear-metrics-api/pkg/config" | ||
"github.com/aerogear/aerogear-metrics-api/pkg/dao" | ||
"github.com/aerogear/aerogear-metrics-api/pkg/mobile" | ||
"github.com/aerogear/aerogear-metrics-api/pkg/web" | ||
) | ||
|
||
func main() { | ||
router := web.NewRouter() | ||
db, err := dao.Connect() | ||
|
||
config := config.GetConfig() | ||
|
||
dbHandler := dao.DatabaseHandler{} | ||
|
||
err := dbHandler.Connect(config["DBHost"], config["DBUser"], config["DBPassword"], config["DBName"], config["SSLMode"]) | ||
|
||
if err != nil { | ||
panic("failed to connect to sql database : " + err.Error()) | ||
} | ||
defer db.Close() | ||
metricDao := dao.NewMetricsDAO(db) | ||
defer dbHandler.DB.Close() | ||
|
||
if err := dbHandler.DoInitialSetup(); err != nil { | ||
panic("failed to perform database setup : " + err.Error()) | ||
} | ||
|
||
metricsDao := dao.NewMetricsDAO(dbHandler.DB) | ||
router := web.NewRouter() | ||
|
||
//metrics route | ||
{ | ||
metricsService := mobile.NewMetricsService(metricDao) | ||
metricsService := mobile.NewMetricsService(metricsDao) | ||
metricsHandler := web.NewMetricsHandler(metricsService) | ||
web.MetricsRoute(router, metricsHandler) | ||
} | ||
//health route | ||
{ | ||
healthHandler := web.NewHealthHandler() | ||
healthHandler := web.NewHealthHandler(metricsDao) | ||
web.HealthzRoute(router, healthHandler) | ||
} | ||
|
||
log.Printf("Starting application... going to listen on %v", config["ListenAddress"]) | ||
|
||
//start | ||
if err := http.ListenAndServe(":3000", router); err != nil { | ||
if err := http.ListenAndServe(config["ListenAddress"], router); err != nil { | ||
panic("failed to start " + err.Error()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
postgres: | ||
image: timescale/timescaledb | ||
image: postgres:9.5 | ||
ports: | ||
- 5432:5432 | ||
environment: | ||
POSTGRES_PASSWORD: postgres | ||
POSTGRES_USER: postgres | ||
PGDATA : /var/lib/postgresql/data/pgdata | ||
volumes: | ||
- psqlvolumes:/var/lib/postgresql/data/pgdata | ||
POSTGRES_DB: aerogear_mobile_metrics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// Simple helper function to read an environment or return a default value | ||
func getEnv(key string, defaultVal string) string { | ||
if value := os.Getenv(key); value != "" { | ||
return value | ||
} | ||
return defaultVal | ||
} | ||
|
||
func GetConfig() map[string]string { | ||
return map[string]string{ | ||
"DBHost": getEnv("PGHOST", "localhost"), | ||
"DBUser": getEnv("PGUSER", "postgres"), | ||
"DBPassword": getEnv("PGPASSWORD", "postgres"), | ||
"DBName": getEnv("PGDATABASE", "aerogear_mobile_metrics"), | ||
"SSLMode": getEnv("PGSSLMODE", "disable"), | ||
"ListenAddress": fmt.Sprintf(":%s", getEnv("PORT", "3000")), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestGetConfig(t *testing.T) { | ||
expected := map[string]string{ | ||
"DBHost": "localhost", | ||
"DBUser": "postgres", | ||
"DBPassword": "postgres", | ||
"DBName": "aerogear_mobile_metrics", | ||
"SSLMode": "disable", | ||
"ListenAddress": ":3000", | ||
} | ||
|
||
config := GetConfig() | ||
|
||
if !reflect.DeepEqual(config, expected) { | ||
t.Error("GetConfig() did not return expected result") | ||
} | ||
} | ||
|
||
func TestGetConfigCustomEnvVariables(t *testing.T) { | ||
expected := map[string]string{ | ||
"DBHost": "testing", | ||
"DBUser": "testing", | ||
"DBPassword": "testing", | ||
"DBName": "testing", | ||
"SSLMode": "testing", | ||
"ListenAddress": ":testing", | ||
} | ||
|
||
os.Setenv("PGHOST", "testing") | ||
os.Setenv("PGUSER", "testing") | ||
os.Setenv("PGPASSWORD", "testing") | ||
os.Setenv("PGDATABASE", "testing") | ||
os.Setenv("PGSSLMODE", "testing") | ||
os.Setenv("PORT", "testing") | ||
|
||
config := GetConfig() | ||
|
||
if !reflect.DeepEqual(config, expected) { | ||
t.Error("GetConfig() did not return expected result") | ||
} | ||
} | ||
|
||
func TestGetConfigEmptyEnvVariables(t *testing.T) { | ||
expected := map[string]string{ | ||
"DBHost": "localhost", | ||
"DBUser": "postgres", | ||
"DBPassword": "postgres", | ||
"DBName": "aerogear_mobile_metrics", | ||
"SSLMode": "disable", | ||
"ListenAddress": ":3000", | ||
} | ||
|
||
os.Setenv("PGHOST", "") | ||
os.Setenv("PGUSER", "") | ||
os.Setenv("PGPASSWORD", "") | ||
os.Setenv("PGDATABASE", "") | ||
os.Setenv("PGSSLMODE", "") | ||
os.Setenv("PORT", "") | ||
|
||
config := GetConfig() | ||
|
||
if !reflect.DeepEqual(config, expected) { | ||
t.Error("GetConfig() did not return expected result") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.