mirror of https://github.com/usememos/memos
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.5 KiB
Go
77 lines
1.5 KiB
Go
3 years ago
|
package profile
|
||
3 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
3 years ago
|
"memos/common"
|
||
3 years ago
|
"os"
|
||
|
"path/filepath"
|
||
3 years ago
|
"strconv"
|
||
3 years ago
|
"strings"
|
||
|
)
|
||
|
|
||
3 years ago
|
// Profile is the configuration to start main server.
|
||
3 years ago
|
type Profile struct {
|
||
3 years ago
|
// Mode can be "prod" or "dev"
|
||
3 years ago
|
Mode string `json:"mode"`
|
||
3 years ago
|
// Port is the binding port for server
|
||
3 years ago
|
Port int `json:"port"`
|
||
|
// DSN points to where Memos stores its own data
|
||
3 years ago
|
DSN string `json:"dsn"`
|
||
3 years ago
|
// Version is the current version of server
|
||
|
Version string `json:"version"`
|
||
3 years ago
|
}
|
||
|
|
||
|
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 {
|
||
3 years ago
|
error := fmt.Errorf("unable to access -data %s, err %w", dataDir, err)
|
||
3 years ago
|
return "", error
|
||
|
}
|
||
|
|
||
|
return dataDir, nil
|
||
|
}
|
||
|
|
||
|
// GetDevProfile will return a profile for dev.
|
||
3 years ago
|
func GetProfile() *Profile {
|
||
3 years ago
|
mode := os.Getenv("mode")
|
||
3 years ago
|
if mode != "dev" && mode != "prod" {
|
||
3 years ago
|
mode = "dev"
|
||
|
}
|
||
|
|
||
|
port, err := strconv.Atoi(os.Getenv("port"))
|
||
|
if err != nil {
|
||
|
port = 8080
|
||
|
}
|
||
|
|
||
3 years ago
|
data := ""
|
||
3 years ago
|
if mode == "prod" {
|
||
3 years ago
|
data = "/var/opt/memos"
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
dataDir, err := checkDSN(data)
|
||
3 years ago
|
if err != nil {
|
||
3 years ago
|
fmt.Printf("Failed to check dsn: %s, err: %+v\n", dataDir, err)
|
||
3 years ago
|
os.Exit(1)
|
||
|
}
|
||
|
|
||
3 years ago
|
dsn := fmt.Sprintf("%s/memos_%s.db", dataDir, mode)
|
||
3 years ago
|
|
||
3 years ago
|
return &Profile{
|
||
|
Mode: mode,
|
||
|
Port: port,
|
||
|
DSN: dsn,
|
||
3 years ago
|
Version: common.Version,
|
||
3 years ago
|
}
|
||
|
}
|