Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
52743017
Commit
52743017
authored
Dec 14, 2023
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: implement memo route
parent
6cf7192d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
10 deletions
+58
-10
.gitignore
.gitignore
+1
-0
frontend.go
server/frontend/frontend.go
+53
-4
server.go
server/server.go
+4
-6
No files found.
.gitignore
View file @
52743017
...
...
@@ -6,6 +6,7 @@ tmp
# Frontend asset
web/dist
server/frontend/dist
# build folder
build
...
...
server/frontend/frontend.go
View file @
52743017
...
...
@@ -2,6 +2,7 @@ package frontend
import
(
"embed"
"fmt"
"io/fs"
"net/http"
"strings"
...
...
@@ -10,6 +11,8 @@ import (
"github.com/labstack/echo/v4/middleware"
"github.com/usememos/memos/internal/util"
"github.com/usememos/memos/server/profile"
"github.com/usememos/memos/store"
)
//go:embed dist
...
...
@@ -18,7 +21,19 @@ var embeddedFiles embed.FS
//go:embed dist/index.html
var
rawIndexHTML
string
func
Serve
(
e
*
echo
.
Echo
)
{
type
FrontendService
struct
{
Profile
*
profile
.
Profile
Store
*
store
.
Store
}
func
NewFrontendService
(
profile
*
profile
.
Profile
,
store
*
store
.
Store
)
*
FrontendService
{
return
&
FrontendService
{
Profile
:
profile
,
Store
:
store
,
}
}
func
(
s
*
FrontendService
)
Serve
(
e
*
echo
.
Echo
)
{
// Use echo static middleware to serve the built dist folder
// refer: https://github.com/labstack/echo/blob/master/middleware/static.go
e
.
Use
(
middleware
.
StaticWithConfig
(
middleware
.
StaticConfig
{
...
...
@@ -44,16 +59,50 @@ func Serve(e *echo.Echo) {
Filesystem
:
getFileSystem
(
"dist/assets"
),
}))
registerRoutes
(
e
)
s
.
registerRoutes
(
e
)
}
func
registerRoutes
(
e
*
echo
.
Echo
)
{
func
(
s
*
FrontendService
)
registerRoutes
(
e
*
echo
.
Echo
)
{
e
.
GET
(
"/m/:memoID"
,
func
(
c
echo
.
Context
)
error
{
indexHTML
:=
strings
.
ReplaceAll
(
rawIndexHTML
,
"<!-- memos.metadata -->"
,
"<meta name=
\"
memos-memo-id
\"
content=
\"
"
+
c
.
Param
(
"memoID"
)
+
"
\"
>"
+
"
\n
"
)
ctx
:=
c
.
Request
()
.
Context
()
memoID
,
err
:=
util
.
ConvertStringToInt32
(
c
.
Param
(
"memoID"
))
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"invalid memo id"
)
}
memo
,
err
:=
s
.
Store
.
GetMemo
(
ctx
,
&
store
.
FindMemo
{
ID
:
&
memoID
,
})
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"failed to retrieve memo"
)
}
if
memo
==
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusNotFound
,
"memo not found"
)
}
if
memo
.
Visibility
!=
store
.
Public
{
return
echo
.
NewHTTPError
(
http
.
StatusForbidden
,
"memo is not public"
)
}
indexHTML
:=
strings
.
ReplaceAll
(
rawIndexHTML
,
"<!-- memos.metadata -->"
,
generateMemoMetadata
(
memo
))
return
c
.
HTML
(
http
.
StatusOK
,
indexHTML
)
})
}
func
generateMemoMetadata
(
memo
*
store
.
Memo
)
string
{
metadataList
:=
[]
string
{
fmt
.
Sprintf
(
`<meta name="description" content="%s" />`
,
memo
.
Content
),
fmt
.
Sprintf
(
`<meta property="og:title" content="%s" />`
,
fmt
.
Sprintf
(
"Memos - %d"
,
memo
.
ID
)),
fmt
.
Sprintf
(
`<meta property="og:description" content="%s" />`
,
memo
.
Content
),
fmt
.
Sprintf
(
`<meta property="og:image" content="%s" />`
,
"https://www.usememos.com/logo.png"
),
`<meta property="og:type" content="website" />`
,
// Twitter related metadata.
fmt
.
Sprintf
(
`<meta name="twitter:title" content="%s" />`
,
fmt
.
Sprintf
(
"Memos - %d"
,
memo
.
ID
)),
fmt
.
Sprintf
(
`<meta name="twitter:description" content="%s" />`
,
memo
.
Content
),
fmt
.
Sprintf
(
`<meta name="twitter:image" content="%s" />`
,
"https://www.usememos.com/logo.png"
),
`<meta name="twitter:card" content="summary" />`
,
}
return
strings
.
Join
(
metadataList
,
"
\n
"
)
}
func
getFileSystem
(
path
string
)
http
.
FileSystem
{
fs
,
err
:=
fs
.
Sub
(
embeddedFiles
,
path
)
if
err
!=
nil
{
...
...
server/server.go
View file @
52743017
...
...
@@ -33,9 +33,6 @@ type Server struct {
Profile
*
profile
.
Profile
Store
*
store
.
Store
// API services.
apiV2Service
*
apiv2
.
APIV2Service
// Asynchronous runners.
backupRunner
*
backup
.
BackupRunner
telegramBot
*
telegram
.
Bot
...
...
@@ -84,7 +81,8 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
s
.
ID
=
serverID
// Serve frontend.
frontend
.
Serve
(
e
)
frontendService
:=
frontend
.
NewFrontendService
(
profile
,
store
)
frontendService
.
Serve
(
e
)
// Serve swagger in dev/demo mode.
if
profile
.
Mode
==
"dev"
||
profile
.
Mode
==
"demo"
{
...
...
@@ -110,9 +108,9 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
apiV1Service
:=
apiv1
.
NewAPIV1Service
(
s
.
Secret
,
profile
,
store
,
s
.
telegramBot
)
apiV1Service
.
Register
(
rootGroup
)
s
.
apiV2Service
=
apiv2
.
NewAPIV2Service
(
s
.
Secret
,
profile
,
store
,
s
.
Profile
.
Port
+
1
)
apiV2Service
:
=
apiv2
.
NewAPIV2Service
(
s
.
Secret
,
profile
,
store
,
s
.
Profile
.
Port
+
1
)
// Register gRPC gateway as api v2.
if
err
:=
s
.
apiV2Service
.
RegisterGateway
(
ctx
,
e
);
err
!=
nil
{
if
err
:=
apiV2Service
.
RegisterGateway
(
ctx
,
e
);
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"failed to register gRPC gateway"
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment