Skip to content

Commit

Permalink
Make bot production ready (#2)
Browse files Browse the repository at this point in the history
* Some additional configuration
* Add endpoint for alive
* Change json key for alive
* Add CI
  • Loading branch information
Divkix authored Aug 6, 2022
1 parent a2aa526 commit a327aaa
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 6 deletions.
14 changes: 14 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 2
updates:
- package-ecosystem: gomod
directory: "/"
schedule:
interval: daily
- package-ecosystem: docker
directory: "/"
schedule:
interval: daily
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI

on:
push:
branches:
- main
pull_request:

jobs:
ci:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
go-version: ['1.18', '1.19']
os: [ubuntu-latest, macOS-latest, windows-latest]

steps:
- name: Set up ${{ matrix.go-version }}
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- uses: actions/checkout@v3
- name: Build app
run: |-
go build -ldflags="-w -s" .
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,3 @@

# Dependency directories (remove the comment below to include it)
# vendor/

config.go
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Build Stage: Build bot using the alpine image, also install doppler in it
FROM golang:1.19-alpine AS builder
RUN addgroup -S gigafeeduser \
&& adduser -S -u 10000 -g gigafeeduser gigafeeduser
RUN apk add -U --no-cache ca-certificates
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 GOOS=`go env GOHOSTOS` GOARCH=`go env GOHOSTARCH` go build -o out/GIGAFeed

# Run Stage: Run bot using the bot and doppler binary copied from build stage
FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /app/out/GIGAFeed /
COPY --from=builder /etc/passwd /etc/passwd
USER gigafeeduser
CMD ["/GIGAFeed"]
14 changes: 14 additions & 0 deletions Dockerfile.doppler
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Build Stage: Build bot using the alpine image, also install doppler in it
FROM golang:1.19-alpine AS builder
RUN apk add --no-cache curl wget gnupg git
RUN (curl -Ls --tlsv1.2 --proto "=https" --retry 3 https://cli.doppler.com/install.sh || wget -t 3 -qO- https://cli.doppler.com/install.sh) | sh
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 GOOS=`go env GOHOSTOS` GOARCH=`go env GOHOSTARCH` go build -o out/GIGAFeed -ldflags="-w -s" .

# Run Stage: Run bot using the bot and doppler binary copied from build stage
FROM gcr.io/distroless/static
COPY --from=builder /app/out/GIGAFeed /
COPY --from=builder /usr/local/bin/doppler /
ENTRYPOINT ["/doppler", "run", "--"]
CMD ["/GIGAFeed"]
23 changes: 23 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import "os"

// first the token will be read from the environment variable
// if environment variable is empty, will use the constant value
var BOT_TOKEN = func() string {
if os.Getenv("BOT_TOKEN") == "" {
// constant bot token value
return "3838833:efuhuefhuefhuefheu"
}
return os.Getenv("BOT_TOKEN")
}()

// first the token will be read from the environment variable
// if environment variable is empty, will use the constant value
var PORT = func() string {
if os.Getenv("PORT") == "" {
// constant bot token value
return "8080"
}
return os.Getenv("PORT")
}()
3 changes: 0 additions & 3 deletions config.go.sample

This file was deleted.

19 changes: 18 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,26 @@ func main() {

func webhookListener(b *gotgbot.Bot) {
mux := http.NewServeMux()
mux.HandleFunc("/alive", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
w.Header().Set("Content-Type", "application/json")
type status struct {
Alive bool `json:"alive"`
BotId int64 `json:"bot_id"`
BotUsername string `json:"bot_username"`
}
jsonResp, _ := json.Marshal(
status{
Alive: true,
BotId: b.Id,
BotUsername: b.Username,
},
)
w.Write(jsonResp)
})
mux.HandleFunc("/", processUpdate(b))
server := &http.Server{
Addr: "0.0.0.0:3455",
Addr: "0.0.0.0:" + PORT,
Handler: mux,
ReadTimeout: time.Second * 2,
}
Expand Down

1 comment on commit a327aaa

@celestix
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a test message

Please sign in to comment.