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
b1e69564
Commit
b1e69564
authored
Sep 08, 2022
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: add cache for resource
parent
ad462cec
Changes
6
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
102 additions
and
81 deletions
+102
-81
go.mod
go.mod
+7
-7
go.sum
go.sum
+46
-24
embed_frontend.go
server/embed_frontend.go
+17
-3
resource.go
server/resource.go
+30
-0
server.go
server/server.go
+2
-3
webhook.go
server/webhook.go
+0
-44
No files found.
go.mod
View file @
b1e69564
...
@@ -8,20 +8,20 @@ require github.com/google/uuid v1.3.0
...
@@ -8,20 +8,20 @@ require github.com/google/uuid v1.3.0
require (
require (
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/mattn/go-colorable v0.1.1
1
// indirect
github.com/mattn/go-colorable v0.1.1
2
// indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
golang.org/x/crypto v0.0.0-202
10920023735-84f357641f63
golang.org/x/crypto v0.0.0-202
20722155217-630584e8d5aa
golang.org/x/net v0.0.0-202
10917221730-978cfadd31cf
// indirect
golang.org/x/net v0.0.0-202
20728030405-41545e8bf201
// indirect
golang.org/x/sys v0.0.0-20220
405052023-b1e9470b6e64
// indirect
golang.org/x/sys v0.0.0-20220
728004956-3c1f35247d10
// indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-202
01208040808-7e3f01d25324
// indirect
golang.org/x/time v0.0.0-202
20722155302-e5dcc9cfc0b9
// indirect
)
)
require (
require (
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/context v1.1.1 // indirect
github.com/labstack/echo/v4 v4.
6.3
github.com/labstack/echo/v4 v4.
9.0
github.com/labstack/gommon v0.3.1 // indirect
github.com/labstack/gommon v0.3.1 // indirect
)
)
...
@@ -29,7 +29,7 @@ require (
...
@@ -29,7 +29,7 @@ require (
github.com/VictoriaMetrics/fastcache v1.10.0
github.com/VictoriaMetrics/fastcache v1.10.0
github.com/gorilla/securecookie v1.1.1
github.com/gorilla/securecookie v1.1.1
github.com/gorilla/sessions v1.2.1
github.com/gorilla/sessions v1.2.1
github.com/labstack/echo-contrib v0.1
2
.0
github.com/labstack/echo-contrib v0.1
3
.0
)
)
require (
require (
...
...
go.sum
View file @
b1e69564
This diff is collapsed.
Click to expand it.
server/embed_frontend.go
View file @
b1e69564
...
@@ -12,8 +12,8 @@ import (
...
@@ -12,8 +12,8 @@ import (
//go:embed dist
//go:embed dist
var
embeddedFiles
embed
.
FS
var
embeddedFiles
embed
.
FS
func
getFileSystem
()
http
.
FileSystem
{
func
getFileSystem
(
path
string
)
http
.
FileSystem
{
fs
,
err
:=
fs
.
Sub
(
embeddedFiles
,
"dist"
)
fs
,
err
:=
fs
.
Sub
(
embeddedFiles
,
path
)
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
err
)
panic
(
err
)
}
}
...
@@ -22,8 +22,22 @@ func getFileSystem() http.FileSystem {
...
@@ -22,8 +22,22 @@ func getFileSystem() http.FileSystem {
}
}
func
embedFrontend
(
e
*
echo
.
Echo
)
{
func
embedFrontend
(
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
{
e
.
Use
(
middleware
.
StaticWithConfig
(
middleware
.
StaticConfig
{
HTML5
:
true
,
HTML5
:
true
,
Filesystem
:
getFileSystem
(),
Filesystem
:
getFileSystem
(
"dist"
),
}))
g
:=
e
.
Group
(
"assets"
)
g
.
Use
(
func
(
next
echo
.
HandlerFunc
)
echo
.
HandlerFunc
{
return
func
(
c
echo
.
Context
)
error
{
c
.
Response
()
.
Header
()
.
Set
(
echo
.
HeaderCacheControl
,
"max-age=31536000, immutable"
)
return
next
(
c
)
}
})
g
.
Use
(
middleware
.
StaticWithConfig
(
middleware
.
StaticConfig
{
HTML5
:
true
,
Filesystem
:
getFileSystem
(
"dist/assets"
),
}))
}))
}
}
server/resource.go
View file @
b1e69564
...
@@ -3,6 +3,7 @@ package server
...
@@ -3,6 +3,7 @@ package server
import
(
import
(
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"html"
"io"
"io"
"net/http"
"net/http"
"strconv"
"strconv"
...
@@ -168,3 +169,32 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
...
@@ -168,3 +169,32 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
return
c
.
JSON
(
http
.
StatusOK
,
true
)
return
c
.
JSON
(
http
.
StatusOK
,
true
)
})
})
}
}
func
(
s
*
Server
)
registerResourcePublicRoutes
(
g
*
echo
.
Group
)
{
g
.
GET
(
"/r/:resourceId/:filename"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
resourceID
,
err
:=
strconv
.
Atoi
(
c
.
Param
(
"resourceId"
))
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
fmt
.
Sprintf
(
"ID is not a number: %s"
,
c
.
Param
(
"resourceId"
)))
.
SetInternal
(
err
)
}
filename
:=
html
.
UnescapeString
(
c
.
Param
(
"filename"
))
resourceFind
:=
&
api
.
ResourceFind
{
ID
:
&
resourceID
,
Filename
:
&
filename
,
}
resource
,
err
:=
s
.
Store
.
FindResource
(
ctx
,
resourceFind
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
fmt
.
Sprintf
(
"Failed to fetch resource ID: %v"
,
resourceID
))
.
SetInternal
(
err
)
}
c
.
Response
()
.
Writer
.
WriteHeader
(
http
.
StatusOK
)
c
.
Response
()
.
Writer
.
Header
()
.
Set
(
"Content-Type"
,
resource
.
Type
)
c
.
Response
()
.
Header
()
.
Set
(
echo
.
HeaderCacheControl
,
"max-age=31536000, immutable"
)
if
_
,
err
:=
c
.
Response
()
.
Writer
.
Write
(
resource
.
Blob
);
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to write response"
)
.
SetInternal
(
err
)
}
return
nil
})
}
server/server.go
View file @
b1e69564
...
@@ -56,9 +56,8 @@ func NewServer(profile *profile.Profile) *Server {
...
@@ -56,9 +56,8 @@ func NewServer(profile *profile.Profile) *Server {
Profile
:
profile
,
Profile
:
profile
,
}
}
// Webhooks api skips auth checker.
publicRouteGroup
:=
e
.
Group
(
"/h"
)
webhookGroup
:=
e
.
Group
(
"/h"
)
s
.
registerResourcePublicRoutes
(
publicRouteGroup
)
s
.
registerWebhookRoutes
(
webhookGroup
)
apiGroup
:=
e
.
Group
(
"/api"
)
apiGroup
:=
e
.
Group
(
"/api"
)
apiGroup
.
Use
(
func
(
next
echo
.
HandlerFunc
)
echo
.
HandlerFunc
{
apiGroup
.
Use
(
func
(
next
echo
.
HandlerFunc
)
echo
.
HandlerFunc
{
...
...
server/webhook.go
deleted
100644 → 0
View file @
ad462cec
package
server
import
(
"fmt"
"html"
"net/http"
"strconv"
"github.com/usememos/memos/api"
"github.com/labstack/echo/v4"
)
func
(
s
*
Server
)
registerWebhookRoutes
(
g
*
echo
.
Group
)
{
g
.
GET
(
"/test"
,
func
(
c
echo
.
Context
)
error
{
return
c
.
HTML
(
http
.
StatusOK
,
"<strong>Hello, World!</strong>"
)
})
g
.
GET
(
"/r/:resourceId/:filename"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
resourceID
,
err
:=
strconv
.
Atoi
(
c
.
Param
(
"resourceId"
))
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
fmt
.
Sprintf
(
"ID is not a number: %s"
,
c
.
Param
(
"resourceId"
)))
.
SetInternal
(
err
)
}
filename
:=
html
.
UnescapeString
(
c
.
Param
(
"filename"
))
resourceFind
:=
&
api
.
ResourceFind
{
ID
:
&
resourceID
,
Filename
:
&
filename
,
}
resource
,
err
:=
s
.
Store
.
FindResource
(
ctx
,
resourceFind
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
fmt
.
Sprintf
(
"Failed to fetch resource ID: %v"
,
resourceID
))
.
SetInternal
(
err
)
}
c
.
Response
()
.
Writer
.
WriteHeader
(
http
.
StatusOK
)
c
.
Response
()
.
Writer
.
Header
()
.
Set
(
"Content-Type"
,
resource
.
Type
)
if
_
,
err
:=
c
.
Response
()
.
Writer
.
Write
(
resource
.
Blob
);
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to write response"
)
.
SetInternal
(
err
)
}
return
nil
})
}
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