Skip to content

Commit

Permalink
fix: Suggested changes (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
maleck13 authored and Dara Hayes committed Feb 16, 2018
1 parent 0d90d34 commit 171fc7a
Show file tree
Hide file tree
Showing 26 changed files with 243 additions and 240 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor/
.idea/
File renamed without changes.
10 changes: 0 additions & 10 deletions aerogear_metrics_api_test.go

This file was deleted.

35 changes: 35 additions & 0 deletions cmd/metrics-api/metrics-server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"github.com/aerogear/aerogear-metrics-api/pkg/web"
"github.com/aerogear/aerogear-metrics-api/pkg/dao"
"github.com/aerogear/aerogear-metrics-api/pkg/mobile"
"net/http"
)

func main() {
router := web.NewRouter()
db, err := dao.Connect()
if err != nil{
panic("failed to connect to sql database : "+ err.Error())
}
defer db.Close()
metricDao := dao.NewMetricsDAO(db)

//metrics route
{
metricsService := mobile.NewMetricsService(metricDao)
metricsHandler := web.NewMetricsHandler(metricsService)
web.MetricsRoute(router,metricsHandler)
}
//health route
{
healthHandler := web.NewHealthHandler()
web.HealthzRoute(router,healthHandler)
}

//start
if err := http.ListenAndServe(":3000",router); err != nil{
panic("failed to start " + err.Error())
}
}
17 changes: 7 additions & 10 deletions pkg/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@ package dao

import (
"errors"

"github.com/aerogear/aerogear-metrics-api/pkg/models"
"database/sql"
"github.com/aerogear/aerogear-metrics-api/pkg/mobile"
)

type MetricsDAO struct {
DBConnectionString string
db *sql.DB
}

// Connect to database
func (m *MetricsDAO) Connect() {

}

// Create a metrics record
func (m *MetricsDAO) Create(metric models.Metric) (models.Metric, error) {
func (m *MetricsDAO) Create(metric mobile.Metric) (mobile.Metric, error) {
return metric, errors.New("Not Implemented yet")
}

Expand All @@ -38,8 +35,8 @@ func (m *MetricsDAO) CheckConnection() {

}

func GetMetricsDAO(connectionString string) MetricsDAO {
return MetricsDAO{
DBConnectionString: connectionString,
func NewMetricsDAO(db *sql.DB) *MetricsDAO {
return &MetricsDAO{
db:db,
}
}
21 changes: 21 additions & 0 deletions pkg/dao/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dao

import "database/sql"


var db *sql.DB

func Connect()(*sql.DB,error){
if db != nil{
return db,nil
}
//connection logic
return nil,nil
}

func Disconnect()error{
if nil != db{
return db.Close()
}
return nil
}
36 changes: 0 additions & 36 deletions pkg/handlers/handlers.go

This file was deleted.

17 changes: 0 additions & 17 deletions pkg/handlers/healthzHandler.go

This file was deleted.

17 changes: 0 additions & 17 deletions pkg/handlers/helpers.go

This file was deleted.

32 changes: 0 additions & 32 deletions pkg/handlers/metricsHandler.go

This file was deleted.

19 changes: 0 additions & 19 deletions pkg/metrics/app.go

This file was deleted.

13 changes: 0 additions & 13 deletions pkg/metrics/health.go

This file was deleted.

16 changes: 0 additions & 16 deletions pkg/metrics/metrics.go

This file was deleted.

5 changes: 0 additions & 5 deletions pkg/metrics/types.go

This file was deleted.

7 changes: 7 additions & 0 deletions pkg/mobile/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package mobile


// MetricCreator defines how a metric can be created
type MetricCreator interface{
Create(m Metric)(Metric,error)
}
20 changes: 20 additions & 0 deletions pkg/mobile/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package mobile

import (
"errors"

)

type MetricsService struct {
mdao MetricCreator
}

func NewMetricsService(dao MetricCreator)*MetricsService{
return &MetricsService{mdao:dao}
}



func (m MetricsService) Create(metric Metric) (Metric, error) {
return metric, errors.New("This is an error")
}
10 changes: 7 additions & 3 deletions pkg/models/metric.go → pkg/mobile/types.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package models
package mobile

import "time"

// Metric struct is what the client payload should be parsed into
type AppConfig struct {
DBConnectionString string
}

// ClientMetric struct is what the client payload should be parsed into
// Need to figure out how to structure this
type Metric struct {
Timestamp time.Time `json:"timestamp"`
ClientID string `json:"clientId"`
Data interface{} `json:"data"`
}
}
14 changes: 0 additions & 14 deletions pkg/models/app.go

This file was deleted.

11 changes: 0 additions & 11 deletions pkg/models/healthz.go

This file was deleted.

39 changes: 39 additions & 0 deletions pkg/web/healthzHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package web

import (
"net/http"
"time"


)

// not reusable type so make it package local

type healthResponse struct {
Timestamp time.Time `json:"time"`
Status string `json:"status"`
}

type healthHandler struct{

}

func NewHealthHandler()*healthHandler{
return &healthHandler{}
}

func (hh *healthHandler) Healthz(w http.ResponseWriter, r *http.Request) {
status := healthResponse{
Timestamp: time.Now().UTC(),
Status: "ok",
}

if err := withJSON(w, 200, status); err != nil{
http.Error(w,err.Error(),http.StatusInternalServerError)
return
}
}

func (hh *healthHandler)Ping(w http.ResponseWriter, r *http.Request) {

}
Loading

0 comments on commit 171fc7a

Please sign in to comment.