Skip to content

Commit

Permalink
Isolate stage interface, reader function rename and refactoring, more…
Browse files Browse the repository at this point in the history
… tests and coverage, deps update, throw out config structs
  • Loading branch information
smgladkovskiy committed Mar 10, 2021
1 parent a7d1891 commit d735ec3
Show file tree
Hide file tree
Showing 28 changed files with 462 additions and 1,222 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
38 changes: 21 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,32 +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
# ----

# ----
## TEST stuff start
## TESTS stuff start

test-unit:
go test -cover -race -count=1 -timeout 1s -coverprofile=c.out -v ./... && go tool cover -func=c.out
.PHONY: test-unit
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)

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

test: deps test-unit coverage-html
.PHONY: test
tests_html: tests_include_check
@make -f tests.mk go_tests_html
.PHONY: tests

## TEST stuff end
## 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.
7 changes: 0 additions & 7 deletions cfg_auth.go

This file was deleted.

129 changes: 0 additions & 129 deletions cfg_database.go

This file was deleted.

14 changes: 0 additions & 14 deletions cfg_internal_service.go

This file was deleted.

Loading

0 comments on commit d735ec3

Please sign in to comment.