Skip to content

Commit b11f8b4

Browse files
authored
add sandbox binding for go example (#40)
1 parent 5c47980 commit b11f8b4

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

go-example-logs-api-extension/agent/http.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
"github.com/golang-collections/go-datastructures/queue"
1818
)
1919

20-
// DefaulHttpListenerPort is used to set the URL where the logs will be sent by Logs API
21-
const DefaulHttpListenerPort = "1234"
20+
// DefaultHttpListenerPort is used to set the URL where the logs will be sent by Logs API
21+
const DefaultHttpListenerPort = "1234"
2222

2323
// LogsApiHttpListener is used to listen to the Logs API using HTTP
2424
type LogsApiHttpListener struct {
@@ -36,12 +36,22 @@ func NewLogsApiHttpListener(lq *queue.Queue) (*LogsApiHttpListener, error) {
3636
}, nil
3737
}
3838

39+
func ListenOnAddress() string {
40+
env_aws_local, ok := os.LookupEnv("AWS_SAM_LOCAL")
41+
if ok && "true" == env_aws_local {
42+
return ":" + DefaultHttpListenerPort
43+
}
44+
45+
return "sandbox:" + DefaultHttpListenerPort
46+
}
47+
3948
// Start initiates the server in a goroutine where the logs will be sent
4049
func (s *LogsApiHttpListener) Start() (bool, error) {
41-
s.httpServer = &http.Server{Addr: ":" + DefaulHttpListenerPort}
50+
address := ListenOnAddress()
51+
s.httpServer = &http.Server{Addr: address}
4252
http.HandleFunc("/", s.http_handler)
4353
go func() {
44-
logger.Infof("Serving agent on %s", ":"+DefaulHttpListenerPort)
54+
logger.Infof("Serving agent on %s", address)
4555
err := s.httpServer.ListenAndServe()
4656
if err != http.ErrServerClosed {
4757
logger.Errorf("Unexpected stop on Http Server: %v", err)
@@ -138,7 +148,7 @@ func (a HttpAgent) Init(agentID string) error {
138148
}
139149
destination := logsapi.Destination{
140150
Protocol: logsapi.HttpProto,
141-
URI: logsapi.URI(fmt.Sprintf("http://sandbox:%s", DefaulHttpListenerPort)),
151+
URI: logsapi.URI(fmt.Sprintf("http://sandbox:%s", DefaultHttpListenerPort)),
142152
HttpMethod: logsapi.HttpPost,
143153
Encoding: logsapi.JSON,
144154
}

go-example-logs-api-extension/go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
github.com/aws/aws-sdk-go v1.35.17 h1:zhahppAMdPvJ9GP302SMOPW5SNoAbnjdOyaTmxA9WJU=
12
github.com/aws/aws-sdk-go v1.35.17/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k=
23
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
34
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/golang-collections/go-datastructures v0.0.0-20150211160725-59788d5eb259 h1:ZHJ7+IGpuOXtVf6Zk/a3WuHQgkC+vXwaqfUBDFwahtI=
46
github.com/golang-collections/go-datastructures v0.0.0-20150211160725-59788d5eb259/go.mod h1:9Qcha0gTWLw//0VNka1Cbnjvg3pNKGFdAm7E9sBabxE=
7+
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
58
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
9+
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
610
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
711
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
812
github.com/karrick/godirwalk v1.12.0 h1:nkS4xxsjiZMvVlazd0mFyiwD4BR9f3m6LXGhM2TUx3Y=
@@ -11,6 +15,7 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
1115
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
1216
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1317
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
18+
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
1419
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
1520
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1621
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
@@ -20,6 +25,7 @@ github.com/uudashr/gopkgs/v2 v2.1.2/go.mod h1:O9VKOuPWrUpVhaxcg7N3QiTrlDhgJb/84Y
2025
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2126
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
2227
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
28+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
2329
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2430
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
2531
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

0 commit comments

Comments
 (0)