-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathserver_reference_implementation.go
More file actions
61 lines (52 loc) · 1.77 KB
/
server_reference_implementation.go
File metadata and controls
61 lines (52 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package server
import (
crypto_rand "crypto/rand"
"errors"
"fmt"
"net"
"net/http"
"time"
"github.com/IABTechLab/adscert/pkg/adscert/api"
"github.com/IABTechLab/adscert/pkg/adscert/discovery"
"github.com/IABTechLab/adscert/pkg/adscert/metrics"
"github.com/IABTechLab/adscert/pkg/adscert/server"
"github.com/IABTechLab/adscert/pkg/adscert/signatory"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
func SetUpAdsCertSignatoryServer(grpcServer *grpc.Server, adscertCallSign string, domainCheckInterval time.Duration, domainRenewalInterval time.Duration, privateKeys []string) {
signatoryApi := signatory.NewLocalAuthenticatedConnectionsSignatory(
adscertCallSign,
crypto_rand.Reader,
discovery.NewDefaultDnsResolver(),
discovery.NewDefaultDomainStore(),
domainCheckInterval,
domainRenewalInterval,
privateKeys)
handler := &server.AdsCertSignatoryServer{
SignatoryAPI: signatoryApi,
}
api.RegisterAdsCertSignatoryServer(grpcServer, handler)
reflection.Register(grpcServer)
}
func StartServingRequests(grpcServer *grpc.Server, serverPort int) error {
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", serverPort))
if err != nil {
return fmt.Errorf("error listening on TCP for gRPC server: %v", err)
}
// Start server and block indefinitely.
if err = grpcServer.Serve(listener); err != nil {
return fmt.Errorf("error serving gRPC: %v", err)
}
return nil
}
func StartMetricsServer(metricsPort int) error {
http.Handle("/metrics", promhttp.HandlerFor(metrics.GetAdscertMetricsRegistry(), promhttp.HandlerOpts{}))
err := http.ListenAndServe(fmt.Sprintf(":%d", metricsPort), nil)
// Ignore normal shutdown errors.
if errors.Is(err, http.ErrServerClosed) {
return nil
}
return err
}