You've already forked golang-saas-starter-kit
mirror of
https://github.com/raseels-repos/golang-saas-starter-kit.git
synced 2025-08-06 22:32:51 +02:00
Reduce countries loaded for geonames to US only by default.
Schema loads in around 1min now.
This commit is contained in:
@ -11,6 +11,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web/webcontext"
|
||||||
"github.com/huandu/go-sqlbuilder"
|
"github.com/huandu/go-sqlbuilder"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -24,9 +25,12 @@ const (
|
|||||||
geonamesTableName = "geonames"
|
geonamesTableName = "geonames"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
// List of country codes that will geonames will be downloaded for.
|
||||||
// List of country codes that will geonames will be downloaded for.
|
func ValidGeonameCountries(ctx context.Context) []string {
|
||||||
ValidGeonameCountries = []string{
|
if webcontext.ContextEnv(ctx) == webcontext.Env_Dev {
|
||||||
|
return []string{"US"}
|
||||||
|
}
|
||||||
|
return []string{
|
||||||
"AD", "AR", "AS", "AT", "AU", "AX", "BD", "BE", "BG", "BM",
|
"AD", "AR", "AS", "AT", "AU", "AX", "BD", "BE", "BG", "BM",
|
||||||
"BR", "BY", "CA", "CH", "CO", "CR", "CZ", "DE", "DK", "DO",
|
"BR", "BY", "CA", "CH", "CO", "CR", "CZ", "DE", "DK", "DO",
|
||||||
"DZ", "ES", "FI", "FO", "FR", "GB", "GF", "GG", "GL", "GP",
|
"DZ", "ES", "FI", "FO", "FR", "GB", "GF", "GG", "GL", "GP",
|
||||||
@ -36,7 +40,7 @@ var (
|
|||||||
"PK", "PL", "PM", "PR", "PT", "RE", "RO", "RU", "SE", "SI",
|
"PK", "PL", "PM", "PR", "PT", "RE", "RO", "RU", "SE", "SI",
|
||||||
"SJ", "SK", "SM", "TH", "TR", "UA", "US", "UY", "VA", "VI",
|
"SJ", "SK", "SM", "TH", "TR", "UA", "US", "UY", "VA", "VI",
|
||||||
"WF", "YT", "ZA"}
|
"WF", "YT", "ZA"}
|
||||||
)
|
}
|
||||||
|
|
||||||
// FindGeonames ....
|
// FindGeonames ....
|
||||||
func FindGeonames(ctx context.Context, dbConn *sqlx.DB, orderBy, where string, args ...interface{}) ([]*Geoname, error) {
|
func FindGeonames(ctx context.Context, dbConn *sqlx.DB, orderBy, where string, args ...interface{}) ([]*Geoname, error) {
|
||||||
@ -194,7 +198,7 @@ func LoadGeonames(ctx context.Context, rr chan<- interface{}, countries ...strin
|
|||||||
defer close(rr)
|
defer close(rr)
|
||||||
|
|
||||||
if len(countries) == 0 {
|
if len(countries) == 0 {
|
||||||
countries = ValidGeonameCountries
|
countries = ValidGeonameCountries(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, country := range countries {
|
for _, country := range countries {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package schema
|
package schema
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
@ -8,7 +9,7 @@ import (
|
|||||||
|
|
||||||
// initSchema runs before any migrations are executed. This happens when no other migrations
|
// initSchema runs before any migrations are executed. This happens when no other migrations
|
||||||
// have previously been executed.
|
// have previously been executed.
|
||||||
func initSchema(db *sqlx.DB, log *log.Logger, isUnittest bool) func(*sqlx.DB) error {
|
func initSchema(ctx context.Context, db *sqlx.DB, log *log.Logger, isUnittest bool) func(*sqlx.DB) error {
|
||||||
f := func(db *sqlx.DB) error {
|
f := func(db *sqlx.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ import (
|
|||||||
|
|
||||||
// migrationList returns a list of migrations to be executed. If the id of the
|
// migrationList returns a list of migrations to be executed. If the id of the
|
||||||
// migration already exists in the migrations table it will be skipped.
|
// migration already exists in the migrations table it will be skipped.
|
||||||
func migrationList(db *sqlx.DB, log *log.Logger, isUnittest bool) []*sqlxmigrate.Migration {
|
func migrationList(ctx context.Context, db *sqlx.DB, log *log.Logger, isUnittest bool) []*sqlxmigrate.Migration {
|
||||||
return []*sqlxmigrate.Migration{
|
return []*sqlxmigrate.Migration{
|
||||||
// Create table users.
|
// Create table users.
|
||||||
{
|
{
|
||||||
@ -213,7 +213,7 @@ func migrationList(db *sqlx.DB, log *log.Logger, isUnittest bool) []*sqlxmigrate
|
|||||||
},
|
},
|
||||||
// Load new geonames table.
|
// Load new geonames table.
|
||||||
{
|
{
|
||||||
ID: "20190731-02b",
|
ID: "20190731-02h",
|
||||||
Migrate: func(tx *sql.Tx) error {
|
Migrate: func(tx *sql.Tx) error {
|
||||||
|
|
||||||
schemas := []string{
|
schemas := []string{
|
||||||
@ -253,7 +253,7 @@ func migrationList(db *sqlx.DB, log *log.Logger, isUnittest bool) []*sqlxmigrate
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
resChan := make(chan interface{})
|
resChan := make(chan interface{})
|
||||||
go geonames.LoadGeonames(context.Background(), resChan)
|
go geonames.LoadGeonames(ctx, resChan)
|
||||||
|
|
||||||
for r := range resChan {
|
for r := range resChan {
|
||||||
switch v := r.(type) {
|
switch v := r.(type) {
|
||||||
@ -288,7 +288,7 @@ func migrationList(db *sqlx.DB, log *log.Logger, isUnittest bool) []*sqlxmigrate
|
|||||||
},
|
},
|
||||||
// Load new countries table.
|
// Load new countries table.
|
||||||
{
|
{
|
||||||
ID: "20190731-02d",
|
ID: "20190731-02f",
|
||||||
Migrate: func(tx *sql.Tx) error {
|
Migrate: func(tx *sql.Tx) error {
|
||||||
|
|
||||||
schemas := []string{
|
schemas := []string{
|
||||||
@ -489,7 +489,7 @@ func migrationList(db *sqlx.DB, log *log.Logger, isUnittest bool) []*sqlxmigrate
|
|||||||
},
|
},
|
||||||
// Load new country_timezones table.
|
// Load new country_timezones table.
|
||||||
{
|
{
|
||||||
ID: "20190731-03d",
|
ID: "20190731-03e",
|
||||||
Migrate: func(tx *sql.Tx) error {
|
Migrate: func(tx *sql.Tx) error {
|
||||||
|
|
||||||
queries := []string{
|
queries := []string{
|
||||||
|
@ -1,21 +1,22 @@
|
|||||||
package schema
|
package schema
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/geeks-accelerator/sqlxmigrate"
|
"github.com/geeks-accelerator/sqlxmigrate"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Migrate(masterDb *sqlx.DB, log *log.Logger, isUnittest bool) error {
|
func Migrate(ctx context.Context, masterDb *sqlx.DB, log *log.Logger, isUnittest bool) error {
|
||||||
// Load list of Schema migrations and init new sqlxmigrate client
|
// Load list of Schema migrations and init new sqlxmigrate client
|
||||||
migrations := migrationList(masterDb, log, isUnittest)
|
migrations := migrationList(ctx, masterDb, log, isUnittest)
|
||||||
m := sqlxmigrate.New(masterDb, sqlxmigrate.DefaultOptions, migrations)
|
m := sqlxmigrate.New(masterDb, sqlxmigrate.DefaultOptions, migrations)
|
||||||
m.SetLogger(log)
|
m.SetLogger(log)
|
||||||
|
|
||||||
// Append any schema that need to be applied if this is a fresh migration
|
// Append any schema that need to be applied if this is a fresh migration
|
||||||
// ie. the migrations database table does not exist.
|
// ie. the migrations database table does not exist.
|
||||||
m.InitSchema(initSchema(masterDb, log, isUnittest))
|
m.InitSchema(initSchema(ctx, masterDb, log, isUnittest))
|
||||||
|
|
||||||
// Execute the migrations
|
// Execute the migrations
|
||||||
return m.Migrate()
|
return m.Migrate()
|
||||||
|
@ -66,7 +66,7 @@ type ServiceDeployFlags struct {
|
|||||||
DockerFile string `validate:"omitempty" example:"./cmd/web-api/Dockerfile"`
|
DockerFile string `validate:"omitempty" example:"./cmd/web-api/Dockerfile"`
|
||||||
EnableLambdaVPC bool `validate:"omitempty" example:"false"`
|
EnableLambdaVPC bool `validate:"omitempty" example:"false"`
|
||||||
EnableEcsElb bool `validate:"omitempty" example:"false"`
|
EnableEcsElb bool `validate:"omitempty" example:"false"`
|
||||||
IsLambda bool `validate:"omitempty" example:"false"`
|
IsLambda bool `validate:"omitempty" example:"false"`
|
||||||
|
|
||||||
StaticFilesS3Enable bool `validate:"omitempty" example:"false"`
|
StaticFilesS3Enable bool `validate:"omitempty" example:"false"`
|
||||||
StaticFilesImgResizeEnable bool `validate:"omitempty" example:"false"`
|
StaticFilesImgResizeEnable bool `validate:"omitempty" example:"false"`
|
||||||
@ -137,7 +137,7 @@ type serviceDeployRequest struct {
|
|||||||
VpcPublicSubnets []*ec2.CreateSubnetInput
|
VpcPublicSubnets []*ec2.CreateSubnetInput
|
||||||
|
|
||||||
EnableLambdaVPC bool `validate:"omitempty"`
|
EnableLambdaVPC bool `validate:"omitempty"`
|
||||||
IsLambda bool `validate:"omitempty"`
|
IsLambda bool `validate:"omitempty"`
|
||||||
RecreateService bool `validate:"omitempty"`
|
RecreateService bool `validate:"omitempty"`
|
||||||
|
|
||||||
SDNamepsace *servicediscovery.CreatePrivateDnsNamespaceInput
|
SDNamepsace *servicediscovery.CreatePrivateDnsNamespaceInput
|
||||||
@ -194,7 +194,7 @@ func NewServiceDeployRequest(log *log.Logger, flags ServiceDeployFlags) (*servic
|
|||||||
S3BucketPrivateName: flags.S3BucketPrivateName,
|
S3BucketPrivateName: flags.S3BucketPrivateName,
|
||||||
S3BucketPublicName: flags.S3BucketPublicName,
|
S3BucketPublicName: flags.S3BucketPublicName,
|
||||||
|
|
||||||
IsLambda: flags.IsLambda,
|
IsLambda: flags.IsLambda,
|
||||||
EnableLambdaVPC: flags.EnableLambdaVPC,
|
EnableLambdaVPC: flags.EnableLambdaVPC,
|
||||||
EnableEcsElb: flags.EnableEcsElb,
|
EnableEcsElb: flags.EnableEcsElb,
|
||||||
RecreateService: flags.RecreateService,
|
RecreateService: flags.RecreateService,
|
||||||
@ -441,7 +441,7 @@ func NewServiceDeployRequest(log *log.Logger, flags ServiceDeployFlags) (*servic
|
|||||||
|
|
||||||
if req.IsLambda {
|
if req.IsLambda {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -861,7 +861,6 @@ func NewServiceDeployRequest(log *log.Logger, flags ServiceDeployFlags) (*servic
|
|||||||
log.Printf("\t%s\tDefaults set.", tests.Success)
|
log.Printf("\t%s\tDefaults set.", tests.Success)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
r, err := regexp.Compile(`^(\d+)`)
|
r, err := regexp.Compile(`^(\d+)`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.WithStack(err)
|
return nil, errors.WithStack(err)
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"expvar"
|
"expvar"
|
||||||
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web/webcontext"
|
||||||
"log"
|
"log"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"geeks-accelerator/oss/saas-starter-kit/internal/platform/flag"
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/flag"
|
||||||
"geeks-accelerator/oss/saas-starter-kit/internal/schema"
|
"geeks-accelerator/oss/saas-starter-kit/internal/schema"
|
||||||
@ -125,8 +128,16 @@ func main() {
|
|||||||
// =========================================================================
|
// =========================================================================
|
||||||
// Start Migrations
|
// Start Migrations
|
||||||
|
|
||||||
|
// Set the context with the required values to
|
||||||
|
// process the request.
|
||||||
|
v := webcontext.Values{
|
||||||
|
Now: time.Now(),
|
||||||
|
Env: cfg.Env,
|
||||||
|
}
|
||||||
|
ctx := context.WithValue(context.Background(), webcontext.KeyValues, &v)
|
||||||
|
|
||||||
// Execute the migrations
|
// Execute the migrations
|
||||||
if err = schema.Migrate(masterDb, log, false); err != nil {
|
if err = schema.Migrate(ctx, masterDb, log, false); err != nil {
|
||||||
log.Fatalf("main : Migrate : %v", err)
|
log.Fatalf("main : Migrate : %v", err)
|
||||||
}
|
}
|
||||||
log.Printf("main : Migrate : Completed")
|
log.Printf("main : Migrate : Completed")
|
||||||
|
Reference in New Issue
Block a user