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