-
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.
feat: add config file & postgres driver
- Loading branch information
1 parent
08a29a4
commit f3e5eb6
Showing
19 changed files
with
454 additions
and
74 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
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
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package config | ||
|
||
type Config struct { | ||
Database Database `mapstructure:"db"` | ||
} | ||
|
||
type DatabaseType string | ||
|
||
const ( | ||
SqliteDatabaseType = "sqlite" | ||
PostgresqlDatabaseType = "postgresql" | ||
) | ||
|
||
type Database struct { | ||
Type DatabaseType `mapstructure:"type"` | ||
DSN string `mapstructure:"dsn"` | ||
} |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package db | ||
|
||
import ( | ||
"errors" | ||
"github.com/pixlcrashr/stwhh-mensa/pkg/config" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
"time" | ||
) | ||
|
||
func FromConfig(c config.Database) (*gorm.DB, error) { | ||
var dialector gorm.Dialector | ||
|
||
switch c.Type { | ||
case config.SqliteDatabaseType: | ||
dialector = sqlite.Open(c.DSN) | ||
break | ||
case config.PostgresqlDatabaseType: | ||
dialector = postgres.Open(c.DSN) | ||
break | ||
default: | ||
return nil, errors.New("invalid database type specified") | ||
} | ||
|
||
return gorm.Open( | ||
dialector, | ||
&gorm.Config{ | ||
NowFunc: func() time.Time { | ||
return time.Now().UTC() | ||
}, | ||
}, | ||
) | ||
} |
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,4 +1,4 @@ | ||
package logger | ||
package logging | ||
|
||
import "go.uber.org/zap" | ||
|
||
|
Oops, something went wrong.