Skip to content

Commit

Permalink
feat: add random port option to global http (#644)
Browse files Browse the repository at this point in the history
  • Loading branch information
laojianzi authored Nov 6, 2023
1 parent 342e58a commit 76fe3f4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
23 changes: 17 additions & 6 deletions cmd/loggie/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ package main
import (
"flag"
"fmt"
"github.com/loggie-io/loggie/pkg/ops"
"github.com/loggie-io/loggie/pkg/util/json"
"github.com/pkg/errors"
"go.uber.org/automaxprocs/maxprocs"
"net"
"net/http"
"os"
"path/filepath"
Expand All @@ -40,9 +37,13 @@ import (
"github.com/loggie-io/loggie/pkg/discovery/kubernetes"
"github.com/loggie-io/loggie/pkg/eventbus"
_ "github.com/loggie-io/loggie/pkg/include"
"github.com/loggie-io/loggie/pkg/ops"
"github.com/loggie-io/loggie/pkg/ops/helper"
"github.com/loggie-io/loggie/pkg/util/json"
"github.com/loggie-io/loggie/pkg/util/persistence"
"github.com/loggie-io/loggie/pkg/util/yaml"
"github.com/pkg/errors"
"go.uber.org/automaxprocs/maxprocs"
)

var (
Expand Down Expand Up @@ -139,8 +140,18 @@ func main() {

if syscfg.Loggie.Http.Enabled {
go func() {
if err = http.ListenAndServe(fmt.Sprintf("%s:%d", syscfg.Loggie.Http.Host, syscfg.Loggie.Http.Port), nil); err != nil {
log.Fatal("http listen and serve err: %v", err)
if syscfg.Loggie.Http.RandPort {
syscfg.Loggie.Http.Port = 0
}

listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", syscfg.Loggie.Http.Host, syscfg.Loggie.Http.Port))
if err != nil {
log.Fatal("http listen err: %v", err)
}

log.Info("http listen addr %s", listener.Addr().String())
if err = http.Serve(listener, nil); err != nil {
log.Fatal("http serve err: %v", err)
}
}()
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/core/sysconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ func (d *Defaults) SetDefaults() {
}

type Http struct {
Enabled bool `yaml:"enabled" default:"false"`
Host string `yaml:"host" default:"0.0.0.0"`
Port int `yaml:"port" default:"9196"`
Enabled bool `yaml:"enabled" default:"false"`
Host string `yaml:"host" default:"0.0.0.0"`
Port int `yaml:"port" default:"9196"`
RandPort bool `yaml:"randPort" default:"false"`
}

0 comments on commit 76fe3f4

Please sign in to comment.