Skip to content

Commit

Permalink
rewrite reddit bot to be better selfhostable and less prone to being …
Browse files Browse the repository at this point in the history
…ratelimited
  • Loading branch information
topi314 committed Jul 10, 2023
1 parent 62540df commit d7dcfcc
Show file tree
Hide file tree
Showing 28 changed files with 1,601 additions and 915 deletions.
7 changes: 4 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.editorconfig
.github/
.gitignore
README.md
LICENSE
Dockerfile
conf.env
conf.env.example
config.json
config.example.yml
21 changes: 13 additions & 8 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ root = true

[*]
charset = utf-8
end_of_line = crlf
end_of_line = lf
indent_size = 4
indent_style = space
indent_style = tab
insert_final_newline = false
max_line_length = 120
max_line_length = 420
tab_width = 4
ij_continuation_indent_size = 8
ij_formatter_off_tag = @formatter:off
Expand All @@ -16,22 +16,27 @@ ij_smart_tabs = false
ij_visual_guides = none
ij_wrap_on_typing = false

[{*.go, *.go2}]
indent_style = tab
[{*.yml,*.yaml}]
indent_size = 2

[{*.go,*.go2}]
ij_continuation_indent_size = 4
ij_go_add_leading_space_to_comments = false
ij_go_add_parentheses_for_single_import = false
ij_smart_tabs = true
ij_go_GROUP_CURRENT_PROJECT_IMPORTS = false
ij_go_add_leading_space_to_comments = true
ij_go_add_parentheses_for_single_import = true
ij_go_call_parameters_new_line_after_left_paren = true
ij_go_call_parameters_right_paren_on_new_line = true
ij_go_call_parameters_wrap = off
ij_go_fill_paragraph_width = 80
ij_go_group_current_project_imports = true
ij_go_group_stdlib_imports = true
ij_go_import_sorting = gofmt
ij_go_keep_indents_on_empty_lines = false
ij_go_local_group_mode = project
ij_go_move_all_imports_in_one_declaration = true
ij_go_move_all_stdlib_imports_in_one_group = true
ij_go_remove_redundant_import_aliases = true
ij_go_run_go_fmt_on_reformat = true
ij_go_use_back_quotes_for_imports = false
ij_go_wrap_comp_lit = off
ij_go_wrap_comp_lit_newline_after_lbrace = true
Expand Down
63 changes: 63 additions & 0 deletions .github/workflows/binary.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Binary Release

on:
push:

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
goos: [ linux, windows, darwin ]
goarch: [ amd64, 386, arm, arm64 ]
exclude:
- goos: darwin
goarch: 386
- goos: darwin
goarch: arm
- goos: windows
goarch: arm
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.20.x
cache: true

- name: File Extension Windows
if: ${{ matrix.goos == 'windows' }}
run: echo "file_extension=.exe" >> $GITHUB_ENV

- name: Prepare Version Info
run: |
echo "VERSION=$(git describe --tags)" >> $GITHUB_ENV
echo "COMMIT=$(git rev-parse HEAD)" >> $GITHUB_ENV
- name: Build
run: |
CGO_ENABLED=0
GOOS=${{ matrix.goos }}
GOARCH=${{ matrix.goarch }}
go build -ldflags "-X main.Version=${{ env.VERSION }} -X main.Commit=${{ env.COMMIT }}" -o reddit-discord-bot-${{ matrix.goos }}-${{ matrix.goarch }}${{ env.file_name }} github.com/topi314/reddit-discord-bot
- name: Upload
uses: actions/upload-artifact@v2
with:
name: reddit-discord-bot
path: reddit-discord-bot

- name: Release
if: ${{ github.event_name == 'release' }}
uses: ncipollo/release-action@v1
with:
artifacts: reddit-discord-bot-*
allowUpdates: true
omitBodyDuringUpdate: true
omitDraftDuringUpdate: true
omitNameDuringUpdate: true
omitPrereleaseDuringUpdate: true
104 changes: 64 additions & 40 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -1,44 +1,68 @@
name: Docker
name: Build and Deploy

on:
push:
push:

jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/topisenpai/reddit-discord-bot
tags: |
type=ref,event=branch
type=ref,event=tag
type=ref,event=pr
type=sha,prefix=
- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64,linux/arm/v7,linux/arm64/v8
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: |
ghcr.io/${{ github.repository }}
tags: |
type=ref,event=branch
type=ref,event=tag
type=ref,event=pr
type=sha,prefix=
- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Prepare Version Info
run: |
echo "VERSION=$(git describe --tags)" >> $GITHUB_ENV
echo "COMMIT=$(git rev-parse HEAD)" >> $GITHUB_ENV
- name: Build and push
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64,linux/arm/v7,linux/arm64/v8
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
VERSION=${{ env.VERSION }}
COMMIT=${{ env.COMMIT }}
deploy:
needs:
- build
runs-on: ubuntu-latest
steps:
- name: Deploy
uses: fjogeleit/http-request-action@v1
with:
url: "https://watchtower.topi.wtf/v1/update?image=ghcr.io/topi314/reddit-discord-bot"
method: "POST"
timeout: 60000
customHeaders: '{"Authorization": "Bearer ${{ secrets.TOKEN }}"}'
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.idea/

conf.env
.env
config.yml
database.db
22 changes: 16 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.18-alpine AS build
FROM --platform=$BUILDPLATFORM golang:1.20-alpine AS build

WORKDIR /build

Expand All @@ -8,14 +8,24 @@ RUN go mod download

COPY . .

RUN CGO_ENABLED=0 go build -o app .
ARG TARGETOS
ARG TARGETARCH
ARG VERSION
ARG COMMIT

FROM alpine
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
CGO_ENABLED=0 \
GOOS=$TARGETOS \
GOARCH=$TARGETARCH \
go build -ldflags="-X 'main.Version=$VERSION' -X 'main.Commit=$COMMIT'" -o reddit-discord-bot github.com/topi314/reddit-discord-bot

WORKDIR /app
FROM alpine

COPY --from=build /build/app /app/bin
COPY --from=build /build/reddit-discord-bot /bin/reddit-discord-bot

EXPOSE 80

ENTRYPOINT ["/app/bin"]
ENTRYPOINT ["/bin/reddit-discord-bot"]

CMD ["-config", "/var/lib/reddit-discord-bot/config.yml", "-database.sqlite.path", "/var/lib/reddit-discord-bot/database.db"]
Loading

0 comments on commit d7dcfcc

Please sign in to comment.