-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update app to use ctx for endpoint handlers
- fix tests - de-pointify a lot of stuff because reasons - add OPTIONS method for license endpoint - remove unneeded env flag from Make compose commands
- Loading branch information
1 parent
a204660
commit 0ef45e5
Showing
16 changed files
with
553 additions
and
620 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pg-data/ | ||
scripts/ | ||
scripts/ | ||
.env* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,12 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"os" | ||
"purity-vision-filter/src" | ||
"strconv" | ||
|
||
"github.com/joho/godotenv" | ||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
var portFlag int | ||
|
||
func main() { | ||
if err := godotenv.Load(); err != nil { | ||
log.Fatal().Err(err) | ||
} | ||
|
||
if err := src.InitConfig(); err != nil { | ||
log.Fatal().Msg(err.Error()) | ||
} | ||
|
||
flag.IntVar(&portFlag, "port", src.DefaultPort, "port to run the service on") | ||
flag.Parse() | ||
|
||
logLevel, err := strconv.Atoi(src.LogLevel) | ||
if err != nil { | ||
panic(err) | ||
} | ||
zerolog.SetGlobalLevel(zerolog.Level(logLevel)) | ||
|
||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, NoColor: true}).With().Caller().Logger() | ||
|
||
conn, err := src.InitDB(src.DBName) | ||
if err != nil { | ||
log.Fatal().Msg(err.Error()) | ||
} | ||
defer conn.Close() | ||
|
||
s := src.NewServe() | ||
s.InitServer(portFlag, conn) | ||
godotenv.Load() | ||
src.InitServer() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,97 +8,73 @@ import ( | |
"github.com/rs/zerolog" | ||
) | ||
|
||
var ( | ||
// DefaultDBName is the default name of the database. | ||
DefaultDBName = "purity" | ||
|
||
// DefaultDBTestName is the default name of the test database. | ||
DefaultDBTestName = "purity_test" | ||
|
||
// DefaultPort is the default port to expose the API server. | ||
DefaultPort int = 8080 | ||
|
||
// DBHost is the host machine running the postgres instance. | ||
DBHost string | ||
|
||
// DBPort is the port that exposes the db server. | ||
DBPort string | ||
|
||
// DBName is the postgres database name. | ||
DBName string | ||
|
||
// DBUser is the postgres user account. | ||
DBUser string | ||
|
||
// DBPassword is the password for the DBUser postgres account. | ||
DBPassword string | ||
|
||
// DBSSLMode sets the SSL mode of the postgres client. | ||
DBSSLMode string | ||
|
||
// LogLevel is the level of logging for the application. | ||
LogLevel string | ||
|
||
// StripeKey is for making Stripe API requests. | ||
StripeKey string | ||
|
||
// Name on email license delivery. | ||
EmailName string | ||
|
||
// SendgridAPIKey is for sending emails. | ||
SendgridAPIKey string | ||
|
||
// Stripe webhook secret. | ||
StripeWebhookSecret string | ||
|
||
// From address for email license delivery. | ||
EmailFrom string | ||
|
||
// TrialLicenseMaxUsage is the maximum image filters for a trial license. | ||
TrialLicenseMaxUsage int = 1000 | ||
) | ||
|
||
func InitConfig() error { | ||
DefaultPort = 8080 | ||
|
||
DBHost = getEnvWithDefault("PURITY_DB_HOST", "localhost") | ||
DBPort = getEnvWithDefault("PURITY_DB_PORT", "5432") | ||
DBName = getEnvWithDefault("PURITY_DB_NAME", DefaultDBName) | ||
DBUser = getEnvWithDefault("PURITY_DB_USER", "postgres") | ||
DBPassword = getEnvWithDefault("PURITY_DB_PASS", "") | ||
DBSSLMode = getEnvWithDefault("PURITY_DB_SSL_MODE", "disable") | ||
type Config struct { | ||
DBHost string // DBHost is the host machine running the postgres instance. | ||
DBPort string // DBPort is the port that exposes the db server. | ||
DBName string // DBName is the postgres database name. | ||
DBUser string // DBUser is the postgres user account. | ||
DBPassword string // DBPassword is the password for the DBUser postgres account. | ||
DBSSLMode string // DBSSLMode sets the SSL mode of the postgres client. | ||
LogLevel string // LogLevel is the level of logging for the application. | ||
StripeKey string // StripeKey is for making Stripe API requests. | ||
EmailName string // Name on email license delivery. | ||
SendgridAPIKey string // SendgridAPIKey is for sending emails. | ||
StripeWebhookSecret string // Stripe webhook secret. | ||
EmailFrom string // From address for email license delivery. | ||
TrialLicenseMaxUsage int // TrialLicenseMaxUsage is the maximum image filters for a trial license. | ||
} | ||
|
||
LogLevel = getEnvWithDefault("PURITY_LOG_LEVEL", strconv.Itoa(int(zerolog.InfoLevel))) | ||
func missingEnvErr(envVar string) error { | ||
return fmt.Errorf("%s not found in environment", envVar) | ||
} | ||
|
||
missingEnvErr := func(envVar string) error { | ||
return fmt.Errorf("%s not found in environment", envVar) | ||
} | ||
func newConfig() (Config, error) { | ||
var ( | ||
StripeKey = os.Getenv("STRIPE_KEY") | ||
StripeWebhookSecret = os.Getenv("STRIPE_WEBHOOK_SECRET") | ||
EmailName = getEnvWithDefault("EMAIL_NAME", "John Doe") | ||
EmailFrom = getEnvWithDefault("EMAIL_FROM", "[email protected]") | ||
SendgridAPIKey = os.Getenv("SENDGRID_API_KEY") | ||
) | ||
|
||
if os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") == "" { | ||
return missingEnvErr("GOOGLE_APPLICATION_CREDENTIALS") | ||
return Config{}, missingEnvErr("GOOGLE_APPLICATION_CREDENTIALS") | ||
} | ||
|
||
if StripeKey = os.Getenv("STRIPE_KEY"); StripeKey == "" { | ||
return missingEnvErr("STRIPE_KEY") | ||
if StripeKey == "" { | ||
return Config{}, missingEnvErr("STRIPE_KEY") | ||
} | ||
|
||
if StripeWebhookSecret = os.Getenv("STRIPE_WEBHOOK_SECRET"); StripeWebhookSecret == "" { | ||
return missingEnvErr("STRIPE_WEBHOOK_SECRET") | ||
if StripeWebhookSecret == "" { | ||
return Config{}, missingEnvErr("STRIPE_WEBHOOK_SECRET") | ||
} | ||
|
||
if EmailName = getEnvWithDefault("EMAIL_NAME", "John Doe"); EmailName == "" { | ||
return missingEnvErr("EMAIL_NAME") | ||
if EmailName == "" { | ||
return Config{}, missingEnvErr("EMAIL_NAME") | ||
} | ||
|
||
if EmailFrom = getEnvWithDefault("EMAIL_FROM", "[email protected]"); EmailFrom == "" { | ||
return missingEnvErr("EMAIL_FROM") | ||
if EmailFrom == "" { | ||
return Config{}, missingEnvErr("EMAIL_FROM") | ||
} | ||
|
||
if SendgridAPIKey = os.Getenv("SENDGRID_API_KEY"); SendgridAPIKey == "" { | ||
return missingEnvErr("SENDGRID_API_KEY") | ||
if SendgridAPIKey == "" { | ||
return Config{}, missingEnvErr("SENDGRID_API_KEY") | ||
} | ||
|
||
return nil | ||
return Config{ | ||
DBHost: getEnvWithDefault("PURITY_DB_HOST", "localhost"), | ||
DBPort: getEnvWithDefault("PURITY_DB_PORT", "5432"), | ||
DBName: getEnvWithDefault("PURITY_DB_NAME", "purity"), | ||
DBUser: getEnvWithDefault("PURITY_DB_USER", "postgres"), | ||
DBPassword: getEnvWithDefault("PURITY_DB_PASS", ""), | ||
DBSSLMode: getEnvWithDefault("PURITY_DB_SSL_MODE", "disable"), | ||
LogLevel: getEnvWithDefault("PURITY_LOG_LEVEL", strconv.Itoa(int(zerolog.InfoLevel))), | ||
StripeKey: StripeKey, | ||
StripeWebhookSecret: StripeWebhookSecret, | ||
EmailName: EmailName, | ||
EmailFrom: EmailFrom, | ||
SendgridAPIKey: SendgridAPIKey, | ||
}, nil | ||
} | ||
|
||
func getEnvWithDefault(name string, def string) string { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.