コード例 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Authorization microservice");

            ResService service = new ResService();

            service.AddHandler(">", new DynamicHandler()
                               .SetAccess(req =>
            {
                if (IsPublic || (req.Token != null && (string)req.Token["user"] == "admin"))
                {
                    req.AccessGranted();
                }
                else
                {
                    req.AccessDenied();
                }
            }));

            service.SetOwnedResources(null, new[] { ">" });
            service.Serve("nats://127.0.0.1:4222");

            while (true)
            {
                Console.WriteLine(IsPublic ? "Access for everyone" : "Access only for admins");
                if (Console.ReadLine() == "QUIT")
                {
                    break;
                }
                IsPublic = !IsPublic;
                service.ResetAll();
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Authentication microservice");

            ResService service = new ResService("authentication");

            service.AddHandler("user", new DynamicHandler()
                               .SetAuthMethod("login", req =>
            {
                if ((string)req.Params["password"] == "mysecret")
                {
                    req.TokenEvent(new { user = "******", foo = "bar" });
                    req.Ok();
                }
                else
                {
                    req.InvalidParams("Wrong password");
                }
            })
                               .SetAuthMethod("logout", req =>
            {
                req.TokenEvent(null);
                req.Ok();
            }));

            service.Serve("nats://127.0.0.1:4222");
            Console.ReadLine();
        }
コード例 #3
0
        static void Main(string[] args)
        {
            ResService service = new ResService("example");

            service.AddHandler("model", new DynamicHandler()
                               .Get(r => r.Model(new
            {
                message = "Hello, World!",
            }))
                               .Access(r => r.AccessGranted()));
            service.Serve("nats://127.0.0.1:4222");
            Console.ReadLine();
        }
コード例 #4
0
        #pragma warning disable xUnit1013 // The test requires a running NATS server
        public void Usage_HelloWorld()
        #pragma warning restore xUnit1013
        {
            ResService service = new ResService("example");

            service.AddHandler("model", new DynamicHandler()
                               .Get(r => r.Model(new {
                message = "Hello, World!"
            }))
                               .Access(r => r.AccessGranted()));
            service.Serve("nats://127.0.0.1:4222");
            // ---
            service.Shutdown();
        }
コード例 #5
0
        static void Main(string[] args)
        {
            // Create a new RES Service
            ResService service = new ResService("text");

            // Add handler for "text.mymodel" resource
            service.AddHandler("mymodel", new MyModelHandler());

            // Start the service
            service.Serve("nats://127.0.0.1:4222");

            // Wait for enter and then stop
            Console.ReadLine();
            service.Shutdown();
        }
コード例 #6
0
        public void Usage_DefineMethodsOnAHandlerClass()
        {
            ResService service = new ResService("example");

            service.AddHandler(new MyResourceHandler());
            service.Serve(Conn);
            Conn.GetMsg().AssertSubject("system.reset");
            string inbox = Conn.NATSRequest("call.example.math.double", new RequestDto {
                CID = Test.CID, Params = new { value = 7 }
            });

            Conn.GetMsg()
            .AssertSubject(inbox)
            .AssertResult(14.0);
        }
コード例 #7
0
        static void Main(string[] args)
        {
            // Create a new RES Service
            ResService service = new ResService("library");

            // Add handler for "example.mymodel" resource
            service.AddHandler("book.$id", new BookModelHandler());
            service.AddHandler("books", new BookCollectionHandler());

            // Start the service
            service.Serve("nats://127.0.0.1:4222");

            // Wait for enter and then stop
            Console.ReadLine();
            service.Shutdown();
        }
コード例 #8
0
        static void Main(string[] args)
        {
            var service = new ResService("search");

            // Open or create the database
            using (var db = new LiteDatabase(@"lite.db"))
            {
                // Add handlers for the resources
                service.AddHandler(new CustomerHandler(db));
                service.AddHandler(new CustomersHandler(db));
                service.AddHandler(new CountriesHandler());

                // Start the service
                service.Serve("nats://127.0.0.1:4222");

                // Wait for enter and then stop
                Console.ReadLine();
                service.Shutdown();
            }
        }
コード例 #9
0
        static void Main(string[] args)
        {
            Console.WriteLine("AwesomeTicker microservice");

            ResService service = new ResService("awesomeTicker");

            service.AddHandler("ticker", new DynamicHandler()
                               .SetModelGet(req => req.Model(new { count = Count })));

            service.Serve("nats://127.0.0.1:4222");

            Timer timer = new Timer(e =>
            {
                service.With("awesomeTicker.ticker", resource =>
                {
                    Count++;
                    resource.ChangeEvent(new Dictionary <string, object> {
                        { "count", Count }
                    });
                });
            }, null, 1000, 1000);

            Console.ReadLine();
        }