Skip to content

Commit

Permalink
Merge pull request #188 from zaanposni/zaanposni/187-configsetup
Browse files Browse the repository at this point in the history
refactored config setup and fixed test env
  • Loading branch information
zaanposni authored Dec 14, 2020
2 parents 9b1d035 + be47476 commit 2239c80
Show file tree
Hide file tree
Showing 26 changed files with 196 additions and 321 deletions.
14 changes: 0 additions & 14 deletions .github/workflows/backend_docker.yml

This file was deleted.

17 changes: 0 additions & 17 deletions .github/workflows/backend_docker_master.yml

This file was deleted.

10 changes: 2 additions & 8 deletions .github/workflows/docker_compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@ jobs:
- uses: actions/checkout@v2
- name: Build the stack
run: |
sudo chmod +x bootstrap_init.sh
./bootstrap_init.sh
docker-compose build
docker-compose up -d
docker-compose --env-file ./tests/.docker.env up -d
SmokeTest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build the stack
run: |
sudo chmod +x bootstrap_init.sh
./bootstrap_init.sh
docker-compose build
docker-compose up -d
docker-compose --env-file ./tests/.docker.env up -d
- name: Test
run: |
cd tests
Expand Down
11 changes: 3 additions & 8 deletions .github/workflows/docker_compose_master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,15 @@ jobs:
- uses: actions/checkout@v2
- name: Build the stack
run: |
sudo chmod +x bootstrap_init.sh
./bootstrap_init.sh
docker-compose build
docker-compose up -d
docker-compose --env-file ./tests/.docker.env up -d
SmokeTest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build the stack
run: |
sudo chmod +x bootstrap_init.sh
./bootstrap_init.sh
docker-compose build
docker-compose up -d
docker-compose --env-file ./tests/.docker.env up -d
- name: Test
run: |
cd tests
Expand Down
29 changes: 28 additions & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
FROM python:3 as init

ARG MYSQL_DATABASE
ENV MYSQL_DATABASE $MYSQL_DATABASE

ARG MYSQL_USER
ENV MYSQL_USER $MYSQL_USER

ARG MYSQL_PASSWORD
ENV MYSQL_PASSWORD $MYSQL_PASSWORD

ARG MYSQL_PORT
ENV MYSQL_PORT $MYSQL_PORT

ARG MYSQL_HOST
ENV MYSQL_HOST $MYSQL_HOST

ARG DISCORD_BOT_TOKEN
ENV DISCORD_BOT_TOKEN $DISCORD_BOT_TOKEN

ARG DISCORD_OAUTH_CLIENT_ID
ENV DISCORD_OAUTH_CLIENT_ID $DISCORD_OAUTH_CLIENT_ID

ARG DISCORD_OAUTH_CLIENT_SECRET
ENV DISCORD_OAUTH_CLIENT_SECRET $DISCORD_OAUTH_CLIENT_SECRET

ARG DISCORD_SITE_ADMINS
ENV DISCORD_SITE_ADMINS $DISCORD_SITE_ADMINS

WORKDIR /app

COPY setup_app.py .
COPY config.json .
COPY ./masz/appsettings.json ./masz/

