Skip to content

Commit

Permalink
chore: add service version
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed May 17, 2022
1 parent 5d0e972 commit 74ab420
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 55 deletions.
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.1
5 changes: 2 additions & 3 deletions api/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Memo struct {
type MemoCreate struct {
// Standard fields
CreatorID int
// Used to import memos with clearly created ts.
// Used to import memos with a clearly created ts.
CreatedTs *int64 `json:"createdTs"`

// Domain specific fields
Expand All @@ -27,7 +27,6 @@ type MemoPatch struct {
ID int

// Standard fields
CreatedTs *int64 `json:"createdTs"`
RowStatus *string `json:"rowStatus"`

// Domain specific fields
Expand All @@ -43,5 +42,5 @@ type MemoFind struct {
}

type MemoDelete struct {
ID *int `json:"id"`
ID int `json:"id"`
}
5 changes: 4 additions & 1 deletion api/system.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package api

import "memos/common"

type SystemStatus struct {
Owner *User `json:"owner"`
Owner *User `json:"owner"`
Profile *common.Profile `json:"profile"`
}
10 changes: 1 addition & 9 deletions bin/server/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import (

type Main struct {
profile *common.Profile

server *server.Server

db *store.DB
}

func (m *Main) Run() error {
Expand All @@ -23,15 +19,11 @@ func (m *Main) Run() error {
return fmt.Errorf("cannot open db: %w", err)
}

m.db = db

s := server.NewServer(m.profile)

storeInstance := store.New(db)
s.Store = storeInstance

m.server = s

if err := s.Run(); err != nil {
return err
}
Expand All @@ -42,7 +34,7 @@ func (m *Main) Run() error {
func Execute() {
profile := common.GetProfile()
m := Main{
profile: &profile,
profile: profile,
}

err := m.Run()
Expand Down
31 changes: 25 additions & 6 deletions common/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
type Profile struct {
// Mode can be "prod" or "dev"
Mode string `json:"mode"`
// Port is the binding port for server.
// Port is the binding port for server
Port int `json:"port"`
// DSN points to where Memos stores its own data
DSN string `json:"dsn"`
// Version is the current version of server
Version string `json:"version"`
}

func checkDSN(dataDir string) (string, error) {
Expand All @@ -38,8 +40,22 @@ func checkDSN(dataDir string) (string, error) {
return dataDir, nil
}

func getSystemVersion() string {
absPath, err := filepath.Abs("./VERSION")
if err != nil {
return "0.0.0"
}

data, err := os.ReadFile(absPath)
if err != nil {
return "0.0.0"
}

return string(data)
}

// GetDevProfile will return a profile for dev.
func GetProfile() Profile {
func GetProfile() *Profile {
mode := os.Getenv("mode")
if mode != "dev" && mode != "prod" {
mode = "dev"
Expand All @@ -63,9 +79,12 @@ func GetProfile() Profile {

dsn := fmt.Sprintf("%s/memos_%s.db", dataDir, mode)

return Profile{
Mode: mode,
Port: port,
DSN: dsn,
version := getSystemVersion()

return &Profile{
Mode: mode,
Port: port,
DSN: dsn,
Version: version,
}
}
2 changes: 1 addition & 1 deletion server/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
}

memoDelete := &api.MemoDelete{
ID: &memoID,
ID: memoID,
}

err = s.Store.DeleteMemo(memoDelete)
Expand Down
3 changes: 2 additions & 1 deletion server/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func (s *Server) registerSystemRoutes(g *echo.Group) {
}

systemStatus := api.SystemStatus{
Owner: ownerUser,
Owner: ownerUser,
Profile: s.Profile,
}

c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
Expand Down
3 changes: 0 additions & 3 deletions store/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ func patchMemo(db *DB, patch *api.MemoPatch) (*api.Memo, error) {
if v := patch.RowStatus; v != nil {
set, args = append(set, "row_status = ?"), append(args, *v)
}
if v := patch.CreatedTs; v != nil {
set, args = append(set, "created_ts = ?"), append(args, *v)
}

args = append(args, patch.ID)

Expand Down
17 changes: 8 additions & 9 deletions store/migration/10001__schema.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
-- user
CREATE TABLE user (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
email TEXT NOT NULL UNIQUE,
role TEXT NOT NULL CHECK (role IN ('OWNER', 'USER')) DEFAULT 'USER',
name TEXT NOT NULL,
password_hash TEXT NOT NULL,
open_id TEXT NOT NULL UNIQUE,
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now'))
open_id TEXT NOT NULL UNIQUE
);

INSERT INTO
Expand All @@ -30,12 +30,12 @@ END;
-- memo
CREATE TABLE memo (
id INTEGER PRIMARY KEY AUTOINCREMENT,
creator_id INTEGER NOT NULL,
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
-- allowed row status are 'NORMAL', 'ARCHIVED', 'HIDDEN'.
row_status TEXT NOT NULL DEFAULT 'NORMAL',
content TEXT NOT NULL DEFAULT '',
creator_id INTEGER NOT NULL,
FOREIGN KEY(creator_id) REFERENCES users(id)
);

Expand All @@ -59,12 +59,11 @@ END;
-- shortcut
CREATE TABLE shortcut (
id INTEGER PRIMARY KEY AUTOINCREMENT,
creator_id INTEGER NOT NULL,
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),

title TEXT NOT NULL DEFAULT '',
payload TEXT NOT NULL DEFAULT '{}',
creator_id INTEGER NOT NULL,
-- allowed row status are 'NORMAL', 'ARCHIVED'.
row_status TEXT NOT NULL DEFAULT 'NORMAL',
FOREIGN KEY(creator_id) REFERENCES users(id)
Expand All @@ -90,13 +89,13 @@ END;
-- resource
CREATE TABLE resource (
id INTEGER PRIMARY KEY AUTOINCREMENT,
creator_id INTEGER NOT NULL,
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
filename TEXT NOT NULL DEFAULT '',
blob BLOB NOT NULL,
type TEXT NOT NULL DEFAULT '',
size INTEGER NOT NULL DEFAULT 0,
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
updated_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now')),
creator_id INTEGER NOT NULL,
FOREIGN KEY(creator_id) REFERENCES users(id)
);

Expand Down
28 changes: 16 additions & 12 deletions web/src/components/AboutSiteDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { useEffect, useState } from "react";
import utils from "../helpers/utils";
import api from "../helpers/api";
import Only from "./common/OnlyWhen";
import { showDialog } from "./Dialog";
import "../less/about-site-dialog.less";

interface Props extends DialogProps {}

const AboutSiteDialog: React.FC<Props> = ({ destroy }: Props) => {
const [lastUpdatedAt, setLastUpdatedAt] = useState("");
const [profile, setProfile] = useState<Profile>();

useEffect(() => {
try {
fetch("https://api.github.com/repos/justmemos/memos/commits/main").then(async (res) => {
const data = (await res.json()) as any;
setLastUpdatedAt(utils.getDateTimeString(new Date(data.commit.committer.date)));
api.getSystemStatus().then(({ profile }) => {
setProfile(profile);
});
} catch (error) {
setLastUpdatedAt("2017/12/31");
setProfile({
mode: "dev",
version: "0.0.0",
});
}
}, []);

Expand All @@ -35,16 +38,17 @@ const AboutSiteDialog: React.FC<Props> = ({ destroy }: Props) => {
</div>
<div className="dialog-content-container">
<p>
Memos is an open source, quickly self-hosted alternative <a href="https://flomoapp.com">flomo</a>.
Memos is an open source, quickly self-hosted alternative to <a href="https://flomoapp.com">flomo</a>.
</p>
<br />
<p>
🏗 <a href="https://github.com/justmemos/memos">This project</a> is working in progress, and very pleasure to your{" "}
<a href="https://github.com/justmemos/memos/issues">PRs</a>.
</p>
<p className="updated-time-text">
Last updated on <span className="pre-text">{lastUpdatedAt}</span> 🎉
<a href="https://github.com/justmemos/memos">🏗 Source code</a>, and built by <a href="https://github.com/boojack">Steven 🐯</a>.
</p>
<Only when={profile !== undefined}>
<p className="updated-time-text">
version: <span className="pre-text">{profile?.version}</span> 🎉
</p>
</Only>
</div>
</>
);
Expand Down
11 changes: 1 addition & 10 deletions web/src/less/about-site-dialog.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

.about-site-dialog {
> .dialog-container {
@apply w-128;
@apply w-96;

> .dialog-content-container {
@apply leading-relaxed;
Expand All @@ -20,15 +20,6 @@
@apply font-mono;
}

> .btn {
@apply px-2 py-1 text-white rounded text-sm;
background-color: @text-green;

&:hover {
@apply opacity-80;
}
}

a {
@apply cursor-pointer underline-offset-2 underline text-blue-600;
}
Expand Down
1 change: 1 addition & 0 deletions web/src/types/api.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
declare namespace API {
interface SystemStatus {
owner: Model.User;
profile: Profile;
}

interface UserCreate {
Expand Down
4 changes: 4 additions & 0 deletions web/src/types/module/system.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface Profile {
mode: string;
version: string;
}

0 comments on commit 74ab420

Please sign in to comment.