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

This commit is contained in:
Sandu Liviu Catalin 2023-08-24 04:59:23 +03:00 committed by GitHub
parent b9b795bf0e
commit 6c01e84099
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 3 deletions

View file

@ -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")

View file

@ -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

View file

@ -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) {