Skip to content

Commit

Permalink
Release v1.2.0
Browse files Browse the repository at this point in the history
* deps update, some more tests, config structs
* Move back to yaml v2
* error reporting
* Isolate stage interface
* reader function rename and refactoring
* tests and linters
* deps update
* throw out config structs
  • Loading branch information
smgladkovskiy authored Mar 10, 2021
1 parent eb963ed commit 5100f0f
Show file tree
Hide file tree
Showing 22 changed files with 569 additions and 200 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
docker:
- image: circleci/golang:1.14
environment:
- IN_CONTAINER: true
IN_CONTAINER: true
steps:
- checkout
- run: go mod vendor
Expand All @@ -23,7 +23,7 @@ jobs:
sudo mkdir -p /cfgs/dev
sudo cp config_examples/configuration/defaults/* /cfgs/defaults
sudo cp config_examples/configuration/dev/* /cfgs/dev
make test
make tests_html
mv coverage.html /tmp/artifacts
mv c.out /tmp/artifacts
- store_artifacts:
Expand Down
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
coverage.out
.idea
vendor

bin
c.out
coverage.html
.golangci.yml
.golangci.yml
linter.mk
tests.mk
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 SpaceTab.io
Copyright (c) 2021 SpaceTab.io

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
40 changes: 25 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
deps: ## Get the dependencies
@go mod vendor

## Test stuff
get_lint_config:
@[ -f ./.golangci.yml ] && echo ".golangci.yml exists" || ( echo "getting .golangci.yml" && curl -O https://raw.githubusercontent.com/spacetab-io/docker-images-golang/master/linter/.golangci.yml )
.PHONY: get_lint_config
# ----
## LINTER stuff start

linter_include_check:
@[ -f linter.mk ] && echo "linter.mk include exists" || (echo "getting linter.mk from github.com" && curl -sO https://raw.githubusercontent.com/spacetab-io/makefiles/master/golang/linter.mk)

lint: get_lint_config
golangci-lint run -v
.PHONY: lint
lint: linter_include_check
@make -f linter.mk go_lint

## LINTER stuff end
# ----

# ----
## TESTS stuff start

tests_include_check:
@[ -f tests.mk ] && echo "tests.mk include exists" || (echo "getting tests.mk from github.com" && curl -sO https://raw.githubusercontent.com/spacetab-io/makefiles/master/golang/tests.mk)

test-unit:
go test ./... --race --cover -count=1 -timeout 1s -coverprofile=c.out -v
.PHONY: test-unit
tests: tests_include_check
@make -f tests.mk go_tests
.PHONY: tests

coverage-html:
go tool cover -html=c.out -o coverage.html
.PHONE: coverage-html
tests_html: tests_include_check
@make -f tests.mk go_tests_html
.PHONY: tests

test: deps test-unit coverage-html
.PHONY: test
## TESTS stuff end
# ----

tests_in_docker: ## Testing code with unit tests in docker container
docker run --rm -v $(shell pwd):/app -i spacetabio/docker-test-golang:1.14-1.0.2 make test
docker run --rm -v $(shell pwd):/app -i spacetabio/docker-test-golang:1.14-1.0.2 make tests
.PHONY: tests_in_docker
82 changes: 43 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,46 @@ Golang Microservice configuration module

[![CircleCI](https://circleci.com/gh/spacetab-io/configuration-go.svg?style=shield)](https://circleci.com/gh/spacetab-io/configuration-go) [![codecov](https://codecov.io/gh/spacetab-io/configuration-go/graph/badge.svg)](https://codecov.io/gh/spacetab-io/configuration-go)


Configuration module for microservices written on Go. Preserves [corporate standards for services configuration](https://confluence.teamc.io/pages/viewpage.action?pageId=4227704).
Configuration module for microservices written on Go.
Preserves [corporate standards for services configuration](https://confluence.teamc.io/pages/viewpage.action?pageId=4227704).

## Installation

Import in your configuration file

import (
"github.com/spacetab-io/configuration-go"
)
```go
package main

import (
config "github.com/spacetab-io/configuration-go"
)

```

## Usage

Some agreements:

1. Configuration must be declared as struct and reveals yaml structure
2. Default config folder: `./configuration`. If you need to override, pass your path in `ReadConfig` function
3. Default stage is `development`. To override, set `STAGE` env variable
3. Stage is passed as `stage.Interface` implementation. In example below stageEnv is used to pass stage through env variable `STAGE`.

Code example:

```go
package main

import (
"fmt"
"log"

"github.com/spacetab-io/configuration-go"
config "github.com/spacetab-io/configuration-go"
"github.com/spacetab-io/configuration-go/stage"
"gopkg.in/yaml.v2"
)


// Your app config structure. This must be related to yaml config file structure. Everything that is not
// in this struct will be passed and will not be processed.
// ConfigStruct is your app config structure. This must be related to yaml config file structure.
// Everything that is not in this struct will be passed and will not be processed.
// Keep in mind that inheritance must be implemented with `struct{}`
type ConfigStruct struct {
Log struct {
Expand All @@ -62,43 +68,41 @@ type ConfigStruct struct {
}

func main() {
// Reader accept config path as param. Commonly it stored like STAGE in ENV.
configPath := config.GetEnv("CONFIG_PATH", "./configuration")
// config.Read receives stage as stage.Interface implementation.
// You can use envStage to pass stage name via ENV param STAGE.
// In NewEnvStage you can pass fallback value if STAGE param is empty.
envStage := stage.NewEnvStage("development")
// Reading ALL config files in defaults configuration folder and recursively merge them with STAGE configs
configBytes, err := config.ReadConfigs(configPath)
configBytes, err := config.Read(envStage, "./configuration", true)
if err != nil {
log.Fatalf("config reading error: %+v", err)
}

var Config ConfigStruct
// unmarshal config into Config structure
err = yaml.Unmarshal(configBytes, &Config)
cfg := ConfigStruct{}
// unmarshal config into Config structure
err = yaml.Unmarshal(configBytes, &cfg)
if err != nil {
log.Fatalf("config unmarshal error: %+v", err)
}
log.Fatalf("config unmarshal error: %+v", err)
}

fmt.Printf("config: %+v", cfg)
}
```

## License

The MIT License

Copyright © 2020 SpaceTab.io, Inc. https://spacetab.io

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.
Copyright © 2021 SpaceTab.io, Inc. https://spacetab.io

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.
14 changes: 14 additions & 0 deletions config_examples/configuration/local/log.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local:
log:
level: "debug"
format: "русский мат"
debug: true
port: "6666"
host: "0.0.0.0"
string_test: not a simple string
bool_test: false
redis:
hostname: 127.1.1.1
password: "very-very-secure-password-with-a-lot-of-words-to-make-sure-that-it-length-is-more-than-100-chars-length"
database: 123
port: 321
Empty file removed config_examples/go.mod
Empty file.
25 changes: 25 additions & 0 deletions defaults/db.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defaults:
db:
driver: postgres
connections:
- host: 127.0.0.1
port: 5432
action: read
- host: 127.0.0.1
port: 5433
action: write
user: postgres
password: ""
schema: public
name: database_name
ssl_mode: disable
extensions:
- hstore
max_idle_connections: 0
max_open_connections: 5
connection_lifetime: 30m
seeds_path: ./db/seeds/
migrations_path: ./db/migrations/
migrations_table: migrations
migrate_on_start: false
debug: true
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/spacetab-io/configuration-go
go 1.14

require (
github.com/imdario/mergo v0.3.10-0.20200517151347-9080aaff8536
github.com/stretchr/testify v1.5.1
gopkg.in/yaml.v2 v2.3.0
github.com/imdario/mergo v0.3.12
github.com/stretchr/testify v1.7.0
gopkg.in/yaml.v2 v2.4.0
)
13 changes: 8 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/imdario/mergo v0.3.10-0.20200517151347-9080aaff8536 h1:X5Ang7dBiLOoIANjgXiArEGrPWSjFQdaUjtsV9+gC5I=
github.com/imdario/mergo v0.3.10-0.20200517151347-9080aaff8536/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1 change: 1 addition & 0 deletions logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package config
Loading

0 comments on commit 5100f0f

Please sign in to comment.