feat: add password auth flag

This commit is contained in:
Steven
2024-07-27 19:24:37 +08:00
parent b9006f8ce0
commit 8bf7cdfd31
14 changed files with 236 additions and 216 deletions

View File

@@ -8,30 +8,29 @@ import (
"strings"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/usememos/memos/server/version"
)
// Profile is the configuration to start main server.
type Profile struct {
// Mode can be "prod" or "dev" or "demo"
Mode string `json:"mode"`
Mode string
// Addr is the binding address for server
Addr string `json:"-"`
Addr string
// Port is the binding port for server
Port int `json:"-"`
Port int
// Data is the data directory
Data string `json:"-"`
Data string
// DSN points to where memos stores its own data
DSN string `json:"-"`
DSN string
// Driver is the database driver
// sqlite, mysql
Driver string `json:"-"`
Driver string
// Version is the current version of server
Version string `json:"version"`
Version string
// Pubic is the flag whether the instance is public for others.
Public bool `json:"public"`
Public bool
// PasswordAuth is the flag whether the instance uses password authentication.
PasswordAuth bool
}
func (p *Profile) IsDev() bool {
@@ -57,45 +56,36 @@ func checkDataDir(dataDir string) (string, error) {
return dataDir, nil
}
// GetProfile will return a profile for dev or prod.
func GetProfile() (*Profile, error) {
profile := Profile{}
err := viper.Unmarshal(&profile)
if err != nil {
return nil, err
func (p *Profile) Validate() error {
if p.Mode != "demo" && p.Mode != "dev" && p.Mode != "prod" {
p.Mode = "demo"
}
if profile.Mode != "demo" && profile.Mode != "dev" && profile.Mode != "prod" {
profile.Mode = "demo"
}
if profile.Mode == "prod" && profile.Data == "" {
if p.Mode == "prod" && p.Data == "" {
if runtime.GOOS == "windows" {
profile.Data = filepath.Join(os.Getenv("ProgramData"), "memos")
if _, err := os.Stat(profile.Data); os.IsNotExist(err) {
if err := os.MkdirAll(profile.Data, 0770); err != nil {
fmt.Printf("Failed to create data directory: %s, err: %+v\n", profile.Data, err)
return nil, err
p.Data = filepath.Join(os.Getenv("ProgramData"), "memos")
if _, err := os.Stat(p.Data); os.IsNotExist(err) {
if err := os.MkdirAll(p.Data, 0770); err != nil {
fmt.Printf("Failed to create data directory: %s, err: %+v\n", p.Data, err)
return err
}
}
} else {
profile.Data = "/var/opt/memos"
p.Data = "/var/opt/memos"
}
}
dataDir, err := checkDataDir(profile.Data)
dataDir, err := checkDataDir(p.Data)
if err != nil {
fmt.Printf("Failed to check dsn: %s, err: %+v\n", dataDir, err)
return nil, err
return err
}
profile.Data = dataDir
if profile.Driver == "sqlite" && profile.DSN == "" {
dbFile := fmt.Sprintf("memos_%s.db", profile.Mode)
profile.DSN = filepath.Join(dataDir, dbFile)
p.Data = dataDir
if p.Driver == "sqlite" && p.DSN == "" {
dbFile := fmt.Sprintf("memos_%s.db", p.Mode)
p.DSN = filepath.Join(dataDir, dbFile)
}
profile.Version = version.GetCurrentVersion(profile.Mode)
return &profile, nil
return nil
}