Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: feature flag system #48

Merged
merged 2 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ jobs:
- name: TruffleHog OSS
uses: trufflesecurity/trufflehog@main
with:
path: ./
base: ${{ github.event.repository.default_branch }}
head: HEAD
extra_args: --debug --only-verified

ci:
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ jobs:
- name: TruffleHog OSS
uses: trufflesecurity/trufflehog@main
with:
path: ./
base: ${{ github.event.repository.default_branch }}
head: HEAD
extra_args: --debug --only-verified

ci:
Expand Down
2 changes: 0 additions & 2 deletions analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package analytics

import (
"github.com/allegro/bigcache/v3"
"github.com/getsentry/sentry-go"
"github.com/jmoiron/sqlx"
tb "gopkg.in/telebot.v3"
)
Expand All @@ -12,7 +11,6 @@ import (
type Dependency struct {
Memory *bigcache.BigCache
Bot *tb.Bot
Logger *sentry.Client
DB *sqlx.DB
TeknumID string
}
Expand Down
46 changes: 29 additions & 17 deletions analytics/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import (
"errors"
"net"
"net/http"
"os"
"time"

"github.com/teknologi-umum/captcha/analytics"
"github.com/teknologi-umum/captcha/shared"

"github.com/allegro/bigcache/v3"
"github.com/getsentry/sentry-go"
sentryhttp "github.com/getsentry/sentry-go/http"
"github.com/go-chi/chi/v5"
"github.com/jmoiron/sqlx"
Expand All @@ -25,7 +22,6 @@
type Dependency struct {
DB *sqlx.DB
Memory *bigcache.BigCache
Logger *sentry.Client
Mongo *mongo.Client
MongoDBName string
}
Expand Down Expand Up @@ -58,12 +54,12 @@
// Config is the configuration struct for the server package.
// Only the Port field is optional. It will be set to 8080 if not set.
type Config struct {
DB *sqlx.DB
Mongo *mongo.Client
MongoDBName string
Memory *bigcache.BigCache
Logger *sentry.Client
Port string
DB *sqlx.DB
Mongo *mongo.Client
MongoDBName string
Memory *bigcache.BigCache
Environment string
ListeningAddress string
}

// New creates and runs an HTTP server instance for fetching analytics data
Expand All @@ -72,26 +68,25 @@
// Requires 3 parameter that should be sent from the main goroutine.
func New(config Config) *http.Server {
// Give default port
if config.Port == "" {
config.Port = "8080"
if config.ListeningAddress == "" {
config.ListeningAddress = ":8080"

Check warning on line 72 in analytics/server/server.go

View check run for this annotation

Codecov / codecov/patch

analytics/server/server.go#L71-L72

Added lines #L71 - L72 were not covered by tests
}

deps := &Dependency{
DB: config.DB,
Memory: config.Memory,
Logger: config.Logger,
Mongo: config.Mongo,
MongoDBName: config.MongoDBName,
}

secureMiddleware := secure.New(secure.Options{
BrowserXssFilter: true,
ContentTypeNosniff: true,
SSLRedirect: os.Getenv("ENV") == "production",
IsDevelopment: os.Getenv("ENV") == "development",
SSLRedirect: config.Environment == "production",
IsDevelopment: config.Environment == "development",

Check warning on line 86 in analytics/server/server.go

View check run for this annotation

Codecov / codecov/patch

analytics/server/server.go#L85-L86

Added lines #L85 - L86 were not covered by tests
})
corsMiddleware := cors.New(cors.Options{
Debug: os.Getenv("ENV") == "development",
Debug: config.Environment == "development",

Check warning on line 89 in analytics/server/server.go

View check run for this annotation

Codecov / codecov/patch

analytics/server/server.go#L89

Added line #L89 was not covered by tests
AllowedOrigins: []string{},
AllowedMethods: []string{"GET", "OPTIONS"},
})
Expand All @@ -114,6 +109,11 @@
})

r.Get("/users", func(w http.ResponseWriter, r *http.Request) {
if deps.DB == nil {
w.WriteHeader(http.StatusNotFound)
return
}

Check warning on line 115 in analytics/server/server.go

View check run for this annotation

Codecov / codecov/patch

analytics/server/server.go#L112-L115

Added lines #L112 - L115 were not covered by tests

data, err := deps.GetAll(r.Context())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -140,6 +140,10 @@
})

r.Get("/hourly", func(w http.ResponseWriter, r *http.Request) {
if deps.DB == nil {
w.WriteHeader(http.StatusNotFound)
return
}

Check warning on line 146 in analytics/server/server.go

View check run for this annotation

Codecov / codecov/patch

analytics/server/server.go#L143-L146

Added lines #L143 - L146 were not covered by tests
data, err := deps.GetHourly(r.Context())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -166,6 +170,10 @@
})

r.Get("/total", func(w http.ResponseWriter, r *http.Request) {
if deps.DB == nil {
w.WriteHeader(http.StatusNotFound)
return
}

Check warning on line 176 in analytics/server/server.go

View check run for this annotation

Codecov / codecov/patch

analytics/server/server.go#L173-L176

Added lines #L173 - L176 were not covered by tests
data, err := deps.GetTotal(r.Context())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -192,6 +200,10 @@
})

r.Get("/dukun", func(w http.ResponseWriter, r *http.Request) {
if deps.Mongo == nil {
w.WriteHeader(http.StatusNotFound)
return
}

Check warning on line 206 in analytics/server/server.go

View check run for this annotation

Codecov / codecov/patch

analytics/server/server.go#L203-L206

Added lines #L203 - L206 were not covered by tests
data, err := deps.GetDukunPoints(r.Context())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand Down Expand Up @@ -219,7 +231,7 @@

return &http.Server{
Handler: r,
Addr: net.JoinHostPort("", config.Port),
Addr: config.ListeningAddress,

Check warning on line 234 in analytics/server/server.go

View check run for this annotation

Codecov / codecov/patch

analytics/server/server.go#L234

Added line #L234 was not covered by tests
ReadTimeout: time.Minute,
WriteTimeout: time.Minute,
ReadHeaderTimeout: time.Minute,
Expand Down
4 changes: 1 addition & 3 deletions ascii/ascii.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import (
"github.com/teknologi-umum/captcha/shared"
"github.com/teknologi-umum/captcha/utils"

"github.com/getsentry/sentry-go"
tb "gopkg.in/telebot.v3"
)

// Dependencies contains dependency injection struct
// to be used for the Ascii package.
type Dependencies struct {
Bot *tb.Bot
Logger *sentry.Client
Bot *tb.Bot
}

// Ascii simply sends ASCII art message for fun.
Expand Down
5 changes: 2 additions & 3 deletions badwords/badwords.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package badwords

import (
"context"
"os"
"strings"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
Expand All @@ -14,6 +12,7 @@ import (
type Dependency struct {
Mongo *mongo.Client
MongoDBName string
AdminIDs []string
}

// AddBadWords will add a new word into the MongoDB database.
Expand All @@ -29,7 +28,7 @@ func (d *Dependency) AddBadWord(ctx context.Context, word string) error {
// Authenticate will check if the user is allowed to add a new
// badword into the database.
func (d *Dependency) Authenticate(id string) bool {
admins := strings.Split(os.Getenv("ADMIN_ID"), ",")
admins := d.AdminIDs

for _, admin := range admins {
if admin == id {
Expand Down
6 changes: 1 addition & 5 deletions badwords/badwords_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestMain(m *testing.M) {
dependency = &badwords.Dependency{
Mongo: db,
MongoDBName: dbName,
AdminIDs: []string{"30", "40", "50", "60"},
}

exitCode := m.Run()
Expand All @@ -64,11 +65,6 @@ func TestMain(m *testing.M) {
}

func TestAuthenticate(t *testing.T) {
err := os.Setenv("ADMIN_ID", "30,40,50,60")
if err != nil {
t.Fatal(err)
}

ok := dependency.Authenticate("30")
if !ok {
t.Errorf("failed to authenticate with the value of %d", 30)
Expand Down
2 changes: 0 additions & 2 deletions captcha/captcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/teknologi-umum/captcha/analytics"

"github.com/allegro/bigcache/v3"
"github.com/getsentry/sentry-go"
tb "gopkg.in/telebot.v3"
)

Expand All @@ -13,7 +12,6 @@ import (
type Dependencies struct {
Memory *bigcache.BigCache
Bot *tb.Bot
Logger *sentry.Client
Analytics *analytics.Dependency
TeknumID string
}
Loading