Skip to content

Commit

Permalink
fixes after refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
yakuter committed May 3, 2020
1 parent 5a1934d commit 82aff0b
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 50 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ These environment variables are accepted:

**Database Variables**
- PW_DB_DRIVER
- PW_DB_DBNAME
- PW_DB_NAME
- PW_DB_USERNAME
- PW_DB_PASSWORD
- PW_DB_HOST
- PW_DB_PORT
- PW_DB_PATH

**Backup Variables**
- PW_BACKUP_FOLDER
Expand Down
9 changes: 8 additions & 1 deletion cmd/passwall-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
"time"

_ "github.com/heroku/x/hmetrics/onload"
"github.com/pass-wall/passwall-server/internal/api"
"github.com/pass-wall/passwall-server/internal/app"
"github.com/pass-wall/passwall-server/internal/config"
"github.com/pass-wall/passwall-server/internal/router"
"github.com/pass-wall/passwall-server/internal/storage"
)

func main() {
cfg := setupConfigDefaults()
cfg := config.SetupConfigDefaults()

logger := log.New(os.Stdout, "[passwall-server] ", 0)
logger.Printf("listening on %s", cfg.Server.Port)
Expand All @@ -23,6 +25,11 @@ func main() {
logger.Fatalf("failed to open storage: %s\n", err)
}

// Migrate database tables
// TODO: Migrate should be in storege.New functions of categories
api.MigrateTables(s)

// Start cron jobs like backup
app.StartCronJob(s)

srv := &http.Server{
Expand Down
15 changes: 1 addition & 14 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,4 @@ services:
ports:
- 3625:3625
volumes:
- ./store:/app/store

# passwall-web:
# container_name: passwall-web
# image: passwall-web:latest
# restart: always
# build:
# context: frontend
# ports:
# - 3000:3000

# Uncomment volumes section if you want to store persist data
# volumes:
# passwall-db:
- ./store:/app/store
26 changes: 23 additions & 3 deletions internal/api/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -142,7 +143,7 @@ func Backup(s storage.Store) http.HandlerFunc {
}
}

// List all backups
// ListBackup all backups
func ListBackup(w http.ResponseWriter, r *http.Request) {
backupFiles, err := app.GetBackupFiles()

Expand All @@ -161,7 +162,22 @@ func ListBackup(w http.ResponseWriter, r *http.Request) {

// MigrateTables runs auto migration for the models, will only add missing fields
// won't delete/change current data in the store.
func MigrateTables(s storage.Store) http.HandlerFunc {
func MigrateTables(s storage.Store) {
if err := s.Logins().Migrate(); err != nil {
log.Println(err)
}
if err := s.CreditCards().Migrate(); err != nil {
log.Println(err)
}
if err := s.BankAccounts().Migrate(); err != nil {
log.Println(err)
}
if err := s.Notes().Migrate(); err != nil {
log.Println(err)
}
}

/* func MigrateTables(s storage.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := s.Logins().Migrate(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -175,8 +191,12 @@ func MigrateTables(s storage.Store) http.HandlerFunc {
w.WriteHeader(http.StatusInternalServerError)
return
}
if err := s.Notes().Migrate(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
}
} */

func upload(r *http.Request) (*os.File, error) {

Expand Down
32 changes: 23 additions & 9 deletions cmd/passwall-server/configuration.go → internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package main
package config

import (
"crypto/rand"
"encoding/base64"
"log"
"os"

"github.com/pass-wall/passwall-server/internal/storage"
"github.com/spf13/viper"
)

// Configuration ...
type Configuration struct {
Server ServerConfiguration
Database storage.Configuration
Database DatabaseConfiguration
}

// ServerConfiguration is the required paramters to set up a server
type ServerConfiguration struct {
Port string `default:"3625"`
Username string `default:"passwall"`
Expand All @@ -25,7 +25,20 @@ type ServerConfiguration struct {
Timeout int `default:"24"`
}

func setupConfigDefaults() *Configuration {
// DatabaseConfiguration is the required paramters to set up a DB instance
type DatabaseConfiguration struct {
Driver string `default:"3625"`
Name string `default:"passwall"`
Username string `default:"user"`
Password string `default:"password"`
Host string `default:"localhost"`
Port string `default:"5432"`
Path string `default:"./store/passwall.db"`
LogMode bool `default:"false"`
}

// SetupConfigDefaults ...
func SetupConfigDefaults() *Configuration {

var configuration *Configuration

Expand All @@ -35,9 +48,11 @@ func setupConfigDefaults() *Configuration {

// Bind environment variables
bindEnvs()

// Set default values
setDefaults()

// Auto generate config.yml file if it doesn't exist
if !fileExists("./store/config.yml") {
viper.Set("server.passphrase", generateSecureKey())
viper.Set("server.secret", generateSecureKey())
Expand All @@ -58,8 +73,6 @@ func setupConfigDefaults() *Configuration {
}

func bindEnvs() {
viper.SetEnvPrefix("PW")

viper.BindEnv("server.port", "PORT")
viper.BindEnv("server.username", "PW_SERVER_USERNAME")
viper.BindEnv("server.password", "PW_SERVER_PASSWORD")
Expand All @@ -72,12 +85,13 @@ func bindEnvs() {
viper.BindEnv("server.refreshTokenExpireDuration", "PW_SERVER_REFRESH_TOKEN_EXPIRE_DURATION")

viper.BindEnv("database.driver", "PW_DB_DRIVER")
viper.BindEnv("database.dbname", "PW_DB_DBNAME")
viper.BindEnv("database.name", "PW_DB_NAME")
viper.BindEnv("database.username", "PW_DB_USERNAME")
viper.BindEnv("database.password", "PW_DB_PASSWORD")
viper.BindEnv("database.host", "PW_DB_HOST")
viper.BindEnv("database.port", "PW_DB_PORT")
viper.BindEnv("database.dbpath", "PW_DB_PATH")
viper.BindEnv("database.path", "PW_DB_PATH")
viper.BindEnv("database.logmode", "PW_DB_LOG_MODE")

viper.BindEnv("backup.folder", "PW_BACKUP_FOLDER")
viper.BindEnv("backup.rotation", "PW_BACKUP_ROTATION")
Expand All @@ -97,7 +111,7 @@ func setDefaults() {
viper.SetDefault("server.refreshTokenExpireDuration", "15d")

viper.SetDefault("database.driver", "sqlite")
viper.SetDefault("database.dbname", "passwall")
viper.SetDefault("database.name", "passwall")
viper.SetDefault("database.username", "user")
viper.SetDefault("database.password", "password")
viper.SetDefault("database.host", "localhost")
Expand Down
12 changes: 7 additions & 5 deletions internal/storage/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/pass-wall/passwall-server/internal/config"
"github.com/pass-wall/passwall-server/internal/storage/bankaccount"
"github.com/pass-wall/passwall-server/internal/storage/creditcard"
"github.com/pass-wall/passwall-server/internal/storage/login"
Expand All @@ -24,27 +25,28 @@ type Database struct {
}

// New opens a database according to configuration.
func New(cfg *Configuration) (*Database, error) {
func New(cfg *config.DatabaseConfiguration) (*Database, error) {
var db *gorm.DB
var err error

switch cfg.Driver {
case "sqlite":
path := cfg.DBPath
if cfg.DBPath == "" {
path := cfg.Path

if cfg.Path == "" {
return nil, errors.New("sqlite db path should not be empty")
}
db, err = gorm.Open("sqlite3", path)
if err != nil {
return nil, fmt.Errorf("could not open sqlite database: %w", err)
}
case "postgres":
db, err = gorm.Open("postgres", "host="+cfg.Host+" port="+cfg.Port+" user="+cfg.Username+" dbname="+cfg.DBName+" sslmode=disable password="+cfg.Password)
db, err = gorm.Open("postgres", "host="+cfg.Host+" port="+cfg.Port+" user="+cfg.Username+" dbname="+cfg.Name+" sslmode=disable password="+cfg.Password)
if err != nil {
return nil, fmt.Errorf("could not open postgresql connection: %w", err)
}
case "mysql":
db, err = gorm.Open("mysql", cfg.Username+":"+cfg.Password+"@tcp("+cfg.Host+":"+cfg.Port+")/"+cfg.DBName+"?charset=utf8&parseTime=True&loc=Local")
db, err = gorm.Open("mysql", cfg.Username+":"+cfg.Password+"@tcp("+cfg.Host+":"+cfg.Port+")/"+cfg.Name+"?charset=utf8&parseTime=True&loc=Local")
if err != nil {
return nil, fmt.Errorf("could not open mysql connection: %w", err)
}
Expand Down
13 changes: 0 additions & 13 deletions internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,3 @@ type Store interface {
BankAccounts() BankAccountRepository
Notes() NoteRepository
}

// Configuration is the required paramters to set up a DB instance
// Default value is set on configuration.go
type Configuration struct {
Driver string `default:"3625"`
DBName string `default:"passwall"`
Username string `default:"user"`
Password string `default:"password"`
Host string `default:"localhost"`
Port string `default:"5432"`
DBPath string `default:"./store/passwall.db"`
LogMode bool `default:"false"`
}
9 changes: 5 additions & 4 deletions store/config-sample.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
database:
driver: "sqlite"
dbname: "passwall"
name: "passwall"
username: "user"
password: "password"
dbpath: "./store/passwall.db"
path: "./store/passwall.db"
logmod: false

# Uncomment for mysql
# database:
# driver: "mysql"
# dbname: "passwall"
# name: "passwall"
# username: "user"
# password: "password"
# host: "localhost" # or compose service name
Expand All @@ -17,7 +18,7 @@ database:
# Uncomment for postgres
# database:
# driver: "postgres"
# dbname: "passwall"
# name: "passwall"
# username: "user"
# password: "password"
# host: "localhost" # or compose service name
Expand Down

0 comments on commit 82aff0b

Please sign in to comment.