Skip to content

Commit

Permalink
custom bind address
Browse files Browse the repository at this point in the history
  • Loading branch information
d-led committed Oct 8, 2020
1 parent 05a5941 commit 4e8b2f4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .link-checker-service.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

# proxy = "http://<some-proxy>:<port>"

# uncomment to bind to a custom address
# bindAddress = "127.0.0.1:8080"

# uncomment if CORS is undesired
corsOrigins = [
"http://localhost:8080",
Expand Down
7 changes: 6 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

Notable changes will be documented here

## 0.9.10 preliminary
## 0.9.11

- link-checker-service:
- `serve -a <addr>` 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:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <path-to-config-toml>`.

### Environment Variables
Expand Down
7 changes: 7 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -31,6 +33,7 @@ var serveCmd = &cobra.Command{
MaxURLsInRequest: maxURLsInRequest,
DisableRequestLogging: disableRequestLogging,
DomainBlacklistGlobs: domainBlacklistGlobs,
BindAddress: viper.GetString(bindAddressKey),
})
server.Run()
},
Expand Down Expand Up @@ -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")
Expand Down
12 changes: 10 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Options struct {
MaxURLsInRequest uint
DisableRequestLogging bool
DomainBlacklistGlobs []string
BindAddress string
}

// Server starts an instance of the link checker service
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 4e8b2f4

Please sign in to comment.