mirror of
https://github.com/usememos/memos.git
synced 2025-04-25 06:38:49 +02:00
feat(mode): add demo mode (#1121)
* feat(mode): add demo mode * chroe: Update store/db/db.go Co-authored-by: boojack <stevenlgtm@gmail.com> * chroe: Update store/db/db.go Co-authored-by: boojack <stevenlgtm@gmail.com> --------- Co-authored-by: boojack <stevenlgtm@gmail.com>
This commit is contained in:
parent
d0b8b076cf
commit
afaaec8492
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
"go.lintOnSave": "workspace",
|
"go.lintOnSave": "workspace",
|
||||||
"go.lintTool": "golangci-lint"
|
"go.lintTool": "golangci-lint",
|
||||||
|
"go.inferGopath": false
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ services:
|
|||||||
image: "${MEMOS_IMAGE}"
|
image: "${MEMOS_IMAGE}"
|
||||||
volumes:
|
volumes:
|
||||||
- memos_volume:/var/opt/memos
|
- memos_volume:/var/opt/memos
|
||||||
command: ["--mode", "dev"]
|
command: ["--mode", "demo"]
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
memos_volume:
|
memos_volume:
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
// Profile is the configuration to start main server.
|
// Profile is the configuration to start main server.
|
||||||
type Profile struct {
|
type Profile struct {
|
||||||
// Mode can be "prod" or "dev"
|
// Mode can be "prod" or "dev" or "demo"
|
||||||
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 int `json:"-"`
|
||||||
@ -47,13 +47,13 @@ func checkDSN(dataDir string) (string, error) {
|
|||||||
// GetDevProfile will return a profile for dev or prod.
|
// GetDevProfile will return a profile for dev or prod.
|
||||||
func GetProfile() (*Profile, error) {
|
func GetProfile() (*Profile, error) {
|
||||||
profile := Profile{}
|
profile := Profile{}
|
||||||
flag.StringVar(&profile.Mode, "mode", "dev", "mode of server")
|
flag.StringVar(&profile.Mode, "mode", "demo", "mode of server")
|
||||||
flag.IntVar(&profile.Port, "port", 8081, "port of server")
|
flag.IntVar(&profile.Port, "port", 8081, "port of server")
|
||||||
flag.StringVar(&profile.Data, "data", "", "data directory")
|
flag.StringVar(&profile.Data, "data", "", "data directory")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if profile.Mode != "dev" && profile.Mode != "prod" {
|
if profile.Mode != "dev" && profile.Mode != "prod" && profile.Mode != "demo" {
|
||||||
profile.Mode = "dev"
|
profile.Mode = "demo"
|
||||||
}
|
}
|
||||||
|
|
||||||
if profile.Mode == "prod" && profile.Data == "" {
|
if profile.Mode == "prod" && profile.Data == "" {
|
||||||
|
@ -15,7 +15,7 @@ var Version = "0.10.3"
|
|||||||
var DevVersion = "0.10.3"
|
var DevVersion = "0.10.3"
|
||||||
|
|
||||||
func GetCurrentVersion(mode string) string {
|
func GetCurrentVersion(mode string) string {
|
||||||
if mode == "dev" {
|
if mode == "dev" || mode == "demo" {
|
||||||
return DevVersion
|
return DevVersion
|
||||||
}
|
}
|
||||||
return Version
|
return Version
|
||||||
|
@ -38,7 +38,7 @@ func (raw *activityRaw) toActivity() *api.Activity {
|
|||||||
|
|
||||||
// CreateActivity creates an instance of Activity.
|
// CreateActivity creates an instance of Activity.
|
||||||
func (s *Store) CreateActivity(ctx context.Context, create *api.ActivityCreate) (*api.Activity, error) {
|
func (s *Store) CreateActivity(ctx context.Context, create *api.ActivityCreate) (*api.Activity, error) {
|
||||||
if s.profile.Mode != "dev" {
|
if s.profile.Mode == "prod" {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,17 +49,7 @@ func (db *DB) Open(ctx context.Context) (err error) {
|
|||||||
}
|
}
|
||||||
db.DBInstance = sqliteDB
|
db.DBInstance = sqliteDB
|
||||||
|
|
||||||
if db.profile.Mode == "dev" {
|
if db.profile.Mode == "prod" {
|
||||||
// In dev mode, we should migrate and seed the database.
|
|
||||||
if _, err := os.Stat(db.profile.DSN); errors.Is(err, os.ErrNotExist) {
|
|
||||||
if err := db.applyLatestSchema(ctx); err != nil {
|
|
||||||
return fmt.Errorf("failed to apply latest schema: %w", err)
|
|
||||||
}
|
|
||||||
if err := db.seed(ctx); err != nil {
|
|
||||||
return fmt.Errorf("failed to seed: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// If db file not exists, we should migrate the database.
|
// If db file not exists, we should migrate the database.
|
||||||
if _, err := os.Stat(db.profile.DSN); errors.Is(err, os.ErrNotExist) {
|
if _, err := os.Stat(db.profile.DSN); errors.Is(err, os.ErrNotExist) {
|
||||||
if err := db.applyLatestSchema(ctx); err != nil {
|
if err := db.applyLatestSchema(ctx); err != nil {
|
||||||
@ -120,6 +110,19 @@ func (db *DB) Open(ctx context.Context) (err error) {
|
|||||||
println(fmt.Sprintf("Failed to remove temp database file, err %v", err))
|
println(fmt.Sprintf("Failed to remove temp database file, err %v", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// In non-prod mode, we should migrate the database.
|
||||||
|
if _, err := os.Stat(db.profile.DSN); errors.Is(err, os.ErrNotExist) {
|
||||||
|
if err := db.applyLatestSchema(ctx); err != nil {
|
||||||
|
return fmt.Errorf("failed to apply latest schema: %w", err)
|
||||||
|
}
|
||||||
|
// In demo mode, we should seed the database.
|
||||||
|
if db.profile.Mode == "demo" {
|
||||||
|
if err := db.seed(ctx); err != nil {
|
||||||
|
return fmt.Errorf("failed to seed: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -26,8 +26,8 @@ const Auth = () => {
|
|||||||
const actionBtnLoadingState = useLoading(false);
|
const actionBtnLoadingState = useLoading(false);
|
||||||
const { appearance, locale, systemStatus } = globalStore.state;
|
const { appearance, locale, systemStatus } = globalStore.state;
|
||||||
const mode = systemStatus.profile.mode;
|
const mode = systemStatus.profile.mode;
|
||||||
const [username, setUsername] = useState(mode === "dev" ? "demohero" : "");
|
const [username, setUsername] = useState(mode === "demo" ? "demohero" : "");
|
||||||
const [password, setPassword] = useState(mode === "dev" ? "secret" : "");
|
const [password, setPassword] = useState(mode === "demo" ? "secret" : "");
|
||||||
const [identityProviderList, setIdentityProviderList] = useState<IdentityProvider[]>([]);
|
const [identityProviderList, setIdentityProviderList] = useState<IdentityProvider[]>([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -65,7 +65,7 @@ export const useGlobalStore = () => {
|
|||||||
return store.getState().global;
|
return store.getState().global;
|
||||||
},
|
},
|
||||||
isDev: () => {
|
isDev: () => {
|
||||||
return state.systemStatus.profile.mode === "dev";
|
return state.systemStatus.profile.mode !== "prod";
|
||||||
},
|
},
|
||||||
fetchSystemStatus: async () => {
|
fetchSystemStatus: async () => {
|
||||||
const { data: systemStatus } = (await api.getSystemStatus()).data;
|
const { data: systemStatus } = (await api.getSystemStatus()).data;
|
||||||
|
@ -14,7 +14,7 @@ const globalSlice = createSlice({
|
|||||||
systemStatus: {
|
systemStatus: {
|
||||||
host: undefined,
|
host: undefined,
|
||||||
profile: {
|
profile: {
|
||||||
mode: "dev",
|
mode: "demo",
|
||||||
version: "",
|
version: "",
|
||||||
},
|
},
|
||||||
dbSize: 0,
|
dbSize: 0,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user