From 7e76c3b7088b0b4cf2ea0511348fdff27142e6bb Mon Sep 17 00:00:00 2001 From: Reinaldy Rafli Date: Sun, 23 Jun 2024 10:55:41 +0700 Subject: [PATCH] ci: move back to fly.io (#64) --- .github/workflows/codeql.yml | 41 ------------------------------- .github/workflows/deploy.yml | 14 +++++++++++ README.md | 10 ++++++-- cmd/captcha/configuration.go | 1 + cmd/captcha/main.go | 31 ++++++++++++++--------- cmd/captcha/transport_wrapper.go | 16 ++++++------ doc.go | 2 +- fly.toml | 42 ++++++-------------------------- 8 files changed, 60 insertions(+), 97 deletions(-) delete mode 100644 .github/workflows/codeql.yml diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml deleted file mode 100644 index 871e217..0000000 --- a/.github/workflows/codeql.yml +++ /dev/null @@ -1,41 +0,0 @@ -name: "CodeQL" - -on: - push: - branches: [ "master" ] - pull_request: - branches: [ "master" ] - schedule: - - cron: "11 18 * * 6" - -jobs: - analyze: - name: Analyze - runs-on: ubuntu-latest - permissions: - actions: read - contents: read - security-events: write - - strategy: - fail-fast: false - matrix: - language: [ go ] - - steps: - - name: Checkout - uses: actions/checkout@v3 - - - name: Initialize CodeQL - uses: github/codeql-action/init@v2 - with: - languages: ${{ matrix.language }} - queries: +security-and-quality - - - name: Autobuild - uses: github/codeql-action/autobuild@v2 - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 - with: - category: "/language:${{ matrix.language }}" diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 0bc2ebe..3ae58ed 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -133,3 +133,17 @@ jobs: environment: production set_commits: skip version: ${{ github.sha }} + + deploy: + name: Deploy app + needs: + - ci + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: superfly/flyctl-actions/setup-flyctl@master + + - run: flyctl deploy --remote-only + env: + FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} diff --git a/README.md b/README.md index 8c111a7..56f440c 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ feature_flag: dukun: false under_attack: true reminder: false + deletion: false + http_server: false home_group_id: 0 # Assuming default value admin_ids: [ ] # Optional sentry.io DSN, you can track project errors & performance there @@ -72,7 +74,9 @@ under_attack: "badwords_insertion": false, "dukun": false, "under_attack": true, - "reminder": false + "reminder": false, + "deletion": false, + "http_server": false }, "home_group_id": 0, "admin_ids": [], @@ -107,6 +111,8 @@ Optional: * FEATURE_FLAG_DUKUN: (Default: "false") * FEATURE_FLAG_UNDER_ATTACK: (Default: "true") * FEATURE_FLAG_REMINDER: (Default: "false") +* FEATURE_FLAG_DELETION: (Default: "false") +* FEATURE_FLAG_HTTP_SERVER: (Default: "false") * HOME_GROUP_ID: (No default value provided) * ADMIN_IDS: (No default value provided, comma-separated string) * SENTRY_DSN: (No default value provided) @@ -199,7 +205,7 @@ Let's work together to make Telegram a safer and more enjoyable experience for e ``` Teknologi Umum Captcha Bot -Copyright (C) 2023 Teknologi Umum +Copyright (C) 2024 Teknologi Umum This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/cmd/captcha/configuration.go b/cmd/captcha/configuration.go index c1edb42..7658621 100644 --- a/cmd/captcha/configuration.go +++ b/cmd/captcha/configuration.go @@ -14,6 +14,7 @@ type FeatureFlag struct { UnderAttack bool `yaml:"under_attack" json:"under_attack" env:"FEATURE_FLAG_UNDER_ATTACK" env-default:"true"` Reminder bool `yaml:"reminder" json:"reminder" env:"FEATURE_FLAG_REMINDER" env-default:"false"` Deletion bool `yaml:"deletion" json:"deletion" env:"FEATURE_FLAG_DELETION" env-default:"false"` + HttpServer bool `yaml:"http_server" json:"http_server" env:"FEATURE_FLAG_HTTP_SERVER" env-default:"false"` } type Configuration struct { diff --git a/cmd/captcha/main.go b/cmd/captcha/main.go index 742b1d4..82560fb 100644 --- a/cmd/captcha/main.go +++ b/cmd/captcha/main.go @@ -359,13 +359,16 @@ func main() { return } - httpServer := server.New(server.Config{ - DB: db, - Memory: cache, - Mongo: mongoClient, - MongoDBName: mongoDBName, - ListeningAddress: net.JoinHostPort(configuration.HTTPServer.ListeningHost, configuration.HTTPServer.ListeningPort), - }) + var httpServer *http.Server + if configuration.FeatureFlag.HttpServer { + httpServer = server.New(server.Config{ + DB: db, + Memory: cache, + Mongo: mongoClient, + MongoDBName: mongoDBName, + ListeningAddress: net.JoinHostPort(configuration.HTTPServer.ListeningHost, configuration.HTTPServer.ListeningPort), + }) + } // This is basically just for health check. b.Handle("/start", func(c tb.Context) error { @@ -412,6 +415,10 @@ func main() { signal.Notify(exitSignal, os.Interrupt) go func() { + if httpServer == nil { + return + } + // Start an HTTP server instance log.Printf("Starting http server on %s", httpServer.Addr) err := httpServer.ListenAndServe() @@ -429,10 +436,12 @@ func main() { b.Stop() - err = httpServer.Shutdown(shutdownCtx) - if err != nil { - log.Error().Err(err).Msg("Shutting down the http server") - sentry.CaptureException(err) + if httpServer != nil { + err := httpServer.Shutdown(shutdownCtx) + if err != nil { + log.Error().Err(err).Msg("Shutting down the http server") + sentry.CaptureException(err) + } } log.Debug().Msg("Starting a 10 second countdown until killing the application") diff --git a/cmd/captcha/transport_wrapper.go b/cmd/captcha/transport_wrapper.go index 58b4d54..eb99dca 100644 --- a/cmd/captcha/transport_wrapper.go +++ b/cmd/captcha/transport_wrapper.go @@ -24,17 +24,17 @@ func (s *SentryTransportWrapper) RoundTrip(request *http.Request) (*http.Respons ctx := request.Context() var cleanRequestURL string pathFragments := strings.Split(request.URL.Path, "/") - if len(pathFragments) >= 2 { - if pathFragments[0] == "" { - pathFragments[1] = "bot[Filtered]" - } else { - pathFragments[0] = "bot[Filtered]" + for i, value := range pathFragments { + if strings.HasPrefix(value, "bot") { + pathFragments[i] = "bot[Filtered]" } - - cleanRequestURL = strings.Join(pathFragments, "/") } - span := sentry.StartSpan(ctx, "http.client", sentry.WithTransactionName(fmt.Sprintf("%s %s", request.Method, cleanRequestURL))) + cleanRequestURL = strings.Join(pathFragments, "/") + transactionName := fmt.Sprintf("%s %s", request.Method, cleanRequestURL) + span := sentry.StartSpan(ctx, "http.client", + sentry.WithTransactionName(transactionName), + sentry.WithDescription(transactionName)) defer span.Finish() span.SetData("http.query", request.URL.Query().Encode()) diff --git a/doc.go b/doc.go index ce46df7..fdbd6d3 100644 --- a/doc.go +++ b/doc.go @@ -1,5 +1,5 @@ // Teknologi Umum Captcha Bot -// Copyright (C) 2023 Teknologi Umum +// Copyright (C) 2024 Teknologi Umum // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by diff --git a/fly.toml b/fly.toml index 9e2e2f3..859a919 100644 --- a/fly.toml +++ b/fly.toml @@ -1,38 +1,12 @@ +# fly.toml app configuration file generated for teknologi-umum-captcha on 2024-06-23T10:31:45+07:00 +# +# See https://fly.io/docs/reference/configuration/ for information about how to use this file. # -app = "captcha-bot" - -kill_signal = "SIGINT" -kill_timeout = 5 - -[env] - -[experimental] - allowed_public_ports = [] - auto_rollback = true - -[[services]] - http_checks = [] - internal_port = 8080 - processes = ["app"] - protocol = "tcp" - script_checks = [] - - [services.concurrency] - hard_limit = 25 - soft_limit = 20 - type = "connections" - - [[services.ports]] - handlers = ["http"] - port = 80 +app = 'teknologi-umum-captcha' +primary_region = 'cdg' - [[services.ports]] - handlers = ["tls", "http"] - port = 443 +[build] - [[services.tcp_checks]] - grace_period = "1s" - interval = "15s" - restart_limit = 6 - timeout = "2s" +[[vm]] + size = 'shared-cpu-1x'