Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
Major refactoring (switch to go-pg, basis for plans (#4), code cleanu…
Browse files Browse the repository at this point in the history
…p) (#13)
  • Loading branch information
julienrbrt committed Jan 16, 2021
1 parent abd2050 commit 5e51304
Show file tree
Hide file tree
Showing 93 changed files with 2,590 additions and 1,597 deletions.
12 changes: 11 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Default App Name
######################
# DEFAULT APP CONFIG #
######################

APP_NAME='WoningFinder'
APP_PORT=8080
APP_DEBUG=true

# Credentials Vault
AES_SECRET='test'
Expand Down Expand Up @@ -28,5 +32,11 @@ REDIS_PASSWORD='woningfinder'
# Sentry Credentials
SENTRY_DSN='SENTRY_DSN'

# Stripe Credentials
STRIPE_API_KEY='apikey'

# Sendgrid Credentials
SENDGRID_API_KEY='apikey'

# Mapbox Credentials
MAPBOX_API_KEY='apikey'
66 changes: 20 additions & 46 deletions cmd/housing-finder/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

import (
"fmt"
"log"
"os"
"time"

"github.com/woningfinder/woningfinder/internal/corporation/scheduler"

"github.com/woningfinder/woningfinder/internal/services/corporation"

"github.com/woningfinder/woningfinder/internal/bootstrap"
"github.com/woningfinder/woningfinder/internal/corporation"
"github.com/woningfinder/woningfinder/pkg/config"
"github.com/woningfinder/woningfinder/pkg/logging"

Expand All @@ -23,25 +25,21 @@ func init() {
if err := godotenv.Load("../../.env"); err != nil {
_ = config.MustGetString("APP_NAME")
}

// register m2m models with go-pg
bootstrap.RegisterModel()
}

func main() {
logger := logging.NewZapLoggerWithSentry(config.MustGetString("SENTRY_DSN"))

// connect to databases
err := bootstrap.InitDB()
if err != nil {
logger.Sugar().Fatal(err)
}

err = bootstrap.InitRedis()
if err != nil {
logger.Sugar().Fatal(err)
}
logger := logging.NewZapLogger(config.GetBoolOrDefault("APP_DEBUG", false), config.MustGetString("SENTRY_DSN"))

dbClient := bootstrap.CreateDBClient(logger)
redisClient := bootstrap.CreateRedisClient(logger)
mapboxClient := bootstrap.CreateMapboxClient()

clientProvider := bootstrap.CreateClientProvider(logger, mapboxClient)
corporationService := corporation.NewService(logger, bootstrap.DB, bootstrap.RDB)
corporationService := corporation.NewService(logger, dbClient, redisClient)

// get time location
nl, err := time.LoadLocation("Europe/Amsterdam")
if err != nil {
Expand All @@ -52,9 +50,8 @@ func main() {
c := cron.New(cron.WithLocation(nl), cron.WithSeconds(), cron.WithLogger(cron.VerbosePrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags))))

// populate crons
for _, corp := range *clientProvider.List() {
// https://github.com/golang/go/wiki/CommonMistakes#using-reference-to-loop-iterator-variable
corp := corp
for _, corp := range clientProvider.List() {
corp := corp // https://github.com/golang/go/wiki/CommonMistakes#using-reference-to-loop-iterator-variable

// get corporation client
client, err := clientProvider.Get(corp)
Expand All @@ -63,40 +60,17 @@ func main() {
continue
}

// specify cron time or fallback to default
// always check at midnight
if _, err = c.AddFunc("@midnight", func() {
if err := corporationService.PublishOffers(client, corp); err != nil {
logger.Sugar().Error(err)
}
}); err != nil {
logger.Sugar().Error(err)
}

// check at 0, 10 and 30 seconds after the publishing time
var spec string
for _, second := range []int{0, 10, 25, 50} {
if corp.SelectionTime != (time.Time{}) {
spec = buildSpec(corp.SelectionTime.Hour(), corp.SelectionTime.Minute(), second)
} else {
// the default is running at 17:00 if not specified
spec = buildSpec(17, 00, second)
}

if _, err = c.AddFunc(spec, func() {
// schedule corporation fetching
schedule := scheduler.CorporationScheduler(corp)
for _, s := range schedule {
c.Schedule(s, cron.FuncJob(func() {
if err := corporationService.PublishOffers(client, corp); err != nil {
logger.Sugar().Error(err)
}
}); err != nil {
logger.Sugar().Error(err)
}
}))
}
}

// start cron scheduler
c.Run()
}

func buildSpec(hour, minute, second int) string {
return fmt.Sprintf("%d %d %d * * *", second, minute, hour)
}
34 changes: 17 additions & 17 deletions cmd/housing-matcher/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
"github.com/woningfinder/woningfinder/internal/domain/entity"
"github.com/woningfinder/woningfinder/pkg/logging"

"github.com/joho/godotenv"
"github.com/woningfinder/woningfinder/internal/bootstrap"
"github.com/woningfinder/woningfinder/internal/corporation"
"github.com/woningfinder/woningfinder/internal/user"
"github.com/woningfinder/woningfinder/internal/services/corporation"
"github.com/woningfinder/woningfinder/internal/services/user"
"github.com/woningfinder/woningfinder/pkg/config"
)

Expand All @@ -18,28 +19,27 @@ func init() {
if err := godotenv.Load("../../.env"); err != nil {
_ = config.MustGetString("APP_NAME")
}

// register m2m models with go-pg
bootstrap.RegisterModel()
}

func main() {
logger := logging.NewZapLoggerWithSentry(config.MustGetString("SENTRY_DSN"))

err := bootstrap.InitDB()
if err != nil {
logger.Sugar().Fatal(err)
}

err = bootstrap.InitRedis()
if err != nil {
logger.Sugar().Fatal(err)
}
logger := logging.NewZapLogger(config.GetBoolOrDefault("APP_DEBUG", false), config.MustGetString("SENTRY_DSN"))

dbClient := bootstrap.CreateDBClient(logger)
redisClient := bootstrap.CreateRedisClient(logger)
clientProvider := bootstrap.CreateClientProvider(logger, nil)
corporationService := corporation.NewService(logger, bootstrap.DB, bootstrap.RDB)
userService := user.NewService(logger, bootstrap.DB, bootstrap.RDB, config.MustGetString("AES_SECRET"), clientProvider, corporationService)
corporationService := corporation.NewService(logger, dbClient, redisClient)
userService := user.NewService(logger, dbClient, redisClient, config.MustGetString("AES_SECRET"), clientProvider, corporationService)

offerList := make(chan corporation.OfferList)
offerList := make(chan entity.OfferList)
// subscribe to pub/sub messages inside a new goroutine
go corporationService.SubscribeOffers(offerList)
go func(offerList chan entity.OfferList) {
if err := corporationService.SubscribeOffers(offerList); err != nil {
logger.Sugar().Fatal(err)
}
}(offerList)

for o := range offerList {
if err := userService.MatchOffer(o); err != nil {
Expand Down
30 changes: 21 additions & 9 deletions cmd/tools/customer-delete/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package main

import (
"os"
"strings"

"github.com/woningfinder/woningfinder/internal/services/corporation"
"github.com/woningfinder/woningfinder/internal/services/user"

"github.com/woningfinder/woningfinder/pkg/logging"

"github.com/woningfinder/woningfinder/internal/bootstrap"
"github.com/woningfinder/woningfinder/internal/corporation"
"github.com/woningfinder/woningfinder/internal/user"
"github.com/woningfinder/woningfinder/pkg/config"

"github.com/joho/godotenv"
Expand All @@ -19,22 +23,30 @@ func init() {
if err := godotenv.Load("../../../.env"); err != nil {
_ = config.MustGetString("APP_NAME")
}

// register m2m models with go-pg
bootstrap.RegisterModel()
}

func main() {
logger := logging.NewZapLogger()
logger := logging.NewZapLogger(config.GetBoolOrDefault("APP_DEBUG", false), config.MustGetString("SENTRY_DSN"))

err := bootstrap.InitDB()
if err != nil {
logger.Sugar().Fatal(err)
// read email to delete from arguments
if len(os.Args) != 2 {
logger.Sugar().Fatal("customer-delete must have an user email as (only) argument\n")
}
email := os.Args[0]
if email == "" || !strings.Contains(email, "@") {
logger.Sugar().Fatal("incorrect argument for user to delete, have %s, expect a correct email", email)
}

dbClient := bootstrap.CreateDBClient(logger)
clientProvider := bootstrap.CreateClientProvider(logger, nil)
corporationService := corporation.NewService(logger, bootstrap.DB, nil)
userService := user.NewService(logger, bootstrap.DB, bootstrap.RDB, config.MustGetString("AES_SECRET"), clientProvider, corporationService)
corporationService := corporation.NewService(logger, dbClient, nil)
userService := user.NewService(logger, dbClient, nil, config.MustGetString("AES_SECRET"), clientProvider, corporationService)

// get user
u, err := userService.GetUser("PLACEHOLDER_EMAIL_TO_DELETE")
u, err := userService.GetUser(email)
if err != nil {
logger.Sugar().Fatal(err)
}
Expand Down
53 changes: 0 additions & 53 deletions cmd/tools/db-initiator/main.go

This file was deleted.

45 changes: 45 additions & 0 deletions cmd/tools/db-migration/1_initial.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"github.com/go-pg/migrations/v8"
"github.com/go-pg/pg/v10/orm"
"github.com/woningfinder/woningfinder/internal/bootstrap"
"github.com/woningfinder/woningfinder/internal/domain/entity"
)

func init() {
// register m2m models with go-pg
bootstrap.RegisterModel()

// models
models := []interface{}{
(*entity.CorporationCity)(nil),
(*entity.CorporationSelectionMethod)(nil),
(*entity.HousingPreferencesHousingType)(nil),
(*entity.HousingPreferencesCity)(nil),

(*entity.Corporation)(nil),
(*entity.SelectionMethod)(nil),
(*entity.HousingType)(nil),
(*entity.City)(nil),
(*entity.User)(nil),
(*entity.Tier)(nil),
(*entity.HousingPreferences)(nil),
(*entity.HousingPreferencesCityDistrict)(nil),
(*entity.HousingPreferencesMatch)(nil),
(*entity.CorporationCredentials)(nil),
}

migrations.MustRegisterTx(func(db migrations.DB) error {
for _, model := range models {
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
IfNotExists: true,
})
if err != nil {
return err
}
}

return nil
})
}
Loading

0 comments on commit 5e51304

Please sign in to comment.