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.
69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
3 years ago
|
package common
|
||
4 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
3 years ago
|
"strconv"
|
||
4 years ago
|
"strings"
|
||
|
)
|
||
|
|
||
|
type Profile struct {
|
||
3 years ago
|
// Mode can be "release" or "dev"
|
||
|
Mode string `json:"mode"`
|
||
|
// Port is the binding port for server.
|
||
|
Port int `json:"port"`
|
||
|
// DSN points to where Memos stores its own data
|
||
|
DSN string `json:"-"`
|
||
4 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 {
|
||
4 years ago
|
error := fmt.Errorf("unable to access -data %s, err %w", dataDir, err)
|
||
4 years ago
|
return "", error
|
||
|
}
|
||
|
|
||
|
return dataDir, nil
|
||
|
}
|
||
|
|
||
|
// GetDevProfile will return a profile for dev.
|
||
|
func GetProfile() Profile {
|
||
3 years ago
|
mode := os.Getenv("mode")
|
||
|
if mode != "dev" && mode != "release" {
|
||
|
mode = "dev"
|
||
|
}
|
||
|
|
||
|
port, err := strconv.Atoi(os.Getenv("port"))
|
||
|
if err != nil {
|
||
|
port = 8080
|
||
|
}
|
||
|
|
||
|
data := os.Getenv("data")
|
||
4 years ago
|
|
||
3 years ago
|
dataDir, err := checkDSN(data)
|
||
4 years ago
|
if err != nil {
|
||
4 years ago
|
fmt.Printf("Failed to check dsn: %s, err: %+v\n", dataDir, err)
|
||
4 years ago
|
os.Exit(1)
|
||
|
}
|
||
|
|
||
3 years ago
|
dsn := fmt.Sprintf("file:%s/memos_%s.db", dataDir, mode)
|
||
4 years ago
|
|
||
|
return Profile{
|
||
3 years ago
|
Mode: mode,
|
||
|
Port: port,
|
||
|
DSN: dsn,
|
||
4 years ago
|
}
|
||
|
}
|