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
6c01e840
Unverified
Commit
6c01e840
authored
Aug 24, 2023
by
Sandu Liviu Catalin
Committed by
GitHub
Aug 24, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add configuration option to bind server to specific address (#2165)
parent
b9b795bf
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
17 additions
and
3 deletions
+17
-3
memos.go
cmd/memos.go
+13
-1
profile.go
server/profile/profile.go
+2
-0
server.go
server/server.go
+2
-2
No files found.
cmd/memos.go
View file @
6c01e840
...
@@ -32,6 +32,7 @@ const (
...
@@ -32,6 +32,7 @@ const (
var
(
var
(
profile
*
_profile
.
Profile
profile
*
_profile
.
Profile
mode
string
mode
string
addr
string
port
int
port
int
data
string
data
string
...
@@ -91,6 +92,7 @@ func init() {
...
@@ -91,6 +92,7 @@ func init() {
cobra
.
OnInitialize
(
initConfig
)
cobra
.
OnInitialize
(
initConfig
)
rootCmd
.
PersistentFlags
()
.
StringVarP
(
&
mode
,
"mode"
,
"m"
,
"demo"
,
`mode of server, can be "prod" or "dev" or "demo"`
)
rootCmd
.
PersistentFlags
()
.
StringVarP
(
&
mode
,
"mode"
,
"m"
,
"demo"
,
`mode of server, can be "prod" or "dev" or "demo"`
)
rootCmd
.
PersistentFlags
()
.
StringVarP
(
&
addr
,
"addr"
,
"a"
,
""
,
"address of server"
)
rootCmd
.
PersistentFlags
()
.
IntVarP
(
&
port
,
"port"
,
"p"
,
8081
,
"port of server"
)
rootCmd
.
PersistentFlags
()
.
IntVarP
(
&
port
,
"port"
,
"p"
,
8081
,
"port of server"
)
rootCmd
.
PersistentFlags
()
.
StringVarP
(
&
data
,
"data"
,
"d"
,
""
,
"data directory"
)
rootCmd
.
PersistentFlags
()
.
StringVarP
(
&
data
,
"data"
,
"d"
,
""
,
"data directory"
)
...
@@ -98,6 +100,10 @@ func init() {
...
@@ -98,6 +100,10 @@ func init() {
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
err
)
panic
(
err
)
}
}
err
=
viper
.
BindPFlag
(
"addr"
,
rootCmd
.
PersistentFlags
()
.
Lookup
(
"addr"
))
if
err
!=
nil
{
panic
(
err
)
}
err
=
viper
.
BindPFlag
(
"port"
,
rootCmd
.
PersistentFlags
()
.
Lookup
(
"port"
))
err
=
viper
.
BindPFlag
(
"port"
,
rootCmd
.
PersistentFlags
()
.
Lookup
(
"port"
))
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
err
)
panic
(
err
)
...
@@ -108,6 +114,7 @@ func init() {
...
@@ -108,6 +114,7 @@ func init() {
}
}
viper
.
SetDefault
(
"mode"
,
"demo"
)
viper
.
SetDefault
(
"mode"
,
"demo"
)
viper
.
SetDefault
(
"addr"
,
""
)
viper
.
SetDefault
(
"port"
,
8081
)
viper
.
SetDefault
(
"port"
,
8081
)
viper
.
SetEnvPrefix
(
"memos"
)
viper
.
SetEnvPrefix
(
"memos"
)
}
}
...
@@ -124,6 +131,7 @@ func initConfig() {
...
@@ -124,6 +131,7 @@ func initConfig() {
println
(
"---"
)
println
(
"---"
)
println
(
"Server profile"
)
println
(
"Server profile"
)
println
(
"dsn:"
,
profile
.
DSN
)
println
(
"dsn:"
,
profile
.
DSN
)
println
(
"addr:"
,
profile
.
Addr
)
println
(
"port:"
,
profile
.
Port
)
println
(
"port:"
,
profile
.
Port
)
println
(
"mode:"
,
profile
.
Mode
)
println
(
"mode:"
,
profile
.
Mode
)
println
(
"version:"
,
profile
.
Version
)
println
(
"version:"
,
profile
.
Version
)
...
@@ -132,7 +140,11 @@ func initConfig() {
...
@@ -132,7 +140,11 @@ func initConfig() {
func
printGreetings
()
{
func
printGreetings
()
{
fmt
.
Print
(
greetingBanner
)
fmt
.
Print
(
greetingBanner
)
fmt
.
Printf
(
"Version %s has been started on port %d
\n
"
,
profile
.
Version
,
profile
.
Port
)
if
len
(
profile
.
Addr
)
==
0
{
fmt
.
Printf
(
"Version %s has been started on port %d
\n
"
,
profile
.
Version
,
profile
.
Port
)
}
else
{
fmt
.
Printf
(
"Version %s has been started on address '%s' and port %d
\n
"
,
profile
.
Version
,
profile
.
Addr
,
profile
.
Port
)
}
fmt
.
Println
(
"---"
)
fmt
.
Println
(
"---"
)
fmt
.
Println
(
"See more in:"
)
fmt
.
Println
(
"See more in:"
)
fmt
.
Printf
(
"👉Website: %s
\n
"
,
"https://usememos.com"
)
fmt
.
Printf
(
"👉Website: %s
\n
"
,
"https://usememos.com"
)
...
...
server/profile/profile.go
View file @
6c01e840
...
@@ -15,6 +15,8 @@ import (
...
@@ -15,6 +15,8 @@ import (
type
Profile
struct
{
type
Profile
struct
{
// Mode can be "prod" or "dev" or "demo"
// Mode can be "prod" or "dev" or "demo"
Mode
string
`json:"mode"`
Mode
string
`json:"mode"`
// Addr is the binding address for server
Addr
string
`json:"-"`
// Port is the binding port for server
// Port is the binding port for server
Port
int
`json:"-"`
Port
int
`json:"-"`
// Data is the data directory
// Data is the data directory
...
...
server/server.go
View file @
6c01e840
...
@@ -139,7 +139,7 @@ func (s *Server) Start(ctx context.Context) error {
...
@@ -139,7 +139,7 @@ func (s *Server) Start(ctx context.Context) error {
go
s
.
backupRunner
.
Run
(
ctx
)
go
s
.
backupRunner
.
Run
(
ctx
)
// Start gRPC server.
// Start gRPC server.
listen
,
err
:=
net
.
Listen
(
"tcp"
,
fmt
.
Sprintf
(
"
:%d"
,
s
.
Profile
.
Port
+
1
))
listen
,
err
:=
net
.
Listen
(
"tcp"
,
fmt
.
Sprintf
(
"
%s:%d"
,
s
.
Profile
.
Addr
,
s
.
Profile
.
Port
+
1
))
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
@@ -152,7 +152,7 @@ func (s *Server) Start(ctx context.Context) error {
...
@@ -152,7 +152,7 @@ func (s *Server) Start(ctx context.Context) error {
// programmatically set API version same as the server version
// programmatically set API version same as the server version
apiv1
.
SwaggerInfo
.
Version
=
s
.
Profile
.
Version
apiv1
.
SwaggerInfo
.
Version
=
s
.
Profile
.
Version
return
s
.
e
.
Start
(
fmt
.
Sprintf
(
"
:%d"
,
s
.
Profile
.
Port
))
return
s
.
e
.
Start
(
fmt
.
Sprintf
(
"
%s:%d"
,
s
.
Profile
.
Addr
,
s
.
Profile
.
Port
))
}
}
func
(
s
*
Server
)
Shutdown
(
ctx
context
.
Context
)
{
func
(
s
*
Server
)
Shutdown
(
ctx
context
.
Context
)
{
...
...
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