diff --git a/README.md b/README.md index 6f7c749d..ccdfd7fa 100644 --- a/README.md +++ b/README.md @@ -28,15 +28,11 @@ Making sure that you are in charge of your data and more customizations. - 📑 Experience excellent interaction logic; - ⚡️ Quick privatization deployment; -## ⚓️ Deploy Guide with Docker +## ⚓️ Deploy with Docker -1. download the [initialized db file](https://github.com/justmemos/memos/raw/main/resources/memos_release.db): - -2. pull and run docker image: - - ```docker - docker run --name memos --publish 8080:8080 --volume ~/path/to/your/data/:/var/opt/memos -e mode=release -e data=/var/opt/memos neosmemo/memos:dev - ``` +```docker +docker run --name memos --publish 8080:8080 --volume ~/path/to/your/data/:/var/opt/memos -e mode=release neosmemo/memos:dev +``` The default user account is `guest` with password `secret`. diff --git a/common/profile.go b/common/profile.go index c0a5c4f1..09a4d86e 100644 --- a/common/profile.go +++ b/common/profile.go @@ -14,7 +14,7 @@ type Profile struct { // Port is the binding port for server. Port int `json:"port"` // DSN points to where Memos stores its own data - DSN string `json:"-"` + DSN string `json:"dsn"` } func checkDSN(dataDir string) (string, error) { @@ -50,7 +50,10 @@ func GetProfile() Profile { port = 8080 } - data := os.Getenv("data") + data := "" + if mode == "release" { + data = "/var/opt/memos" + } dataDir, err := checkDSN(data) if err != nil { @@ -58,7 +61,7 @@ func GetProfile() Profile { os.Exit(1) } - dsn := fmt.Sprintf("file:%s/memos_%s.db", dataDir, mode) + dsn := fmt.Sprintf("%s/memos_%s.db", dataDir, mode) return Profile{ Mode: mode, diff --git a/docs/deploy/docker-deploy-guide.md b/docs/deploy/docker-deploy-guide.md deleted file mode 100644 index 9e1882b7..00000000 --- a/docs/deploy/docker-deploy-guide.md +++ /dev/null @@ -1,17 +0,0 @@ -# Deploy `memos` with Docker - -1. download the initialized db file: - - ```shell - mkdir ~/data - cd ~/data - wget --no-check-certificate https://github.com/justmemos/memos/blob/main/resources/memos-release.db?raw=true - ``` - -2. pull and run docker image: - - ```docker - docker run --name memos --publish 8080:8080 --volume ~/path/to/your/data/:/var/opt/memos -e mode=release -e data=/var/opt/memos neosmemo/memos:dev - ``` - -The default user account is `guest` with password `secret`. diff --git a/server/basic_auth.go b/server/basic_auth.go index 9bae4da8..0c5342c8 100644 --- a/server/basic_auth.go +++ b/server/basic_auth.go @@ -56,7 +56,7 @@ func removeUserSession(c echo.Context) error { func BasicAuthMiddleware(us api.UserService, next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { // Skips auth - if common.HasPrefixes(c.Path(), "/api/auth") { + if common.HasPrefixes(c.Path(), "/api/auth", "/api/ping") { return next(c) } diff --git a/store/migration/10000__reset.sql b/store/migration/10000__reset.sql new file mode 100644 index 00000000..d5eb25b2 --- /dev/null +++ b/store/migration/10000__reset.sql @@ -0,0 +1,4 @@ +DROP TABLE IF EXISTS `memo`; +DROP TABLE IF EXISTS `shortcut`; +DROP TABLE IF EXISTS `resource`; +DROP TABLE IF EXISTS `user`; diff --git a/store/migration/10001__schema.sql b/store/migration/10001__schema.sql index a9b182ea..28f04bae 100644 --- a/store/migration/10001__schema.sql +++ b/store/migration/10001__schema.sql @@ -1,8 +1,3 @@ -DROP TABLE IF EXISTS `memo`; -DROP TABLE IF EXISTS `shortcut`; -DROP TABLE IF EXISTS `resource`; -DROP TABLE IF EXISTS `user`; - -- user CREATE TABLE user ( id INTEGER PRIMARY KEY AUTOINCREMENT, diff --git a/store/sqlite.go b/store/sqlite.go index 70a4db43..6e8e37dd 100644 --- a/store/sqlite.go +++ b/store/sqlite.go @@ -7,6 +7,7 @@ import ( "fmt" "io/fs" "memos/common" + "os" "sort" _ "github.com/mattn/go-sqlite3" @@ -42,18 +43,31 @@ func (db *DB) Open() (err error) { } // Connect to the database. - if db.Db, err = sql.Open("sqlite3", db.DSN); err != nil { + sqlDB, err := sql.Open("sqlite3", db.DSN) + if err != nil { return fmt.Errorf("failed to open db with dsn: %s, err: %w", db.DSN, err) } - if db.mode == "dev" { - // If mode is dev, then we will migrate and seed the database. + db.Db = sqlDB + + // If db file not exists, we should migrate and seed the database. + if _, err := os.Stat(db.DSN); errors.Is(err, os.ErrNotExist) { if err := db.migrate(); err != nil { return fmt.Errorf("failed to migrate: %w", err) } if err := db.seed(); err != nil { return fmt.Errorf("failed to seed: %w", err) } + } else { + // If db file exists and mode is dev, we should migrate and seed the database. + if db.mode == "dev" { + if err := db.migrate(); err != nil { + return fmt.Errorf("failed to migrate: %w", err) + } + if err := db.seed(); err != nil { + return fmt.Errorf("failed to seed: %w", err) + } + } } return err