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
8101a5e0
Commit
8101a5e0
authored
Apr 07, 2024
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: add origin flag to config cors
parent
b5893aa6
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
12 deletions
+32
-12
main.go
bin/memos/main.go
+16
-9
profile.go
server/profile/profile.go
+2
-0
server.go
server/server.go
+14
-3
No files found.
bin/memos/main.go
View file @
8101a5e0
...
@@ -31,18 +31,19 @@ const (
...
@@ -31,18 +31,19 @@ const (
)
)
var
(
var
(
profile
*
_profile
.
Profile
profile
*
_profile
.
Profile
mode
string
mode
string
addr
string
addr
string
port
int
port
int
data
string
data
string
driver
string
driver
string
dsn
string
dsn
string
serveFrontend
bool
serveFrontend
bool
allowedOrigins
[]
string
rootCmd
=
&
cobra
.
Command
{
rootCmd
=
&
cobra
.
Command
{
Use
:
"memos"
,
Use
:
"memos"
,
Short
:
`An open
-source, self-hosted memo hub with knowledge management and social networking
.`
,
Short
:
`An open
source, lightweight note-taking service. Easily capture and share your great thoughts
.`
,
Run
:
func
(
_cmd
*
cobra
.
Command
,
_args
[]
string
)
{
Run
:
func
(
_cmd
*
cobra
.
Command
,
_args
[]
string
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
dbDriver
,
err
:=
db
.
NewDBDriver
(
profile
)
dbDriver
,
err
:=
db
.
NewDBDriver
(
profile
)
...
@@ -114,6 +115,7 @@ func init() {
...
@@ -114,6 +115,7 @@ func init() {
rootCmd
.
PersistentFlags
()
.
StringVarP
(
&
driver
,
"driver"
,
""
,
""
,
"database driver"
)
rootCmd
.
PersistentFlags
()
.
StringVarP
(
&
driver
,
"driver"
,
""
,
""
,
"database driver"
)
rootCmd
.
PersistentFlags
()
.
StringVarP
(
&
dsn
,
"dsn"
,
""
,
""
,
"database source name(aka. DSN)"
)
rootCmd
.
PersistentFlags
()
.
StringVarP
(
&
dsn
,
"dsn"
,
""
,
""
,
"database source name(aka. DSN)"
)
rootCmd
.
PersistentFlags
()
.
BoolVarP
(
&
serveFrontend
,
"frontend"
,
""
,
true
,
"serve frontend files"
)
rootCmd
.
PersistentFlags
()
.
BoolVarP
(
&
serveFrontend
,
"frontend"
,
""
,
true
,
"serve frontend files"
)
rootCmd
.
PersistentFlags
()
.
StringArrayVarP
(
&
allowedOrigins
,
"origins"
,
""
,
[]
string
{},
"CORS allowed domain origins"
)
err
:=
viper
.
BindPFlag
(
"mode"
,
rootCmd
.
PersistentFlags
()
.
Lookup
(
"mode"
))
err
:=
viper
.
BindPFlag
(
"mode"
,
rootCmd
.
PersistentFlags
()
.
Lookup
(
"mode"
))
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -143,12 +145,17 @@ func init() {
...
@@ -143,12 +145,17 @@ func init() {
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
err
)
panic
(
err
)
}
}
err
=
viper
.
BindPFlag
(
"origins"
,
rootCmd
.
PersistentFlags
()
.
Lookup
(
"origins"
))
if
err
!=
nil
{
panic
(
err
)
}
viper
.
SetDefault
(
"mode"
,
"demo"
)
viper
.
SetDefault
(
"mode"
,
"demo"
)
viper
.
SetDefault
(
"driver"
,
"sqlite"
)
viper
.
SetDefault
(
"driver"
,
"sqlite"
)
viper
.
SetDefault
(
"addr"
,
""
)
viper
.
SetDefault
(
"addr"
,
""
)
viper
.
SetDefault
(
"port"
,
8081
)
viper
.
SetDefault
(
"port"
,
8081
)
viper
.
SetDefault
(
"frontend"
,
true
)
viper
.
SetDefault
(
"frontend"
,
true
)
viper
.
SetDefault
(
"origins"
,
[]
string
{})
viper
.
SetEnvPrefix
(
"memos"
)
viper
.
SetEnvPrefix
(
"memos"
)
}
}
...
...
server/profile/profile.go
View file @
8101a5e0
...
@@ -32,6 +32,8 @@ type Profile struct {
...
@@ -32,6 +32,8 @@ type Profile struct {
Version
string
`json:"version"`
Version
string
`json:"version"`
// Frontend indicate the frontend is enabled or not
// Frontend indicate the frontend is enabled or not
Frontend
bool
`json:"-"`
Frontend
bool
`json:"-"`
// Origins is the list of allowed origins
Origins
[]
string
`json:"-"`
}
}
func
(
p
*
Profile
)
IsDev
()
bool
{
func
(
p
*
Profile
)
IsDev
()
bool
{
...
...
server/server.go
View file @
8101a5e0
...
@@ -49,7 +49,7 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
...
@@ -49,7 +49,7 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
}
}
// Register CORS middleware.
// Register CORS middleware.
e
.
Use
(
CORSMiddleware
())
e
.
Use
(
CORSMiddleware
(
s
.
Profile
.
Origins
))
serverID
,
err
:=
s
.
getSystemServerID
(
ctx
)
serverID
,
err
:=
s
.
getSystemServerID
(
ctx
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -160,7 +160,7 @@ func grpcRequestSkipper(c echo.Context) bool {
...
@@ -160,7 +160,7 @@ func grpcRequestSkipper(c echo.Context) bool {
return
strings
.
HasPrefix
(
c
.
Request
()
.
URL
.
Path
,
"/memos.api.v2."
)
return
strings
.
HasPrefix
(
c
.
Request
()
.
URL
.
Path
,
"/memos.api.v2."
)
}
}
func
CORSMiddleware
()
echo
.
MiddlewareFunc
{
func
CORSMiddleware
(
origins
[]
string
)
echo
.
MiddlewareFunc
{
return
func
(
next
echo
.
HandlerFunc
)
echo
.
HandlerFunc
{
return
func
(
next
echo
.
HandlerFunc
)
echo
.
HandlerFunc
{
return
func
(
c
echo
.
Context
)
error
{
return
func
(
c
echo
.
Context
)
error
{
if
grpcRequestSkipper
(
c
)
{
if
grpcRequestSkipper
(
c
)
{
...
@@ -170,7 +170,18 @@ func CORSMiddleware() echo.MiddlewareFunc {
...
@@ -170,7 +170,18 @@ func CORSMiddleware() echo.MiddlewareFunc {
r
:=
c
.
Request
()
r
:=
c
.
Request
()
w
:=
c
.
Response
()
.
Writer
w
:=
c
.
Response
()
.
Writer
w
.
Header
()
.
Set
(
"Access-Control-Allow-Origin"
,
r
.
Header
.
Get
(
"Origin"
))
requestOrigin
:=
r
.
Header
.
Get
(
"Origin"
)
if
len
(
origins
)
==
0
{
w
.
Header
()
.
Set
(
"Access-Control-Allow-Origin"
,
requestOrigin
)
}
else
{
for
_
,
origin
:=
range
origins
{
if
origin
==
requestOrigin
{
w
.
Header
()
.
Set
(
"Access-Control-Allow-Origin"
,
origin
)
break
}
}
}
w
.
Header
()
.
Set
(
"Access-Control-Allow-Methods"
,
"GET, POST, PUT, DELETE, PATCH, OPTIONS"
)
w
.
Header
()
.
Set
(
"Access-Control-Allow-Methods"
,
"GET, POST, PUT, DELETE, PATCH, OPTIONS"
)
w
.
Header
()
.
Set
(
"Access-Control-Allow-Headers"
,
"Content-Type, Authorization"
)
w
.
Header
()
.
Set
(
"Access-Control-Allow-Headers"
,
"Content-Type, Authorization"
)
w
.
Header
()
.
Set
(
"Access-Control-Allow-Credentials"
,
"true"
)
w
.
Header
()
.
Set
(
"Access-Control-Allow-Credentials"
,
"true"
)
...
...
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