mirror of
https://github.com/usememos/memos.git
synced 2024-12-26 23:22:47 +08:00
chore: update server profile
This commit is contained in:
parent
0b3c77c79c
commit
c3c2882dc5
8 changed files with 56 additions and 58 deletions
|
@ -1,8 +1,8 @@
|
|||
package api
|
||||
|
||||
import "memos/common"
|
||||
import "memos/server/profile"
|
||||
|
||||
type SystemStatus struct {
|
||||
Owner *User `json:"owner"`
|
||||
Profile *common.Profile `json:"profile"`
|
||||
Owner *User `json:"owner"`
|
||||
Profile *profile.Profile `json:"profile"`
|
||||
}
|
||||
|
|
|
@ -4,8 +4,8 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
|
||||
"memos/common"
|
||||
"memos/server"
|
||||
"memos/server/profile"
|
||||
"memos/store"
|
||||
DB "memos/store/db"
|
||||
)
|
||||
|
@ -22,7 +22,7 @@ const (
|
|||
)
|
||||
|
||||
type Main struct {
|
||||
profile *common.Profile
|
||||
profile *profile.Profile
|
||||
}
|
||||
|
||||
func (m *Main) Run() error {
|
||||
|
@ -44,7 +44,7 @@ func (m *Main) Run() error {
|
|||
}
|
||||
|
||||
func Execute() {
|
||||
profile := common.GetProfile()
|
||||
profile := profile.GetProfile()
|
||||
m := Main{
|
||||
profile: profile,
|
||||
}
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
package common
|
||||
package profile
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"memos/common"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Profile is the configuration to start main server.
|
||||
type Profile struct {
|
||||
// Mode can be "prod" or "dev"
|
||||
Mode string `json:"mode"`
|
||||
|
@ -69,6 +71,6 @@ func GetProfile() *Profile {
|
|||
Mode: mode,
|
||||
Port: port,
|
||||
DSN: dsn,
|
||||
Version: Version,
|
||||
Version: common.Version,
|
||||
}
|
||||
}
|
|
@ -2,7 +2,7 @@ package server
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"memos/common"
|
||||
"memos/server/profile"
|
||||
"memos/store"
|
||||
"time"
|
||||
|
||||
|
@ -16,12 +16,12 @@ import (
|
|||
type Server struct {
|
||||
e *echo.Echo
|
||||
|
||||
Profile *common.Profile
|
||||
Profile *profile.Profile
|
||||
|
||||
Store *store.Store
|
||||
}
|
||||
|
||||
func NewServer(profile *common.Profile) *Server {
|
||||
func NewServer(profile *profile.Profile) *Server {
|
||||
e := echo.New()
|
||||
e.Debug = true
|
||||
e.HideBanner = true
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"fmt"
|
||||
"io/fs"
|
||||
"memos/common"
|
||||
"memos/server/profile"
|
||||
"os"
|
||||
"sort"
|
||||
|
||||
|
@ -20,6 +21,7 @@ var migrationFS embed.FS
|
|||
var seedFS embed.FS
|
||||
|
||||
type DB struct {
|
||||
// sqlite db connection instance
|
||||
Db *sql.DB
|
||||
// datasource name
|
||||
DSN string
|
||||
|
@ -28,7 +30,7 @@ type DB struct {
|
|||
}
|
||||
|
||||
// NewDB returns a new instance of DB associated with the given datasource name.
|
||||
func NewDB(profile *common.Profile) *DB {
|
||||
func NewDB(profile *profile.Profile) *DB {
|
||||
db := &DB{
|
||||
DSN: profile.DSN,
|
||||
mode: profile.Mode,
|
||||
|
@ -76,31 +78,9 @@ func (db *DB) Open() (err error) {
|
|||
}
|
||||
|
||||
func (db *DB) migrate() error {
|
||||
table, err := findTable(db, "migration_history")
|
||||
err := db.compareMigrationHistory()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if table == nil {
|
||||
createTable(db, `
|
||||
CREATE TABLE migration_history (
|
||||
version TEXT NOT NULL PRIMARY KEY,
|
||||
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now'))
|
||||
);
|
||||
`)
|
||||
}
|
||||
|
||||
migrationHistoryList, err := findMigrationHistoyList(db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(migrationHistoryList) == 0 {
|
||||
createMigrationHistoy(db, common.Version)
|
||||
} else {
|
||||
migrationHistory := migrationHistoryList[0]
|
||||
if migrationHistory.Version != common.Version {
|
||||
createMigrationHistoy(db, common.Version)
|
||||
}
|
||||
return fmt.Errorf("failed to compare migration history, err=%w", err)
|
||||
}
|
||||
|
||||
filenames, err := fs.Glob(migrationFS, fmt.Sprintf("%s/*.sql", "migration"))
|
||||
|
@ -154,15 +134,34 @@ func (db *DB) executeFile(FS embed.FS, name string) error {
|
|||
return tx.Commit()
|
||||
}
|
||||
|
||||
func FormatError(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch err {
|
||||
case sql.ErrNoRows:
|
||||
return errors.New("data not found")
|
||||
default:
|
||||
// compareMigrationHistory compares migration history data
|
||||
func (db *DB) compareMigrationHistory() error {
|
||||
table, err := findTable(db, "migration_history")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if table == nil {
|
||||
createTable(db, `
|
||||
CREATE TABLE migration_history (
|
||||
version TEXT NOT NULL PRIMARY KEY,
|
||||
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now'))
|
||||
);
|
||||
`)
|
||||
}
|
||||
|
||||
migrationHistoryList, err := findMigrationHistoryList(db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(migrationHistoryList) == 0 {
|
||||
createMigrationHistory(db, common.Version)
|
||||
} else {
|
||||
migrationHistory := migrationHistoryList[0]
|
||||
if migrationHistory.Version != common.Version {
|
||||
createMigrationHistory(db, common.Version)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package db
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MigrationHistory struct {
|
||||
|
@ -10,7 +9,7 @@ type MigrationHistory struct {
|
|||
Version string
|
||||
}
|
||||
|
||||
func findMigrationHistoyList(db *DB) ([]*MigrationHistory, error) {
|
||||
func findMigrationHistoryList(db *DB) ([]*MigrationHistory, error) {
|
||||
rows, err := db.Db.Query(`
|
||||
SELECT
|
||||
version,
|
||||
|
@ -40,16 +39,14 @@ func findMigrationHistoyList(db *DB) ([]*MigrationHistory, error) {
|
|||
return migrationHistoryList, nil
|
||||
}
|
||||
|
||||
func createMigrationHistoy(db *DB, version string) error {
|
||||
func createMigrationHistory(db *DB, version string) error {
|
||||
result, err := db.Db.Exec(`
|
||||
INSERT INTO migration_history (
|
||||
version,
|
||||
created_ts
|
||||
version
|
||||
)
|
||||
VALUES (?, ?)
|
||||
VALUES (?)
|
||||
`,
|
||||
version,
|
||||
time.Now().Unix(),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -25,7 +25,7 @@ func findTable(db *DB, tableName string) (*Table, error) {
|
|||
args...,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, FormatError(err)
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
|
@ -36,14 +36,14 @@ func findTable(db *DB, tableName string) (*Table, error) {
|
|||
&table.Name,
|
||||
&table.SQL,
|
||||
); err != nil {
|
||||
return nil, FormatError(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tableList = append(tableList, &table)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, FormatError(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(tableList) == 0 {
|
||||
|
|
|
@ -2,17 +2,17 @@ package store
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"memos/common"
|
||||
"memos/server/profile"
|
||||
)
|
||||
|
||||
// Store provides database access to all raw objects
|
||||
type Store struct {
|
||||
db *sql.DB
|
||||
profile *common.Profile
|
||||
profile *profile.Profile
|
||||
}
|
||||
|
||||
// New creates a new instance of Store
|
||||
func New(db *sql.DB, profile *common.Profile) *Store {
|
||||
func New(db *sql.DB, profile *profile.Profile) *Store {
|
||||
return &Store{
|
||||
db: db,
|
||||
profile: profile,
|
||||
|
|
Loading…
Reference in a new issue