Skip to content

Commit

Permalink
Merge pull request #3 from ardityawahyu/adding-influx-to-handler
Browse files Browse the repository at this point in the history
adding influx client to handler option
  • Loading branch information
budiariyanto authored Dec 9, 2019
2 parents a99bbff + b47e83b commit 4e4cf5f
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 110 deletions.
20 changes: 11 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/kitabisa/buroq/config"
"github.com/kitabisa/buroq/internal/app/appcontext"
"github.com/kitabisa/buroq/internal/app/commons"
"github.com/kitabisa/buroq/internal/app/repository"
"github.com/kitabisa/buroq/internal/app/server"
"github.com/kitabisa/buroq/internal/app/service"
Expand Down Expand Up @@ -69,32 +70,33 @@ func start() {
return
}

repo := wiringRepository(repository.Option{
opt := commons.Options{
Config: cfg,
DbMysql: dbMysql,
DbPostgre: dbPostgre,
CachePool: cache,
Influx: influx,
Logger: logger,
}

repo := wiringRepository(repository.Option{
Options: opt,
})

service := wiringService(service.Option{
DbMysql: dbMysql,
DbPostgre: dbPostgre,
CachePool: cache,
Influx: influx,
Logger: logger,
Repo: repo,
Options: opt,
Repository: repo,
})

server := server.NewServer(cfg, service, dbMysql, dbPostgre, cache, logger)
server := server.NewServer(opt, service)

// run app
server.StartApp()
}

func wiringRepository(repoOption repository.Option) *repository.Repository {
// wiring up all your repos here
cacheRepo := repository.NewCacheRepository(repoOption.CachePool)
cacheRepo := repository.NewCacheRepository(repoOption)

repo := repository.Repository{
Cache: cacheRepo,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ require (
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.4.0
github.com/ziutek/mymysql v1.5.4 // indirect
gopkg.in/gorp.v1 v1.7.2 // indirect
gopkg.in/gorp.v1 v1.7.2
gopkg.in/gorp.v2 v2.0.0
)
18 changes: 18 additions & 0 deletions internal/app/commons/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package commons

import (
"github.com/gomodule/redigo/redis"
"github.com/kitabisa/buroq/config"
"github.com/kitabisa/perkakas/v2/log"
"github.com/kitabisa/perkakas/v2/metrics/influx"
"gopkg.in/gorp.v2"
)

type Options struct {
Config config.Provider
DbMysql *gorp.DbMap
DbPostgre *gorp.DbMap
CachePool *redis.Pool
Influx *influx.Client
Logger *log.Logger
}
13 changes: 3 additions & 10 deletions internal/app/handler/handler.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package handler

import (
"github.com/gomodule/redigo/redis"
"github.com/kitabisa/buroq/config"
"github.com/kitabisa/buroq/internal/app/commons"
"github.com/kitabisa/buroq/internal/app/service"
"github.com/kitabisa/perkakas/v2/log"
"gopkg.in/gorp.v2"
)

type HandlerOption struct {
Config config.Provider
Services *service.Service
DbMysql *gorp.DbMap
DbPostgre *gorp.DbMap
CachePool *redis.Pool
Logger *log.Logger
commons.Options
*service.Services
}
14 changes: 7 additions & 7 deletions internal/app/repository/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ type ICacheRepo interface {
}

type cacheRepo struct {
cachePool *redis.Pool
kmutex *kmutex.Kmutex
opt Option
kmutex *kmutex.Kmutex
}

// NewCacheRepository initiate cache repo
func NewCacheRepository(cachePool *redis.Pool) ICacheRepo {
func NewCacheRepository(opt Option) ICacheRepo {
return &cacheRepo{
cachePool: cachePool,
kmutex: kmutex.New(),
opt: opt,
kmutex: kmutex.New(),
}
}

Expand All @@ -32,7 +32,7 @@ func (c *cacheRepo) WriteCache(key string, data interface{}, ttl time.Duration)
defer c.kmutex.Unlock(key)

// write data to cache
conn := c.cachePool.Get()
conn := c.opt.CachePool.Get()
_, err = conn.Do("SETEX", key, ttl.Seconds(), data)
if err != nil {
return err
Expand All @@ -47,7 +47,7 @@ func (c *cacheRepo) WriteCacheIfEmpty(key string, data interface{}, ttl time.Dur
defer c.kmutex.Unlock(key)

// check whether cache value is empty
conn := c.cachePool.Get()
conn := c.opt.CachePool.Get()
_, err = conn.Do("GET", key)
if err != nil {
if err == redis.ErrNil {
Expand Down
13 changes: 2 additions & 11 deletions internal/app/repository/repository.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
package repository

import (
"github.com/gomodule/redigo/redis"
"github.com/kitabisa/perkakas/v2/log"
"github.com/kitabisa/perkakas/v2/metrics/influx"
"gopkg.in/gorp.v2"
)
import "github.com/kitabisa/buroq/internal/app/commons"

// Option anything any repo object needed
type Option struct {
DbMysql *gorp.DbMap
DbPostgre *gorp.DbMap
CachePool *redis.Pool
Influx *influx.Client
Logger *log.Logger
commons.Options
}

// Repository all repo object injected here
Expand Down
22 changes: 5 additions & 17 deletions internal/app/server/router.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
package server

import (
"net/http"

"github.com/go-chi/chi"
cmiddleware "github.com/go-chi/chi/middleware"
"github.com/gomodule/redigo/redis"
"github.com/kitabisa/buroq/config"
"github.com/kitabisa/buroq/internal/app/commons"
"github.com/kitabisa/buroq/internal/app/handler"
"github.com/kitabisa/buroq/internal/app/service"
"github.com/kitabisa/buroq/version"
phttp "github.com/kitabisa/perkakas/v2/http"
"github.com/kitabisa/perkakas/v2/log"
pmiddleware "github.com/kitabisa/perkakas/v2/middleware"
pstructs "github.com/kitabisa/perkakas/v2/structs"
"gopkg.in/gorp.v2"
"net/http"
)

// Router a chi mux
func Router(cfg config.Provider, service *service.Service, dbMysql *gorp.DbMap, dbPostgre *gorp.DbMap, cachePool *redis.Pool, logger *log.Logger) *chi.Mux {
func Router(opt handler.HandlerOption) *chi.Mux {
handlerCtx := phttp.NewContextHandler(pstructs.Meta{
Version: version.Version,
Status: "stable", //TODO: ask infra if this is used
APIEnv: version.Environment,
})
commons.InjectErrors(&handlerCtx)

logMiddleware := pmiddleware.NewHttpRequestLogger(logger)
logMiddleware := pmiddleware.NewHttpRequestLogger(opt.Logger)
// headerCheckMiddleware := pmiddleware.NewHeaderCheck(handlerCtx, cfg.GetString("app.secret"))

r := chi.NewRouter()
Expand All @@ -39,18 +35,10 @@ func Router(cfg config.Provider, service *service.Service, dbMysql *gorp.DbMap,

// the handler
phandler := phttp.NewHttpHandler(handlerCtx)
handlerOpt := handler.HandlerOption{
Config: cfg,
Services: service,
DbMysql: dbMysql,
DbPostgre: dbPostgre,
CachePool: cachePool,
Logger: logger,
}

healthCheckHandler := handler.HealthCheckHandler{}

healthCheckHandler.HandlerOption = handlerOpt
healthCheckHandler.HandlerOption = opt
healthCheckHandler.Handler = phandler(healthCheckHandler.HealthCheck)

// Setup your routing here
Expand Down
32 changes: 13 additions & 19 deletions internal/app/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import (
"os"
"os/signal"

"github.com/gomodule/redigo/redis"
"github.com/kitabisa/buroq/config"
"github.com/kitabisa/buroq/internal/app/commons"
"github.com/kitabisa/buroq/internal/app/handler"
"github.com/kitabisa/buroq/internal/app/service"
"github.com/kitabisa/perkakas/v2/log"
"github.com/sirupsen/logrus"
"gopkg.in/gorp.v2"
)

// IServer interface for server
Expand All @@ -21,23 +19,15 @@ type IServer interface {
}

type server struct {
config config.Provider
service *service.Service
dbMysql *gorp.DbMap
dbPostgre *gorp.DbMap
cachePool *redis.Pool
logger *log.Logger
opt commons.Options
services *service.Services
}

// NewServer create object server
func NewServer(config config.Provider, service *service.Service, dbMysql *gorp.DbMap, dbPostgre *gorp.DbMap, cachePool *redis.Pool, logger *log.Logger) IServer {
func NewServer(opt commons.Options, services *service.Services) IServer {
return &server{
config: config,
service: service,
dbMysql: dbMysql,
dbPostgre: dbPostgre,
cachePool: cachePool,
logger: logger,
opt: opt,
services: services,
}
}

Expand All @@ -59,8 +49,12 @@ func (s *server) StartApp() {
close(idleConnectionClosed)
}()

srv.Addr = fmt.Sprintf("%s:%d", s.config.GetString("app.host"), s.config.GetInt("app.port"))
srv.Handler = Router(s.config, s.service, s.dbMysql, s.dbPostgre, s.cachePool, s.logger)
srv.Addr = fmt.Sprintf("%s:%d", s.opt.Config.GetString("app.host"), s.opt.Config.GetInt("app.port"))
hOpt := handler.HandlerOption{
Options: s.opt,
Services: s.services,
}
srv.Handler = Router(hOpt)

logrus.Infof("[API] HTTP serve at %s\n", srv.Addr)

Expand Down
36 changes: 11 additions & 25 deletions internal/app/service/health_check.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package service

import (
"github.com/gomodule/redigo/redis"
"github.com/kitabisa/buroq/internal/app/commons"
"github.com/kitabisa/buroq/internal/app/repository"
"github.com/kitabisa/perkakas/v2/log"
"github.com/kitabisa/perkakas/v2/metrics/influx"
"gopkg.in/gorp.v2"
)

// IHealthCheck interface for health check service
Expand All @@ -18,49 +14,39 @@ type IHealthCheck interface {
}

type healthCheck struct {
Repo *repository.Repository
dbMysql *gorp.DbMap
dbPostgre *gorp.DbMap
cachePool *redis.Pool
influx *influx.Client
logger *log.Logger
opt Option
}

// NewHealthCheck create health check service instance with option as param
func NewHealthCheck(option Option) IHealthCheck {
func NewHealthCheck(opt Option) IHealthCheck {
return &healthCheck{
Repo: option.Repo,
dbMysql: option.DbMysql,
dbPostgre: option.DbPostgre,
cachePool: option.CachePool,
influx: option.Influx,
logger: option.Logger,
opt: opt,
}
}

func (h *healthCheck) HealthCheckDbMysql() (err error) {
err = h.dbMysql.Db.Ping()
err = h.opt.DbMysql.Db.Ping()
if err != nil {
h.logger.AddMessage(log.FatalLevel, err.Error())
h.opt.Logger.AddMessage(log.FatalLevel, err.Error())
err = commons.ErrDBConn
}
return
}

func (h *healthCheck) HealthCheckDbPostgres() (err error) {
err = h.dbPostgre.Db.Ping()
err = h.opt.DbPostgre.Db.Ping()
if err != nil {
h.logger.AddMessage(log.FatalLevel, err.Error())
h.opt.Logger.AddMessage(log.FatalLevel, err.Error())
err = commons.ErrDBConn
}
return
}

func (h *healthCheck) HealthCheckDbCache() (err error) {
cacheConn := h.cachePool.Get()
cacheConn := h.opt.CachePool.Get()
_, err = cacheConn.Do("PING")
if err != nil {
h.logger.AddMessage(log.FatalLevel, err.Error())
h.opt.Logger.AddMessage(log.FatalLevel, err.Error())
err = commons.ErrCacheConn
return
}
Expand All @@ -70,9 +56,9 @@ func (h *healthCheck) HealthCheckDbCache() (err error) {
}

func (h *healthCheck) HealthCheckInflux() (err error) {
err = h.influx.Ping()
err = h.opt.Influx.Ping()
if err != nil {
h.logger.AddMessage(log.FatalLevel, err.Error())
h.opt.Logger.AddMessage(log.FatalLevel, err.Error())
err = commons.ErrInfluxConn
}

Expand Down
15 changes: 4 additions & 11 deletions internal/app/service/service.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
package service

import (
"github.com/gomodule/redigo/redis"
"github.com/kitabisa/buroq/internal/app/commons"
"github.com/kitabisa/buroq/internal/app/repository"
"github.com/kitabisa/perkakas/v2/log"
"github.com/kitabisa/perkakas/v2/metrics/influx"
"gopkg.in/gorp.v2"
)

// Option anything any service object needed
type Option struct {
DbMysql *gorp.DbMap
DbPostgre *gorp.DbMap
CachePool *redis.Pool
Repo *repository.Repository
Influx *influx.Client
Logger *log.Logger
commons.Options
*repository.Repository
}

// Service all service object injected here
type Service struct {
type Services struct {
HealthCheck IHealthCheck
}

0 comments on commit 4e4cf5f

Please sign in to comment.