memos/server/profile/profile.go
Athurg Gooth c72f221fc0
feat: support mysql as backend storage driver (#2300)
* Rename checkDSN to checkDataDir

* Add option to set DSN and db driver

* Add mysql driver skeleton

* Add mysql container in compose for debug

* Add basic function for mysql driver

* Cleanup go mod with tidy

* Cleanup go.sum with tidy

* Add DeleteUser support for mysql driver

* Fix UpdateUser of mysql driver

* Add DeleteTag support for mysql driver

* Add DeleteResource support for mysql driver

* Add UpdateMemo and DeleteMemo support for mysql driver

* Add MemoRelation support for mysql driver

* Add MemoOrganizer support for mysql driver

* Add Idp support for mysql driver

* Add Storage support for mysql driver

* Add FindMemosVisibilityList support for mysql driver

* Add Vacuum support for mysql driver

* Add Migration support for mysql driver

* Add Migration support for mysql driver

* Fix ListMemo failed with referece

* Change Activity.CreateTs type in MySQL

* Change User.CreateTs type in MySQL

* Fix by golangci-lint

* Change Resource.CreateTs type in MySQL

* Change MigrationHistory.CreateTs type in MySQL

* Change Memo.CreateTs type in MySQL
2023-09-28 09:09:52 -05:00

101 lines
2.4 KiB
Go

package profile
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/usememos/memos/server/version"
)
// Profile is the configuration to start main server.
type Profile struct {
// Mode can be "prod" or "dev" or "demo"
Mode string `json:"mode"`
// Addr is the binding address for server
Addr string `json:"-"`
// Port is the binding port for server
Port int `json:"-"`
// Data is the data directory
Data string `json:"-"`
// DSN points to where Memos stores its own data
DSN string `json:"dsn"`
// Driver is the database driver
Driver string `json:"driver"`
// Version is the current version of server
Version string `json:"version"`
}
func (p *Profile) IsDev() bool {
return p.Mode != "prod"
}
func checkDataDir(dataDir string) (string, error) {
// 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)
if err != nil {
return "", err
}
dataDir = absDir
}
// Trim trailing \ or / in case user supplies
dataDir = strings.TrimRight(dataDir, "\\/")
if _, err := os.Stat(dataDir); err != nil {
return "", errors.Wrapf(err, "unable to access data folder %s", dataDir)
}
return dataDir, nil
}
// GetProfile will return a profile for dev or prod.
func GetProfile() (*Profile, error) {
profile := Profile{}
err := viper.Unmarshal(&profile)
if err != nil {
return nil, err
}
if profile.Mode != "demo" && profile.Mode != "dev" && profile.Mode != "prod" {
profile.Mode = "demo"
}
if profile.Mode == "prod" && profile.Data == "" {
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"
}
}
dataDir, err := checkDataDir(profile.Data)
if err != nil {
fmt.Printf("Failed to check dsn: %s, err: %+v\n", dataDir, err)
return nil, err
}
profile.Data = dataDir
if profile.Driver == "sqlite" && profile.DSN == "" {
dbFile := fmt.Sprintf("memos_%s.db", profile.Mode)
profile.DSN = filepath.Join(dataDir, dbFile)
}
profile.Version = version.GetCurrentVersion(profile.Mode)
return &profile, nil
}