diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..e0de2c3 --- /dev/null +++ b/.github/dependabot.yml @@ -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" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..7e14310 --- /dev/null +++ b/.github/workflows/ci.yml @@ -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" . diff --git a/.gitignore b/.gitignore index 2e17373..66fd13c 100644 --- a/.gitignore +++ b/.gitignore @@ -13,5 +13,3 @@ # Dependency directories (remove the comment below to include it) # vendor/ - -config.go \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2e28667 --- /dev/null +++ b/Dockerfile @@ -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"] diff --git a/Dockerfile.doppler b/Dockerfile.doppler new file mode 100644 index 0000000..6196cd1 --- /dev/null +++ b/Dockerfile.doppler @@ -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"] diff --git a/config.go b/config.go new file mode 100644 index 0000000..3d3ecc8 --- /dev/null +++ b/config.go @@ -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") +}() diff --git a/config.go.sample b/config.go.sample deleted file mode 100644 index f44800d..0000000 --- a/config.go.sample +++ /dev/null @@ -1,3 +0,0 @@ -package main - -const BOT_TOKEN = "3838833:efuhuefhuefhuefheu" diff --git a/main.go b/main.go index 5632bc1..5b13c5b 100644 --- a/main.go +++ b/main.go @@ -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, }