diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..713459c --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,26 @@ +# Golang CircleCI 2.0 configuration file +# +# Check https://circleci.com/docs/2.0/language-go/ for more details +version: 2 +jobs: + build: + docker: + # specify the version + - image: circleci/golang:1.13 + + # Specify service dependencies here if necessary + # CircleCI maintains a library of pre-built images + # documented at https://circleci.com/docs/2.0/circleci-images/ + # - image: circleci/postgres:9.4 + + #### TEMPLATE_NOTE: go expects specific checkout path representing url + #### expecting it in the form of + #### /go/src/github.com/circleci/go-tool + #### /go/src/bitbucket.org/circleci/go-tool + working_directory: /go/src/github.com/kitabisa/buroq + steps: + - checkout + + # specify any bash command here prefixed with `run: ` + - run: go get -v -t -d ./... + - run: go test -v ./... \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ca0fdb2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Kitabisa + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 0f201b1..7d33dfb 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +[![Go Report Card](https://goreportcard.com/badge/github.com/kitabisa/buroq)](https://goreportcard.com/report/github.com/kitabisa/buroq) +![technology:go](https://img.shields.io/badge/technology-go-blue.svg) +[![Maintainability](https://api.codeclimate.com/v1/badges/b6e84c6b2bb2a819b198/maintainability)](https://codeclimate.com/github/kitabisa/buroq/maintainability) + # buroq this is a bootstrap service build with GO for our cookie cutter. diff --git a/cmd/root.go b/cmd/root.go index 2eb1389..e65d3dc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "github.com/gomodule/redigo/redis" "github.com/kitabisa/buroq/config" "github.com/kitabisa/buroq/internal/app/appcontext" "github.com/kitabisa/buroq/internal/app/commons" @@ -11,8 +12,10 @@ import ( "github.com/kitabisa/buroq/internal/app/server" "github.com/kitabisa/buroq/internal/app/service" "github.com/kitabisa/perkakas/v2/log" + "github.com/kitabisa/perkakas/v2/metrics/influx" "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "gopkg.in/gorp.v2" ) // rootCmd represents the base command when called without any subcommands @@ -44,30 +47,44 @@ func start() { logger := log.NewLogger("buroq") app := appcontext.NewAppContext(cfg) - dbMysql, err := app.GetDBInstance(appcontext.DBDialectMysql) - if err != nil { - logrus.Fatalf("Failed to start, error connect to DB MySQL | %v", err) - return + var err error + + var dbMysql *gorp.DbMap + if cfg.GetBool("mysql.is_enabled") { + dbMysql, err = app.GetDBInstance(appcontext.DBDialectMysql) + if err != nil { + logrus.Fatalf("Failed to start, error connect to DB MySQL | %v", err) + return + } } - dbPostgre, err := app.GetDBInstance(appcontext.DBDialectPostgres) - if err != nil { - logrus.Fatalf("Failed to start, error connect to DB Postgre | %v", err) - return + var dbPostgre *gorp.DbMap + if cfg.GetBool("postgre.is_enabled") { + dbPostgre, err = app.GetDBInstance(appcontext.DBDialectPostgres) + if err != nil { + logrus.Fatalf("Failed to start, error connect to DB Postgre | %v", err) + return + } } - cache := app.GetCachePool() - cacheConn, err := cache.Dial() - if err != nil { - logrus.Fatalf("Failed to start, error connect to DB Cache | %v", err) - return + var cache *redis.Pool + if cfg.GetBool("cache.is_enabled") { + cache = app.GetCachePool() + cacheConn, err := cache.Dial() + if err != nil { + logrus.Fatalf("Failed to start, error connect to DB Cache | %v", err) + return + } + defer cacheConn.Close() } - defer cacheConn.Close() - influx, err := app.GetInfluxDBClient() - if err != nil { - logrus.Fatalf("Failed to start, error connect to DB Influx | %v", err) - return + var influx *influx.Client + if cfg.GetBool("influx.is_enabled") { + influx, err = app.GetInfluxDBClient() + if err != nil { + logrus.Fatalf("Failed to start, error connect to DB Influx | %v", err) + return + } } opt := commons.Options{ @@ -105,11 +122,11 @@ func wiringRepository(repoOption repository.Option) *repository.Repository { return &repo } -func wiringService(serviceOption service.Option) *service.Service { +func wiringService(serviceOption service.Option) *service.Services { // wiring up all services hc := service.NewHealthCheck(serviceOption) - svc := service.Service{ + svc := service.Services{ HealthCheck: hc, } diff --git a/internal/app/commons/options.go b/internal/app/commons/options.go index d2a93dc..b535269 100644 --- a/internal/app/commons/options.go +++ b/internal/app/commons/options.go @@ -8,6 +8,7 @@ import ( "gopkg.in/gorp.v2" ) +// Options common option for all object that needed type Options struct { Config config.Provider DbMysql *gorp.DbMap diff --git a/internal/app/driver/mysql.go b/internal/app/driver/mysql.go index e859481..38c2b22 100644 --- a/internal/app/driver/mysql.go +++ b/internal/app/driver/mysql.go @@ -9,6 +9,7 @@ import ( "gopkg.in/gorp.v2" ) +// DBMysqlOption options for mysql connection type DBMysqlOption struct { Host string Port int @@ -21,6 +22,7 @@ type DBMysqlOption struct { ConnMaxLifetime time.Duration } +// NewMysqlDatabase return gorp dbmap object with MySQL options param func NewMysqlDatabase(option DBMysqlOption) (*gorp.DbMap, error) { db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?%s", option.Username, option.Password, option.Host, option.Port, option.DBName, option.AdditionalParameters)) if err != nil { diff --git a/internal/app/driver/postgre.go b/internal/app/driver/postgre.go index d68bd93..219cc14 100644 --- a/internal/app/driver/postgre.go +++ b/internal/app/driver/postgre.go @@ -8,6 +8,7 @@ import ( "gopkg.in/gorp.v2" ) +// DBPostgreOption options for postgre connection type DBPostgreOption struct { Host string Port int @@ -17,6 +18,7 @@ type DBPostgreOption struct { MaxPoolSize int } +// NewPostgreDatabase return gorp dbmap object with postgre options param func NewPostgreDatabase(option DBPostgreOption) (*gorp.DbMap, error) { db, err := sql.Open("postgres", fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s sslmode=disable", option.Host, option.Port, option.Username, option.DBName, option.Password)) if err != nil { diff --git a/internal/app/handler/handler.go b/internal/app/handler/handler.go index d1c113d..20838e0 100644 --- a/internal/app/handler/handler.go +++ b/internal/app/handler/handler.go @@ -5,6 +5,7 @@ import ( "github.com/kitabisa/buroq/internal/app/service" ) +// HandlerOption option for handler, including all service type HandlerOption struct { commons.Options *service.Services diff --git a/internal/app/handler/health_check.go b/internal/app/handler/health_check.go index c045ce5..a3d3cdc 100644 --- a/internal/app/handler/health_check.go +++ b/internal/app/handler/health_check.go @@ -4,6 +4,7 @@ import ( "net/http" ) +// HealthCheckHandler object for health check handler type HealthCheckHandler struct { HandlerOption http.Handler @@ -11,24 +12,32 @@ type HealthCheckHandler struct { // HealthCheck checking if all work well func (h HealthCheckHandler) HealthCheck(w http.ResponseWriter, r *http.Request) (data interface{}, pageToken *string, err error) { - err = h.Services.HealthCheck.HealthCheckDbMysql() - if err != nil { - return + if h.HandlerOption.Config.GetBool("mysql.is_enabled") { + err = h.Services.HealthCheck.HealthCheckDbMysql() + if err != nil { + return + } } - err = h.Services.HealthCheck.HealthCheckDbPostgres() - if err != nil { - return + if h.HandlerOption.Config.GetBool("postgre.is_enabled") { + err = h.Services.HealthCheck.HealthCheckDbPostgres() + if err != nil { + return + } } - err = h.Services.HealthCheck.HealthCheckDbCache() - if err != nil { - return + if h.HandlerOption.Config.GetBool("cache.is_enabled") { + err = h.Services.HealthCheck.HealthCheckDbCache() + if err != nil { + return + } } - err = h.Services.HealthCheck.HealthCheckInflux() - if err != nil { - return + if h.HandlerOption.Config.GetBool("influx.is_enabled") { + err = h.Services.HealthCheck.HealthCheckInflux() + if err != nil { + return + } } return diff --git a/internal/app/service/service.go b/internal/app/service/service.go index 58d7870..99e0298 100644 --- a/internal/app/service/service.go +++ b/internal/app/service/service.go @@ -11,7 +11,7 @@ type Option struct { *repository.Repository } -// Service all service object injected here +// Services all service object injected here type Services struct { HealthCheck IHealthCheck } diff --git a/params/buroq.toml.sample b/params/buroq.toml.sample index 9515114..46a9ac4 100644 --- a/params/buroq.toml.sample +++ b/params/buroq.toml.sample @@ -5,6 +5,7 @@ name = "buroq" secret = "any_secret" [postgre] +is_enabled = false host = "your_db_host" port = 5432 name = "buroq" @@ -13,6 +14,7 @@ password = "your_password" pool_size = 5 [mysql] +is_enabled = false host = "your_db_host" port = 3306 name = "buroq" @@ -24,6 +26,7 @@ conn_lifetime_max = "0s" #maximum duration connection lifetime max additional_parameters = "charset=utf8&parseTime=True&loc=Asia%2fJakarta&time_zone=%27%2B07%3A00%27" [cache] +is_enabled = false host = "your_cache_host" port = 6379 dial_connect_timeout = "5s" @@ -41,6 +44,7 @@ locker_retry_delay = "1s" locker_expiry = "5s" [influx] +is_enabled = false host = "your_influx_host" user = "your_username" pass = "your_password"