From 4e8b2f4b72fa991c120a83209383b3b56c6e3a16 Mon Sep 17 00:00:00 2001 From: Dmitry Ledentsov Date: Thu, 8 Oct 2020 12:44:25 +0200 Subject: [PATCH] custom bind address --- .link-checker-service.toml | 3 +++ CHANGES.md | 7 ++++++- README.md | 3 ++- cmd/serve.go | 7 +++++++ server/server.go | 12 ++++++++++-- 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/.link-checker-service.toml b/.link-checker-service.toml index 0b73ac1..9b5d0e1 100644 --- a/.link-checker-service.toml +++ b/.link-checker-service.toml @@ -3,6 +3,9 @@ # proxy = "http://:" +# uncomment to bind to a custom address +# bindAddress = "127.0.0.1:8080" + # uncomment if CORS is undesired corsOrigins = [ "http://localhost:8080", diff --git a/CHANGES.md b/CHANGES.md index eabf184..236bf1a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,12 @@ Notable changes will be documented here -## 0.9.10 preliminary +## 0.9.11 + +- link-checker-service: + - `serve -a ` allows customizing the bind address, e.g. localhost-only: `127.0.0.1:8080` + +## 0.9.10 - binaries: link-checker-service, sample UI, sample large list check - link-checker-service: diff --git a/README.md b/README.md index 435c3e0..f81e661 100644 --- a/README.md +++ b/README.md @@ -143,11 +143,12 @@ For up-to-date help, check `link-checker-service help` or `link-checker-service To override the service port, define the `PORT` environment variable. +To bind to another address, configure the `bindAddress` option, i.e.: `... serve -a 127.0.0.1:8080` + ### Config File A sample configuration file [configuration file](.link-checker-service.toml) is available, with most possible configuration options listed. - Start the app with the path to the configuration file: `--config `. ### Environment Variables diff --git a/cmd/serve.go b/cmd/serve.go index 5d55cbb..2b7a812 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -19,6 +19,8 @@ var maxURLsInRequest uint = 0 var disableRequestLogging = false var domainBlacklistGlobs []string +const bindAddressKey = "bindAddress" + var serveCmd = &cobra.Command{ Use: "serve", Short: "Starts the link checker web server", @@ -31,6 +33,7 @@ var serveCmd = &cobra.Command{ MaxURLsInRequest: maxURLsInRequest, DisableRequestLogging: disableRequestLogging, DomainBlacklistGlobs: domainBlacklistGlobs, + BindAddress: viper.GetString(bindAddressKey), }) server.Run() }, @@ -62,6 +65,10 @@ func init() { flags.StringSliceVarP(&corsOrigins, "corsOrigins", "o", nil, "provide a list of CORS origins to enable CORS headers, e.g. '-o http://localhost:8080 -o http://localhost:8090") + flags.StringP(bindAddressKey, "a", "", + "bind to a different address other than `:8080`, i.e. 0.0.0.0:4444 or 127.0.0.1:4444") + _ = viper.BindPFlag(bindAddressKey, flags.Lookup(bindAddressKey)) + flags.StringVar(&IPRateLimit, "IPRateLimit", "", "rate-limit requests from an IP. e.g. 5-S (5 per second), 1000-H (1000 per hour)") serveCmd.PersistentFlags().BoolVarP(&disableRequestLogging, "disableRequestLogging", "s", false, "disable request logging") diff --git a/server/server.go b/server/server.go index 71dc225..161fb0c 100644 --- a/server/server.go +++ b/server/server.go @@ -36,6 +36,7 @@ type Options struct { MaxURLsInRequest uint DisableRequestLogging bool DomainBlacklistGlobs []string + BindAddress string } // Server starts an instance of the link checker service @@ -98,8 +99,15 @@ func (s *Server) Detail() *gin.Engine { // Run starts the service instance (binds a port) // set the PORT environment variable for a different port to bind at func (s *Server) Run() { - // listen and serve on 0.0.0.0:8080 - if err := s.server.Run(); err != nil { + var err error + if s.options.BindAddress!="" { + // custom bind address, e.g. 0.0.0.0:4444 + err = s.server.Run(s.options.BindAddress) + } else { + // default behavior: listen and serve on 0.0.0.0:${PORT:-8080} + err = s.server.Run() + } + if err != nil { log.Fatalf("Could not start the server: %v", err) } }