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:
Yoshino-s 2023-02-19 13:36:45 +08:00 committed by GitHub
parent d0b8b076cf
commit afaaec8492
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 27 additions and 23 deletions

View File

@ -1,4 +1,5 @@
{ {
"go.lintOnSave": "workspace", "go.lintOnSave": "workspace",
"go.lintTool": "golangci-lint" "go.lintTool": "golangci-lint",
"go.inferGopath": false
} }

View File

@ -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:

View File

@ -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 == "" {

View File

@ -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

View File

@ -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
} }

View File

@ -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

View File

@ -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(() => {

View File

@ -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;

View File

@ -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,