Skip to content

Commit 58c92b0

Browse files
authored
Add the IP annotation service to main() (#24)
Also clean up a lint error, and simplify Serve() by using errorx.Suppress
1 parent decbcdf commit 58c92b0

File tree

5 files changed

+25
-5
lines changed

5 files changed

+25
-5
lines changed

annotator/annotator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ type Network struct {
7373
Systems []System `json:",omitempty"`
7474
}
7575

76+
// FirstASN gives the ASN that should be used by systems that don't want to deal
77+
// with the complication of MOAS IP addresses.
7678
func (n *Network) FirstASN() uint32 {
7779
if n.Systems == nil || len(n.Systems) == 0 {
7880
return 0

ipservice/ipservice_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ func TestServerAndClientE2E(t *testing.T) {
6464
wg := sync.WaitGroup{}
6565
wg.Add(1)
6666
go func() {
67-
err := srv.Serve()
68-
if err != http.ErrServerClosed {
69-
t.Error("Bad error", err, "when we expected", http.ErrServerClosed)
70-
}
67+
rtx.Must(srv.Serve(), "Could not serve the annotator")
7168
wg.Done()
7269
}()
7370

ipservice/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net"
77
"net/http"
88

9+
"github.com/m-lab/go/errorx"
910
"github.com/m-lab/go/rtx"
1011
"github.com/m-lab/uuid-annotator/annotator"
1112
"github.com/m-lab/uuid-annotator/asnannotator"
@@ -72,7 +73,7 @@ type server struct {
7273
}
7374

7475
func (s *server) Serve() error {
75-
return s.srv.Serve(s.listener)
76+
return errorx.Suppress(s.srv.Serve(s.listener), http.ErrServerClosed)
7677
}
7778

7879
func (s *server) Close() error {

main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/m-lab/uuid-annotator/asnannotator"
1919
"github.com/m-lab/uuid-annotator/geoannotator"
2020
"github.com/m-lab/uuid-annotator/handler"
21+
"github.com/m-lab/uuid-annotator/ipservice"
2122
"github.com/m-lab/uuid-annotator/rawfile"
2223
"github.com/m-lab/uuid-annotator/siteannotator"
2324
)
@@ -130,5 +131,22 @@ func main() {
130131
wg.Done()
131132
}()
132133

134+
// Set up the local service to serve IP annotations as a local service on a
135+
// local unix-domain socket.
136+
if *ipservice.SocketFilename != "" {
137+
ipsrv, err := ipservice.NewServer(*ipservice.SocketFilename, asn, geo)
138+
rtx.Must(err, "Could not start up the local IP annotation service")
139+
wg.Add(2)
140+
go func() {
141+
rtx.Must(ipsrv.Serve(), "Could not serve the local IP annotation service")
142+
wg.Done()
143+
}()
144+
go func() {
145+
<-mainCtx.Done()
146+
ipsrv.Close()
147+
wg.Done()
148+
}()
149+
}
150+
133151
wg.Wait()
134152
}

main_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/m-lab/go/rtx"
1313
"github.com/m-lab/tcp-info/eventsocket"
14+
"github.com/m-lab/uuid-annotator/ipservice"
1415
)
1516

1617
func TestMainSmokeTest(t *testing.T) {
@@ -22,6 +23,7 @@ func TestMainSmokeTest(t *testing.T) {
2223
// Set up global variables.
2324
mainCtx, mainCancel = context.WithCancel(context.Background())
2425
*eventsocket.Filename = dir + "/eventsocket.sock"
26+
*ipservice.SocketFilename = dir + "/ipannotator.sock"
2527
rtx.Must(maxmindurl.Set("file:./testdata/fake.tar.gz"), "Failed to set maxmind url for testing")
2628
rtx.Must(routeviewv4.Set("file:./testdata/RouteViewIPv4.tiny.gz"), "Failed to set routeview v4 url for testing")
2729
rtx.Must(routeviewv6.Set("file:./testdata/RouteViewIPv6.tiny.gz"), "Failed to set routeview v6 url for testing")

0 commit comments

Comments
 (0)