Skip to content

Commit

Permalink
Merge pull request #16 from hellofresh/feature/config
Browse files Browse the repository at this point in the history
Fixing the config
  • Loading branch information
kieranajp authored Nov 18, 2016
2 parents 8dec04a + b624a56 commit 00974c3
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 163 deletions.
7 changes: 4 additions & 3 deletions api_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/etcinit/speedbump"
"github.com/gin-gonic/gin"
"github.com/hellofresh/ginger-middleware/mongodb"
"gopkg.in/redis.v3"
)

// APIManager handles the creation and configuration of an api definition
type APIManager struct {
proxyRegister *ProxyRegister
redisClient *redis.Client
accessor *DatabaseAccessor
accessor *mongodb.DatabaseAccessor
}

// NewAPIManager creates a new instance of the api manager
func NewAPIManager(router *gin.Engine, redisClient *redis.Client, accessor *DatabaseAccessor) *APIManager {
func NewAPIManager(router *gin.Engine, redisClient *redis.Client, accessor *mongodb.DatabaseAccessor) *APIManager {
proxyRegister := &ProxyRegister{Engine: router}
return &APIManager{proxyRegister, redisClient, accessor}
}
Expand All @@ -42,7 +43,7 @@ func (m *APIManager) LoadApps(apiSpecs []*APISpec) {
skip = m.validateProxy(referenceSpec.Proxy)

if false == referenceSpec.Active {
logger.Info("API is not active, skiping...")
log.Info("API is not active, skiping...")
skip = false
}

Expand Down
9 changes: 0 additions & 9 deletions config.go

This file was deleted.

22 changes: 22 additions & 0 deletions config/specification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package config

import "github.com/kelseyhightower/envconfig"

// Specification for basic configurations
type Specification struct {
DatabaseDSN string `envconfig:"DATABASE_DSN"`
Port int `envconfig:"PORT"`
Debug bool `envconfig:"DEBUG"`
StorageDSN string `envconfig:"REDIS_DSN"`
}

//LoadEnv loads environment variables
func LoadEnv() (*Specification, error) {
var config Specification
err := envconfig.Process("", &config)
if err != nil {
return nil, err
}

return &config, nil
}
33 changes: 0 additions & 33 deletions database_accessor.go

This file was deleted.

6 changes: 0 additions & 6 deletions errors.go

This file was deleted.

14 changes: 14 additions & 0 deletions errors/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package errors

type Error struct {
Code int
message string
}

func New(code int, message string) *Error {
return &Error{code, message}
}

func (e Error) Error() string {
return e.message
}
72 changes: 38 additions & 34 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package: github.com/hellofresh/api-gateway
license: MIT
owners:
- name: Italo Lelis de Vietro
email: [email protected]
import:
- package: github.com/RangelReale/osin
- package: github.com/Sirupsen/logrus
version: ^0.10.0
- package: github.com/asaskevich/govalidator
version: ^4.0.0
- package: github.com/etcinit/speedbump
version: ^0.2.0
- package: github.com/gin-gonic/gin
version: 9163ee543d3f1fab9b0ad8cdf6dc2a6ec2c07dbb
- package: github.com/gorilla/context
version: ^1.1.0
version: v1.0-rc.2
- package: github.com/kelseyhightower/envconfig
version: ^1.1.0
- package: gopkg.in/mgo.v2
Expand All @@ -20,5 +21,7 @@ import:
version: ^3.6.4
- package: github.com/itsjamie/gin-cors
version: ^1.0.0
- package: github.com/hellofresh/ginger-middleware
testImport:
- package: github.com/onsi/ginkgo
version: ^1.2.0
version: ^1.2.0
37 changes: 13 additions & 24 deletions handler_api_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"net/http"

log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/hellofresh/janus/errors"
"gopkg.in/mgo.v2"
)

Expand All @@ -16,11 +16,10 @@ type AppsAPI struct {
func (u *AppsAPI) Get() gin.HandlerFunc {
return func(c *gin.Context) {
repo := u.getRepository(u.getDatabase(c))
data, err := repo.FindAll()

data, err := repo.FindAll()
if err != nil {
log.Errorf(err.Error())
c.JSON(http.StatusInternalServerError, err.Error())
panic(err.Error())
}

c.JSON(http.StatusOK, data)
Expand All @@ -35,14 +34,11 @@ func (u *AppsAPI) GetBy() gin.HandlerFunc {

data, err := repo.FindByID(id)
if data.ID == "" {
c.JSON(http.StatusNotFound, "Application not found")
return
panic(errors.New(http.StatusNotFound, "Application not found"))
}

if err != nil {
log.Errorf(err.Error())
c.JSON(http.StatusInternalServerError, err.Error())
return
panic(errors.New(http.StatusInternalServerError, err.Error()))
}

c.JSON(http.StatusOK, data)
Expand All @@ -58,20 +54,16 @@ func (u *AppsAPI) PutBy() gin.HandlerFunc {
repo := u.getRepository(u.getDatabase(c))
definition, err := repo.FindByID(id)
if definition.ID == "" {
c.JSON(http.StatusNotFound, "Application not found")
return
panic(errors.New(http.StatusNotFound, "Application not found"))
}

if err != nil {
log.Errorf(err.Error())
c.JSON(http.StatusInternalServerError, err.Error())
return
panic(errors.New(http.StatusInternalServerError, err.Error()))
}

err = c.Bind(definition)
if nil != err {
log.Errorf("Error when reading json: %s", err.Error())
return
panic(errors.New(http.StatusInternalServerError, err.Error()))
}

repo.Add(definition)
Expand All @@ -89,13 +81,12 @@ func (u *AppsAPI) Post() gin.HandlerFunc {

err := c.Bind(definition)
if nil != err {
log.Fatal("Error when reading json")
panic(errors.New(http.StatusInternalServerError, err.Error()))
}

err = repo.Add(definition)
if nil != err {
c.JSON(http.StatusBadRequest, err)
return
panic(errors.New(http.StatusBadRequest, err.Error()))
}

u.apiManager.Load()
Expand All @@ -111,8 +102,7 @@ func (u *AppsAPI) DeleteBy() gin.HandlerFunc {

err := repo.Remove(id)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
panic(errors.New(http.StatusInternalServerError, err.Error()))
}

u.apiManager.Load()
Expand All @@ -124,7 +114,7 @@ func (u *AppsAPI) getDatabase(c *gin.Context) *mgo.Database {
db, exists := c.Get("db")

if false == exists {
log.Error("DB context was not set for this request")
panic(errors.New(http.StatusInternalServerError, "DB context was not set for this request"))
}

return db.(*mgo.Database)
Expand All @@ -133,9 +123,8 @@ func (u *AppsAPI) getDatabase(c *gin.Context) *mgo.Database {
// GetRepository gets the repository for the handlers
func (u *AppsAPI) getRepository(db *mgo.Database) *MongoAPISpecRepository {
repo, err := NewMongoAppRepository(db)

if err != nil {
log.Panic(err)
panic(errors.New(http.StatusInternalServerError, err.Error()))
}

return repo
Expand Down
Loading

0 comments on commit 00974c3

Please sign in to comment.