Skip to content

Commit 62e0c92

Browse files
authored
feat: change config management from env to yaml (#9)
* disable test caching * switch config from env to yaml * update readme to match the new changes about config file * add production ready docker compose file * force recreate of container with docker compose * update CI workflow * update CI workflow * update CI workflow * update CI workflow
1 parent 7dbbbfe commit 62e0c92

File tree

17 files changed

+155
-91
lines changed

17 files changed

+155
-91
lines changed

.dockerignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
.env.test
2-
*.env
2+
*.env
3+
config.yaml
4+
config
5+
/config.example.yaml
6+
/config.test.example.yaml

.env.example

Lines changed: 0 additions & 2 deletions
This file was deleted.

.env.test.example

Lines changed: 0 additions & 7 deletions
This file was deleted.

.github/workflows/CI.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ jobs:
2222
go-version: ${{ env.GO_VERSION }}
2323
- name: Run Tests
2424
run: |
25-
(
26-
echo BOT_TOKEN=${{ secrets.CI_BOT_TOKEN_1 }}
27-
echo BOT_TOKENS=${{ secrets.CI_BOT_TOKEN_1 }},${{ secrets.CI_BOT_TOKEN_2 }},${{ secrets.CI_BOT_TOKEN_3 }}
28-
echo TOKENS=${{ secrets.CI_BOT_TOKEN_1 }},${{ secrets.CI_BOT_TOKEN_2 }},${{ secrets.CI_BOT_TOKEN_3 }}
29-
echo DRAFT_CHAT_ID=${{ secrets.CI_DRAFT_CHAT_ID }}
30-
echo CHAT_ID=${{ secrets.CI_CHAT_ID }}
31-
) > .env
32-
cp .env .env.test
25+
mkdir config
26+
echo "tokens:" >./config-test/config.yaml
27+
echo " - "${{ secrets.CI_BOT_TOKEN_1 }}"" >> ./config-test/config.yaml
28+
echo " - "${{ secrets.CI_BOT_TOKEN_2 }}"" >> ./config-test/config.yaml
29+
echo " - "${{ secrets.CI_BOT_TOKEN_3 }}"" >> ./config-test/config.yaml
30+
echo "api_key: "" " >> ./config-test/config.yaml
31+
echo "chat_id: ${{ secrets.CI_CHAT_ID }}" >> ./config-test/config.yaml
32+
echo "draft_chat_id: ${{ secrets.CI_DRAFT_CHAT_ID }}" >> ./config-test/config.yaml
3333
make test
3434
chmod 777 ./scripts/wait-for-it/wait-for-it.sh
3535
make docker-e2etest

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
.idea
22
.env.test
3-
*.env
3+
*.env
4+
config.yaml
5+
config.test.yaml
6+
config

Dockerfile

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,23 @@ COPY . .
1414
RUN go build -o bin/tg-bot-storage ./cmd/main.go
1515

1616

17-
RUN apk add --no-cache ca-certificates
17+
#RUN apk add --no-cache ca-certificates
1818

1919
# build image with the binary
20-
FROM scratch
20+
FROM alpine
2121

22+
RUN apk add --no-cache ca-certificates
23+
RUN apk add bash
2224
# copy certificate to be able to make https request to telegram
23-
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
25+
#COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
26+
27+
WORKDIR /app
2428

2529
# copy the binary
26-
COPY --from=build /build/bin/tg-bot-storage /
30+
COPY --from=build /build/bin/tg-bot-storage /app/
31+
32+
VOLUME /app/config
2733

2834
EXPOSE 7000
2935

30-
ENTRYPOINT ["/tg-bot-storage"]
36+
ENTRYPOINT ["/app/tg-bot-storage"]

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ APPNAME=tg-bot-storage
44
## test: run tests on cmd and pkg files.
55
.PHONY: test
66
test: vet fmt
7-
CI="false" go test ./...
7+
CI="false" go test -v -count=1 ./...
88

99
## build: build application binary.
1010
.PHONY: build
@@ -28,7 +28,7 @@ e2etest:
2828

2929
## docker-e2etest: run e2etests in a docker compose
3030
docker-e2etest:
31-
docker-compose -f docker-compose.test.yml up --abort-on-container-exit --exit-code-from e2etests
31+
docker-compose -f docker-compose.test.yml up --force-recreate --abort-on-container-exit --exit-code-from e2etests
3232

3333
## docker-build: build the api docker image
3434
.PHONY: docker-build

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
# Telegram Bot Storage(in development)
2+
23
![Build Status](https://github.com/dipandaaser/tg-bot-storage/workflows/CI/badge.svg)
34
[![License](https://img.shields.io/github/license/dipandaaser/tg-bot-storage)](LICENSE)
45
[![Release](https://img.shields.io/github/release/dipandaaser/tg-bot-storage.svg)](https://github.com/dipandaaser/tg-bot-storage/releases/latest)
56
[![GitHub Releases Stats of tg-bot-storage](https://img.shields.io/github/downloads/dipandaaser/tg-bot-storage/total.svg?logo=github)](https://somsubhra.github.io/github-release-stats/?username=dipandaaser&repository=tg-bot-storage)
67

7-
Telegram Bot Storage is a simple library for storing files in Telegram using bot and by passing limits listed on [telegram bot limits website](https://core.telegram.org/bots/faq#my-bot-is-hitting-limits-how-do-i-avoid-this)
8+
Telegram Bot Storage is a simple library for storing files in Telegram using bot and by passing limits listed
9+
on [telegram bot limits website](https://core.telegram.org/bots/faq#my-bot-is-hitting-limits-how-do-i-avoid-this)
810

911
### Development
1012

1113
#### Dependencies
14+
1215
- Golang 1.16+
1316
- GNU Make
1417

18+
#### How to run
19+
20+
Create a `config.yaml` file under the [config](config) folder.Use [config.example.yaml](config.example.yaml) as a
21+
template to fill the file.
22+
23+
Run the following command: `make run` or `go run ./cmd/main.go`
24+
25+
1526
#### How to test
16-
Create a `.env.test` file with [.env.test.example](.env.test.example) as a template OR just set env var with the same name.
17-
Run the following command: `make test`
27+
Create a `config.yaml` file under the [config-test](config-test)
28+
folder. Use [config.test.example.yaml](config.example.yaml) as a template to fill the file
29+
30+
Run the following commands: `make test` and `make docker-e2etest`

config-test/config.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
package config_test
3+
4+
import (
5+
"github.com/DipandaAser/tg-bot-storage/internal/config"
6+
"gopkg.in/yaml.v3"
7+
"os"
8+
)
9+
10+
type Config struct {
11+
config.Config `yaml:",inline"`
12+
//ChatID is the ID of the chat to store message, this is use in some test
13+
ChatID int64 `yaml:"chat_id"`
14+
//DraftChatID is the ID of the chat used as a draft chat when downloading file
15+
DraftChatID int64 `yaml:"draft_chat_id"`
16+
}
17+
18+
var defaultConfig *Config
19+
20+
//GetDefaultConfig returns a config with default values from the yaml configFilePath
21+
func GetConfig(configFilePath string) Config {
22+
if defaultConfig == nil {
23+
defaultConfig = &Config{Config: config.Config{Tokens: make([]string, 0)}}
24+
file, err := os.Open(configFilePath)
25+
if err != nil {
26+
return Config{}
27+
}
28+
defer file.Close()
29+
30+
err = yaml.NewDecoder(file).Decode(defaultConfig)
31+
if err != nil {
32+
return Config{}
33+
}
34+
}
35+
return *defaultConfig
36+
}

config.example.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
tokens:
2+
- "bot token 1"
3+
- "bot token 2"
4+
- "bot token 3"
5+
api_key: ""

0 commit comments

Comments
 (0)