memos/server/profile/profile.go

91 lines
2.1 KiB
Go
Raw Normal View History

2022-05-22 09:29:34 +08:00
package profile
2022-02-05 13:04:06 +08:00
import (
"fmt"
2024-08-28 22:53:57 +08:00
"log/slog"
2022-02-05 13:04:06 +08:00
"os"
"path/filepath"
"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"
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 {
// Mode can be "prod" or "dev" or "demo"
2024-07-27 19:24:37 +08:00
Mode string
// Addr is the binding address for server
2024-07-27 19:24:37 +08:00
Addr string
2022-05-17 21:21:13 +08:00
// Port is the binding port for server
2024-07-27 19:24:37 +08:00
Port int
2022-07-09 12:57:08 +08:00
// Data is the data directory
2024-07-27 19:24:37 +08:00
Data string
2023-10-18 06:05:19 +08:00
// DSN points to where memos stores its own data
2024-07-27 19:24:37 +08:00
DSN string
// Driver is the database driver
2023-09-29 09:15:54 +08:00
// sqlite, mysql
2024-07-27 19:24:37 +08:00
Driver string
2022-05-17 21:21:13 +08:00
// Version is the current version of server
2024-07-27 19:24:37 +08:00
Version string
2024-08-13 22:16:43 +08:00
// InstanceURL is the url of your memos instance.
InstanceURL string
2022-02-05 13:04:06 +08:00
}
func (p *Profile) IsDev() bool {
return p.Mode != "prod"
}
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) {
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
}
// 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
}
2024-07-27 19:24:37 +08:00
func (p *Profile) Validate() error {
if p.Mode != "demo" && p.Mode != "dev" && p.Mode != "prod" {
p.Mode = "demo"
}
2022-02-18 22:21:10 +08:00
2024-07-27 19:24:37 +08:00
if p.Mode == "prod" && p.Data == "" {
if runtime.GOOS == "windows" {
2024-07-27 19:24:37 +08:00
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 {
2024-08-28 22:53:57 +08:00
slog.Error("failed to create data directory", slog.String("data", p.Data), slog.String("error", err.Error()))
2024-07-27 19:24:37 +08:00
return err
}
}
} else {
2024-07-27 19:24:37 +08:00
p.Data = "/var/opt/memos"
}
2022-05-01 11:06:29 +08:00
}
2022-02-05 13:04:06 +08:00
2024-07-27 19:24:37 +08:00
dataDir, err := checkDataDir(p.Data)
2022-02-05 13:04:06 +08:00
if err != nil {
2024-08-28 22:53:57 +08:00
slog.Error("failed to check dsn", slog.String("data", dataDir), slog.String("error", err.Error()))
2024-07-27 19:24:37 +08:00
return err
2022-02-05 13:04:06 +08:00
}
2024-07-27 19:24:37 +08:00
p.Data = dataDir
if p.Driver == "sqlite" && p.DSN == "" {
dbFile := fmt.Sprintf("memos_%s.db", p.Mode)
p.DSN = filepath.Join(dataDir, dbFile)
}
2022-02-05 13:04:06 +08:00
2024-07-27 19:24:37 +08:00
return nil
2022-02-05 13:04:06 +08:00
}