2022-05-22 09:29:34 +08:00
|
|
|
package profile
|
2022-02-05 13:04:06 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-05-09 08:16:38 +08:00
|
|
|
"runtime"
|
2022-02-05 13:04:06 +08:00
|
|
|
"strings"
|
2022-06-27 22:09:06 +08:00
|
|
|
|
2023-09-17 22:55:13 +08:00
|
|
|
"github.com/pkg/errors"
|
2023-02-23 00:07:16 +08:00
|
|
|
"github.com/spf13/viper"
|
2023-09-17 22:55:13 +08:00
|
|
|
|
2022-09-12 17:25:34 +08:00
|
|
|
"github.com/usememos/memos/server/version"
|
2022-02-05 13:04:06 +08:00
|
|
|
)
|
|
|
|
|
2022-05-22 09:29:34 +08:00
|
|
|
// Profile is the configuration to start main server.
|
2022-02-05 13:04:06 +08:00
|
|
|
type Profile struct {
|
2023-02-19 13:36:45 +08:00
|
|
|
// Mode can be "prod" or "dev" or "demo"
|
2022-03-29 20:53:43 +08:00
|
|
|
Mode string `json:"mode"`
|
2023-08-24 09:59:23 +08:00
|
|
|
// Addr is the binding address for server
|
|
|
|
Addr string `json:"-"`
|
2022-05-17 21:21:13 +08:00
|
|
|
// Port is the binding port for server
|
2023-02-12 17:29:23 +08:00
|
|
|
Port int `json:"-"`
|
2022-07-09 12:57:08 +08:00
|
|
|
// Data is the data directory
|
2023-02-12 17:29:23 +08:00
|
|
|
Data string `json:"-"`
|
2022-03-29 20:53:43 +08:00
|
|
|
// DSN points to where Memos stores its own data
|
2023-09-28 22:09:52 +08:00
|
|
|
DSN string `json:"dsn"`
|
|
|
|
// Driver is the database driver
|
|
|
|
Driver string `json:"driver"`
|
2022-05-17 21:21:13 +08:00
|
|
|
// Version is the current version of server
|
|
|
|
Version string `json:"version"`
|
2022-02-05 13:04:06 +08:00
|
|
|
}
|
|
|
|
|
2023-02-27 21:26:50 +08:00
|
|
|
func (p *Profile) IsDev() bool {
|
|
|
|
return p.Mode != "prod"
|
|
|
|
}
|
|
|
|
|
2023-09-28 22:09:52 +08:00
|
|
|
func checkDataDir(dataDir string) (string, error) {
|
2022-02-05 13:04:06 +08:00
|
|
|
// Convert to absolute path if relative path is supplied.
|
|
|
|
if !filepath.IsAbs(dataDir) {
|
2023-05-09 08:16:38 +08:00
|
|
|
relativeDir := filepath.Join(filepath.Dir(os.Args[0]), dataDir)
|
|
|
|
absDir, err := filepath.Abs(relativeDir)
|
2022-02-05 13:04:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
dataDir = absDir
|
|
|
|
}
|
|
|
|
|
2023-05-09 08:16:38 +08:00
|
|
|
// Trim trailing \ or / in case user supplies
|
|
|
|
dataDir = strings.TrimRight(dataDir, "\\/")
|
2022-02-05 13:04:06 +08:00
|
|
|
|
|
|
|
if _, err := os.Stat(dataDir); err != nil {
|
2023-09-17 22:55:13 +08:00
|
|
|
return "", errors.Wrapf(err, "unable to access data folder %s", dataDir)
|
2022-02-05 13:04:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return dataDir, nil
|
|
|
|
}
|
|
|
|
|
2023-02-25 20:48:38 +08:00
|
|
|
// GetProfile will return a profile for dev or prod.
|
2022-08-24 21:53:12 +08:00
|
|
|
func GetProfile() (*Profile, error) {
|
2022-07-09 12:57:08 +08:00
|
|
|
profile := Profile{}
|
2023-02-23 00:07:16 +08:00
|
|
|
err := viper.Unmarshal(&profile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-18 22:21:10 +08:00
|
|
|
|
2023-02-19 16:34:15 +08:00
|
|
|
if profile.Mode != "demo" && profile.Mode != "dev" && profile.Mode != "prod" {
|
2023-02-19 13:36:45 +08:00
|
|
|
profile.Mode = "demo"
|
2022-02-18 22:21:10 +08:00
|
|
|
}
|
|
|
|
|
2022-07-09 12:57:08 +08:00
|
|
|
if profile.Mode == "prod" && profile.Data == "" {
|
2023-05-09 08:16:38 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
profile.Data = "/var/opt/memos"
|
|
|
|
}
|
2022-05-01 11:06:29 +08:00
|
|
|
}
|
2022-02-05 13:04:06 +08:00
|
|
|
|
2023-09-28 22:09:52 +08:00
|
|
|
dataDir, err := checkDataDir(profile.Data)
|
2022-02-05 13:04:06 +08:00
|
|
|
if err != nil {
|
2022-02-06 10:37:09 +08:00
|
|
|
fmt.Printf("Failed to check dsn: %s, err: %+v\n", dataDir, err)
|
2022-08-24 21:53:12 +08:00
|
|
|
return nil, err
|
2022-02-05 13:04:06 +08:00
|
|
|
}
|
|
|
|
|
2022-07-22 23:21:12 +08:00
|
|
|
profile.Data = dataDir
|
2023-09-28 22:09:52 +08:00
|
|
|
if profile.Driver == "sqlite" && profile.DSN == "" {
|
|
|
|
dbFile := fmt.Sprintf("memos_%s.db", profile.Mode)
|
|
|
|
profile.DSN = filepath.Join(dataDir, dbFile)
|
|
|
|
}
|
2022-09-12 17:25:34 +08:00
|
|
|
profile.Version = version.GetCurrentVersion(profile.Mode)
|
2022-02-05 13:04:06 +08:00
|
|
|
|
2022-08-24 21:53:12 +08:00
|
|
|
return &profile, nil
|
2022-02-05 13:04:06 +08:00
|
|
|
}
|