Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

feat: add configuration option to bind server to specific address (#2165)

Sandu Liviu Catalin6c01e8409971
diff --git a/cmd/memos.go b/cmd/memos.go
index edcbd9740..e1e3f4102 100644
--- a/cmd/memos.go
+++ b/cmd/memos.go
@@ -32,6 +32,7 @@ const (
var (
profile *_profile.Profile
mode string
+ addr string
port int
data string
@@ -91,6 +92,7 @@ func init() {
cobra.OnInitialize(initConfig)
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().StringVarP(&data, "data", "d", "", "data directory")
@@ -98,6 +100,10 @@ func init() {
if err != nil {
panic(err)
}
+ err = viper.BindPFlag("addr", rootCmd.PersistentFlags().Lookup("addr"))
+ if err != nil {
+ panic(err)
+ }
err = viper.BindPFlag("port", rootCmd.PersistentFlags().Lookup("port"))
if err != nil {
panic(err)
@@ -108,6 +114,7 @@ func init() {
}
viper.SetDefault("mode", "demo")
+ viper.SetDefault("addr", "")
viper.SetDefault("port", 8081)
viper.SetEnvPrefix("memos")
}
@@ -124,6 +131,7 @@ func initConfig() {
println("---")
println("Server profile")
println("dsn:", profile.DSN)
+ println("addr:", profile.Addr)
println("port:", profile.Port)
println("mode:", profile.Mode)
println("version:", profile.Version)
@@ -132,7 +140,11 @@ func initConfig() {
func printGreetings() {
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("See more in:")
fmt.Printf("👉Website: %s\n", "https://usememos.com")
diff --git a/server/profile/profile.go b/server/profile/profile.go
index 1c0df4398..a785c59eb 100644
--- a/server/profile/profile.go
+++ b/server/profile/profile.go
@@ -15,6 +15,8 @@ import (
type Profile struct {
// Mode can be "prod" or "dev" or "demo"
Mode string `json:"mode"`
+ // Addr is the binding address for server
+ Addr string `json:"-"`
// Port is the binding port for server
Port int `json:"-"`
// Data is the data directory
diff --git a/server/server.go b/server/server.go
index 17d395285..837f21d8d 100644
--- a/server/server.go
+++ b/server/server.go
@@ -139,7 +139,7 @@ func (s *Server) Start(ctx context.Context) error {
go s.backupRunner.Run(ctx)
// 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 {
return err
}
@@ -152,7 +152,7 @@ func (s *Server) Start(ctx context.Context) error {
// programmatically set API version same as the server 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) {