chore: use flags instead of env vars

This commit is contained in:
boojack
2022-07-09 12:57:08 +08:00
parent 1d8603df2b
commit 7c94db0ca0
4 changed files with 36 additions and 21 deletions

1
.gitignore vendored
View File

@@ -24,5 +24,6 @@ data
# build folder # build folder
build build
memos-build
.DS_Store .DS_Store

View File

@@ -49,6 +49,13 @@ func Execute() {
profile: profile, profile: profile,
} }
println("---")
println("profile")
println("mode:", profile.Mode)
println("port:", profile.Port)
println("dsn:", profile.DSN)
println("version:", profile.Version)
println("---")
println(greetingBanner) println(greetingBanner)
fmt.Printf("Version %s has started at :%d\n", profile.Version, profile.Port) fmt.Printf("Version %s has started at :%d\n", profile.Version, profile.Port)

10
scripts/build.sh Normal file
View File

@@ -0,0 +1,10 @@
# Usage: sh ./scripts/build.sh
set -e
cd "$(dirname "$0")/../"
echo "Start building..."
go build -o ./memos-build/memos ./bin/server/main.go
echo "Build finished"

View File

@@ -1,10 +1,10 @@
package profile package profile
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"github.com/usememos/memos/common" "github.com/usememos/memos/common"
@@ -16,6 +16,8 @@ type Profile struct {
Mode string `json:"mode"` Mode string `json:"mode"`
// Port is the binding port for server // Port is the binding port for server
Port int `json:"port"` Port int `json:"port"`
// Data is the data directory
Data string `json:"data"`
// DSN points to where Memos stores its own data // DSN points to where Memos stores its own data
DSN string `json:"dsn"` DSN string `json:"dsn"`
// Version is the current version of server // Version is the current version of server
@@ -43,35 +45,30 @@ func checkDSN(dataDir string) (string, error) {
return dataDir, nil return dataDir, nil
} }
// GetDevProfile will return a profile for dev. // GetDevProfile will return a profile for dev or prod.
func GetProfile() *Profile { func GetProfile() *Profile {
mode := os.Getenv("mode") profile := Profile{}
if mode != "dev" && mode != "prod" { flag.StringVar(&profile.Mode, "mode", "dev", "mode of server")
mode = "dev" flag.IntVar(&profile.Port, "port", 8080, "port of server")
flag.StringVar(&profile.Data, "data", "", "data directory")
flag.Parse()
if profile.Mode != "dev" && profile.Mode != "prod" {
profile.Mode = "dev"
} }
port, err := strconv.Atoi(os.Getenv("port")) if profile.Mode == "prod" && profile.Data == "" {
if err != nil { profile.Data = "/var/opt/memos"
port = 8080
} }
data := "" dataDir, err := checkDSN(profile.Data)
if mode == "prod" {
data = "/var/opt/memos"
}
dataDir, err := checkDSN(data)
if err != nil { if err != nil {
fmt.Printf("Failed to check dsn: %s, err: %+v\n", dataDir, err) fmt.Printf("Failed to check dsn: %s, err: %+v\n", dataDir, err)
os.Exit(1) os.Exit(1)
} }
dsn := fmt.Sprintf("%s/memos_%s.db", dataDir, mode) profile.DSN = fmt.Sprintf("%s/memos_%s.db", dataDir, profile.Mode)
profile.Version = common.GetCurrentVersion(profile.Mode)
return &Profile{ return &profile
Mode: mode,
Port: port,
DSN: dsn,
Version: common.GetCurrentVersion(mode),
}
} }