RUN python3 setup_app.py
Expand Down
8 changes: 8 additions & 0 deletions backend/masz/Controllers/api/v1/ModCaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ public async Task<IActionResult> PutSpecificItem([FromRoute] string guildid, [Fr
logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 400 Cannot create cases for bots.");
return BadRequest("Cannot create cases for bots.");
}
if (config.Value.SiteAdminDiscordUserIds.Contains(currentReportedUser.Id)) {
logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 400 Cannot create cases for site admins.");
return BadRequest("Cannot create cases for site admins.");
}
modCase.Username = currentReportedUser.Username; // update to new username

var currentReportedMember = await discord.FetchMemberInfo(guildid, modCase.UserId);
Expand Down Expand Up @@ -316,6 +320,10 @@ public async Task<IActionResult> CreateItem([FromRoute] string guildid, [FromBod
logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 400 Cannot create cases for bots.");
return BadRequest("Cannot create cases for bots.");
}
if (config.Value.SiteAdminDiscordUserIds.Contains(currentReportedUser.Id)) {
logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 400 Cannot create cases for site admins.");
return BadRequest("Cannot create cases for site admins.");
}

newModCase.Username = currentReportedUser.Username;

Expand Down
18 changes: 7 additions & 11 deletions backend/setup_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ def random_pass(pass_length=200):
random.seed = (os.urandom(1024))
return ''.join(random.choice(chars) for i in range(pass_length))

with open("./config.json", "r") as fh:
config = json.load(fh)

db = config["mysql_database"]
CNC_STRING = f"Server={db['host']};Port={db['port']};Database={db['database']};Uid={db['user']};Pwd={db['pass']};"
CNC_STRING = f'Server={os.getenv("MYSQL_HOST")};Port={os.getenv("MYSQL_PORT")};Database={os.getenv("MYSQL_DATABASE")};Uid={os.getenv("MYSQL_USER")};Pwd={os.getenv("MYSQL_PASSWORD")};'

PATH = os.path.join("masz", "appsettings.json")

Expand All @@ -21,13 +17,13 @@ def random_pass(pass_length=200):

data["ConnectionStrings"]["DefaultConnection"] = CNC_STRING
data["AppSettings"]["Token"] = random_pass(200)
data["InternalConfig"]["DiscordBotToken"] = config["discord"]["bot_token"]
data["InternalConfig"]["DiscordClientId"] = config["discord"]["oauth_client_id"]
data["InternalConfig"]["DiscordClientSecret"] = config["discord"]["oauth_client_secret"]
data["InternalConfig"]["ServiceHostName"] = config["meta"]["service_name"]
data["InternalConfig"]["ServiceBaseUrl"] = config["meta"]["service_base_url"]
data["InternalConfig"]["DiscordBotToken"] = os.getenv("DISCORD_BOT_TOKEN")
data["InternalConfig"]["DiscordClientId"] = os.getenv("DISCORD_OAUTH_CLIENT_ID")
data["InternalConfig"]["DiscordClientSecret"] = os.getenv("DISCORD_OAUTH_CLIENT_SECRET")
data["InternalConfig"]["ServiceHostName"] = os.getenv("META_SERVICE_NAME")
data["InternalConfig"]["ServiceBaseUrl"] = os.getenv("META_SERVICE_BASE_URL")
try:
data["InternalConfig"]["SiteAdminDiscordUserIds"] = list(config["discord"]["site_admins"])
data["InternalConfig"]["SiteAdminDiscordUserIds"] = list(os.getenv("DISCORD_SITE_ADMINS").split(","))
except:
data["InternalConfig"]["SiteAdminDiscordUserIds"] = []

Expand Down
58 changes: 0 additions & 58 deletions bootstrap.sh

This file was deleted.

50 changes: 0 additions & 50 deletions bootstrap_init.sh

This file was deleted.

22 changes: 0 additions & 22 deletions default-config.json

This file was deleted.

3 changes: 1 addition & 2 deletions discordbot/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ COPY requirements.txt .

RUN python -m pip install -r requirements.txt

COPY config.json .
COPY src/ .

ENTRYPOINT [ "python", "main.py" ]
ENTRYPOINT [ "python", "-u", "main.py" ]
Empty file removed discordbot/src/__init__.py
Empty file.
6 changes: 2 additions & 4 deletions discordbot/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from discord.errors import LoginFailure
from discord.ext import commands

from utils.config import cfg

prefix = os.getenv("BOT_PREFIX", "$")
if prefix.strip() == "":
prefix = "$"
Expand Down Expand Up @@ -50,7 +48,7 @@ async def on_ready():

# discord related
# ================================================
activity = cfg.get("meta", dict()).get("service_base_url", "github.com/zaanposni/discord-masz")
activity = os.getenv("META_SERVICE_BASE_URL", "github.com/zaanposni/discord-masz")
if activity:
game = discord.Game(name=activity)
await client.change_presence(activity=game)
Expand All @@ -59,7 +57,7 @@ async def on_ready():

def start_bot():
try:
token = cfg.get("discord", dict())["bot_token"]
token = os.getenv("DISCORD_BOT_TOKEN")
except KeyError:
print(f"========================")
print(f"'discord'.'bot_token' not found in config files!")
Expand Down
Empty file removed discordbot/src/utils/__init__.py
Empty file.
20 changes: 0 additions & 20 deletions discordbot/src/utils/config.py

This file was deleted.

Loading

0 comments on commit 2239c80

Please sign in to comment.