chore: upgrade version to 0.3.0

This commit is contained in:
Steven 2023-07-21 22:39:21 +08:00
parent b32fdbfc0a
commit 53c1d8fa91
3 changed files with 29 additions and 14 deletions

View File

@ -104,9 +104,7 @@ func JWTMiddleware(server *APIV1Service, next echo.HandlerFunc, secret string) e
} }
return nil, errors.Errorf("unexpected access token kid=%v", t.Header["kid"]) return nil, errors.Errorf("unexpected access token kid=%v", t.Header["kid"])
}) })
if !audienceContains(claims.Audience, auth.AccessTokenAudienceName) {
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Invalid access token, audience mismatch, got %q, expected %q.", claims.Audience, auth.AccessTokenAudienceName))
}
generateToken := time.Until(claims.ExpiresAt.Time) < auth.RefreshThresholdDuration generateToken := time.Until(claims.ExpiresAt.Time) < auth.RefreshThresholdDuration
if err != nil { if err != nil {
var ve *jwt.ValidationError var ve *jwt.ValidationError
@ -122,6 +120,10 @@ func JWTMiddleware(server *APIV1Service, next echo.HandlerFunc, secret string) e
} }
} }
if !audienceContains(claims.Audience, auth.AccessTokenAudienceName) {
return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Invalid access token, audience mismatch, got %q, expected %q.", claims.Audience, auth.AccessTokenAudienceName))
}
// We either have a valid access token or we will attempt to generate new access token and refresh token // We either have a valid access token or we will attempt to generate new access token and refresh token
ctx := c.Request().Context() ctx := c.Request().Context()
userID, err := strconv.Atoi(claims.Subject) userID, err := strconv.Atoi(claims.Subject)

View File

@ -9,10 +9,10 @@ import (
// Version is the service current released version. // Version is the service current released version.
// Semantic versioning: https://semver.org/ // Semantic versioning: https://semver.org/
var Version = "0.2.0" var Version = "0.3.0"
// DevVersion is the service current development version. // DevVersion is the service current development version.
var DevVersion = "0.2.0" var DevVersion = "0.3.0"
func GetCurrentVersion(mode string) string { func GetCurrentVersion(mode string) string {
if mode == "dev" || mode == "demo" { if mode == "dev" || mode == "demo" {

View File

@ -23,12 +23,12 @@ var migrationFS embed.FS
var seedFS embed.FS var seedFS embed.FS
type DB struct { type DB struct {
profile *profile.Profile
// sqlite db connection instance // sqlite db connection instance
DBInstance *sql.DB DBInstance *sql.DB
profile *profile.Profile
} }
// NewDB returns a new instance of DB. // NewDB returns a new instance of DB associated with the given datasource name.
func NewDB(profile *profile.Profile) *DB { func NewDB(profile *profile.Profile) *DB {
db := &DB{ db := &DB{
profile: profile, profile: profile,
@ -42,8 +42,21 @@ func (db *DB) Open(ctx context.Context) (err error) {
return fmt.Errorf("dsn required") return fmt.Errorf("dsn required")
} }
// Connect to the database without foreign_key. // Connect to the database with some sane settings:
sqliteDB, err := sql.Open("sqlite", db.profile.DSN+"?cache=shared&_foreign_keys=0&_journal_mode=WAL") // - No shared-cache: it's obsolete; WAL journal mode is a better solution.
// - No foreign key constraints: it's currently disabled by default, but it's a
// good practice to be explicit and prevent future surprises on SQLite upgrades.
// - Journal mode set to WAL: it's the recommended journal mode for most applications
// as it prevents locking issues.
//
// Notes:
// - When using the `modernc.org/sqlite` driver, each pragma must be prefixed with `_pragma=`.
//
// References:
// - https://pkg.go.dev/modernc.org/sqlite#Driver.Open
// - https://www.sqlite.org/sharedcache.html
// - https://www.sqlite.org/pragma.html
sqliteDB, err := sql.Open("sqlite", db.profile.DSN+"?_pragma=foreign_keys(0)&_pragma=busy_timeout(10000)&_pragma=journal_mode(WAL)")
if err != nil { if err != nil {
return fmt.Errorf("failed to open db with dsn: %s, err: %w", db.profile.DSN, err) return fmt.Errorf("failed to open db with dsn: %s, err: %w", db.profile.DSN, err)
} }
@ -52,16 +65,16 @@ func (db *DB) Open(ctx context.Context) (err error) {
if db.profile.Mode == "prod" { if db.profile.Mode == "prod" {
_, err := os.Stat(db.profile.DSN) _, err := os.Stat(db.profile.DSN)
if err != nil { if err != nil {
// If db file not exists, we should apply the latest schema. // If db file not exists, we should create a new one with latest schema.
if errors.Is(err, os.ErrNotExist) { if errors.Is(err, os.ErrNotExist) {
if err := db.applyLatestSchema(ctx); err != nil { if err := db.applyLatestSchema(ctx); err != nil {
return fmt.Errorf("failed to apply latest schema: %w", err) return fmt.Errorf("failed to apply latest schema, err: %w", err)
} }
} else { } else {
return fmt.Errorf("failed to check database file: %w", err) return fmt.Errorf("failed to get db file stat, err: %w", err)
} }
} else { } else {
// If db file exists, we should check the migration history and apply the migration if needed. // If db file exists, we should check if we need to migrate the database.
currentVersion := version.GetCurrentVersion(db.profile.Mode) currentVersion := version.GetCurrentVersion(db.profile.Mode)
migrationHistoryList, err := db.FindMigrationHistoryList(ctx, &MigrationHistoryFind{}) migrationHistoryList, err := db.FindMigrationHistoryList(ctx, &MigrationHistoryFind{})
if err != nil { if err != nil {
@ -177,7 +190,7 @@ func (db *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion st
} }
} }
// upsert the newest version to migration_history // Upsert the newest version to migration_history.
version := minorVersion + ".0" version := minorVersion + ".0"
if _, err = db.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{ if _, err = db.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{
Version: version, Version: version,