mirror of
https://github.com/usememos/memos.git
synced 2025-03-04 01:04:38 +08:00
feat: flag variables
This commit is contained in:
parent
475f258f5b
commit
f982391a83
5 changed files with 71 additions and 65 deletions
|
@ -26,6 +26,9 @@ WORKDIR /usr/local/memos
|
||||||
COPY --from=backend /backend-build/memos /usr/local/memos/
|
COPY --from=backend /backend-build/memos /usr/local/memos/
|
||||||
COPY --from=frontend /frontend-build/dist /usr/local/memos/web/dist
|
COPY --from=frontend /frontend-build/dist /usr/local/memos/web/dist
|
||||||
|
|
||||||
CMD ["./memos"]
|
# Directory to store the data, which can be referenced as the mounting point.
|
||||||
|
RUN mkdir -p /var/opt/memos
|
||||||
|
|
||||||
EXPOSE 8080
|
CMD ["-mode", "release", "-port", "8080", "-data", "/var/opt/memos"]
|
||||||
|
|
||||||
|
ENTRYPOINT ["./memos"]
|
||||||
|
|
62
bin/server/cmd/profile.go
Normal file
62
bin/server/cmd/profile.go
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Profile struct {
|
||||||
|
// mode can be "release" or "dev"
|
||||||
|
mode string
|
||||||
|
// port is the binding port for server.
|
||||||
|
port int
|
||||||
|
// dsn points to where Memos stores its own data
|
||||||
|
dsn string
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
error := fmt.Errorf("unable to access --data %s, %w", dataDir, err)
|
||||||
|
return "", error
|
||||||
|
}
|
||||||
|
|
||||||
|
return dataDir, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDevProfile will return a profile for dev.
|
||||||
|
func GetProfile() Profile {
|
||||||
|
mode := flag.String("mode", "dev", "")
|
||||||
|
port := flag.Int("port", 8080, "")
|
||||||
|
data := flag.String("data", "", "")
|
||||||
|
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
dataDir, err := checkDSN(*data)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%+v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
dsn := fmt.Sprintf("file:%s/memos_%s.db", dataDir, *mode)
|
||||||
|
|
||||||
|
return Profile{
|
||||||
|
mode: *mode,
|
||||||
|
port: *port,
|
||||||
|
dsn: dsn,
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,17 +0,0 @@
|
||||||
//go:build !release
|
|
||||||
// +build !release
|
|
||||||
|
|
||||||
package cmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
// GetDevProfile will return a profile for dev.
|
|
||||||
func GetDevProfile(dataDir string) Profile {
|
|
||||||
return Profile{
|
|
||||||
mode: "8080",
|
|
||||||
port: 8080,
|
|
||||||
dsn: fmt.Sprintf("file:%s/memos_dev.db", dataDir),
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -3,47 +3,11 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"memos/server"
|
"memos/server"
|
||||||
"memos/store"
|
"memos/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
dataDir string
|
|
||||||
)
|
|
||||||
|
|
||||||
type Profile struct {
|
|
||||||
// mode can be "release" or "dev"
|
|
||||||
mode string
|
|
||||||
// port is the binding port for server.
|
|
||||||
port int
|
|
||||||
// dsn points to where Memos stores its own data
|
|
||||||
dsn string
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkDataDir() 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 {
|
|
||||||
error := fmt.Errorf("unable to access --data %s, %w", dataDir, err)
|
|
||||||
return error
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type Main struct {
|
type Main struct {
|
||||||
profile *Profile
|
profile *Profile
|
||||||
|
|
||||||
|
@ -53,17 +17,11 @@ type Main struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Execute() {
|
func Execute() {
|
||||||
err := checkDataDir()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("%+v\n", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
m := Main{}
|
m := Main{}
|
||||||
profile := GetDevProfile(dataDir)
|
profile := GetProfile()
|
||||||
m.profile = &profile
|
m.profile = &profile
|
||||||
|
|
||||||
err = m.Run()
|
err := m.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("%+v\n", err)
|
fmt.Printf("%+v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -120,9 +120,9 @@ END;
|
||||||
INSERT INTO user
|
INSERT INTO user
|
||||||
(`id`, `name`, `password`, `open_id`)
|
(`id`, `name`, `password`, `open_id`)
|
||||||
VALUES
|
VALUES
|
||||||
(1, 'guest', '123456', 'guest_open_id'),
|
(1, 'guest', '123456', 'guest_open_id');
|
||||||
|
|
||||||
INSERT INTO memo
|
INSERT INTO memo
|
||||||
(`content`, `creator_id`)
|
(`content`, `creator_id`)
|
||||||
VALUES
|
VALUES
|
||||||
('👋 Welcome to memos', 1),
|
('👋 Welcome to memos', 1);
|
||||||
|
|
Loading…
Reference in a new issue