|
| 1 | +package examples |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/alanshaw/ucantone/client" |
| 12 | + "github.com/alanshaw/ucantone/execution" |
| 13 | + "github.com/alanshaw/ucantone/ipld" |
| 14 | + "github.com/alanshaw/ucantone/principal/ed25519" |
| 15 | + "github.com/alanshaw/ucantone/result" |
| 16 | + "github.com/alanshaw/ucantone/server" |
| 17 | + "github.com/alanshaw/ucantone/ucan/invocation" |
| 18 | + "github.com/alanshaw/ucantone/validator/capability" |
| 19 | +) |
| 20 | + |
| 21 | +func TestServer(t *testing.T) { |
| 22 | + echoCapability, err := capability.New("/example/echo") |
| 23 | + if err != nil { |
| 24 | + panic(err) |
| 25 | + } |
| 26 | + |
| 27 | + serviceID, err := ed25519.Generate() |
| 28 | + if err != nil { |
| 29 | + panic(err) |
| 30 | + } |
| 31 | + |
| 32 | + ucanSrv := server.NewHTTP(serviceID) |
| 33 | + |
| 34 | + // Register an echo handler that returns the invocation arguments as the result |
| 35 | + ucanSrv.Handle(echoCapability, func(req execution.Request) (execution.Response, error) { |
| 36 | + return execution.NewResponse(execution.WithSuccess(req.Invocation().Arguments())) |
| 37 | + }) |
| 38 | + |
| 39 | + // Start the server on a random available port |
| 40 | + listener, err := net.Listen("tcp", ":0") |
| 41 | + if err != nil { |
| 42 | + panic(err) |
| 43 | + } |
| 44 | + |
| 45 | + httpSrv := http.Server{Handler: ucanSrv} |
| 46 | + |
| 47 | + go func() { |
| 48 | + err := httpSrv.Serve(listener) |
| 49 | + if err != nil && err != http.ErrServerClosed { |
| 50 | + panic(err) |
| 51 | + } |
| 52 | + }() |
| 53 | + |
| 54 | + serviceURL, err := url.Parse("http://" + listener.Addr().String()) |
| 55 | + if err != nil { |
| 56 | + panic(err) |
| 57 | + } |
| 58 | + fmt.Printf("UCAN Server is running at %s\n", serviceURL.String()) |
| 59 | + |
| 60 | + // Server is now running and can accept invocations! |
| 61 | + |
| 62 | + alice, err := ed25519.Generate() |
| 63 | + if err != nil { |
| 64 | + panic(err) |
| 65 | + } |
| 66 | + |
| 67 | + // Allow alice to invoke the echo capability |
| 68 | + dlg, err := echoCapability.Delegate(serviceID, alice, serviceID) |
| 69 | + if err != nil { |
| 70 | + panic(err) |
| 71 | + } |
| 72 | + |
| 73 | + inv, err := echoCapability.Invoke( |
| 74 | + alice, |
| 75 | + serviceID, |
| 76 | + ipld.Map{"message": "Hello, UCAN!"}, |
| 77 | + invocation.WithProofs(dlg.Link()), |
| 78 | + ) |
| 79 | + if err != nil { |
| 80 | + panic(err) |
| 81 | + } |
| 82 | + |
| 83 | + // create a client to send the invocation to the server |
| 84 | + c, err := client.NewHTTP(serviceURL) |
| 85 | + if err != nil { |
| 86 | + panic(err) |
| 87 | + } |
| 88 | + |
| 89 | + resp, err := c.Execute(execution.NewRequest(context.Background(), inv, execution.WithProofs(dlg))) |
| 90 | + if err != nil { |
| 91 | + panic(err) |
| 92 | + } |
| 93 | + |
| 94 | + result.MatchResultR0( |
| 95 | + resp.Result(), |
| 96 | + func(o ipld.Any) { |
| 97 | + fmt.Printf("Echo response: %v\n", o) |
| 98 | + }, |
| 99 | + func(x ipld.Any) { |
| 100 | + fmt.Printf("Invocation failed: %v\n", x) |
| 101 | + }, |
| 102 | + ) |
| 103 | + |
| 104 | + err = httpSrv.Shutdown(context.Background()) |
| 105 | + if err != nil { |
| 106 | + panic(err) |
| 107 | + } |
| 108 | +} |
0 commit comments