Library for .NET used to create next generation REST, real time, and RPC APIs, where all your reactive web clients are synchronized seamlessly through Resgate.
Visit Resgate.io for more information.
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");
Example | Description |
---|---|
Hello World | Smallest of services serving a static message. |
Edit Text | Single text field that is updated in real time. |
Book Collection | List of book titles & authors that can be edited by many. |
Search | Make live queries against a large customer database. |
ResService service = new ResService("myservice");
[ResourcePattern("mymodel")]
class MyModelHandler : BaseHandler
{
private readonly object model = new
{
message = "Hello, .NET World!"
};
public void Get(IModelRequest request)
{
request.Model(model);
}
}
[ResourcePattern("mycollection")]
class MyCollectionHandler : BaseHandler
{
private readonly object[] collection = new object[]{
"first", "second", "third"
};
public void Get(ICollectionRequest request)
{
request.Collection(collection);
}
}
[ResourcePattern("math")]
class MyResourceHandler : BaseHandler
{
[CallMethod("double")]
public void Double(ICallRequest r)
{
r.Ok(2 * (double)r.Params["value"]);
}
}
service.AddHandler(new MyResourceHandler());
service.AddHandler("article.$id", new DynamicHandler()
.Access(r => r.AccessGranted())
.ModelGet(r =>
{
if (DB.TryGetArticle(r.PathParams["id"], out Article article))
r.Model(article);
else
r.NotFound();
}));
A change event will update the model on all subscribing clients.
MyModel mymodel = new MyModel { Name = "foo" };
MyModel mymodel = new MyModel { Name = "foo" };
service.With("example.mymodel", resource =>
{
mymodel.Name = "bar";
resource.ChangeEvent(new Dictionary<string, object> {
{ "name", "bar" }
});
});
An add event will update the collection for all subscribing clients.
var mycollection = new List<string> { "first", "second" };
service.With("example.mycollection", resource =>
{
resource.AddEvent("third", mycollection.Count);
mycollection.Add("third");
});
service.AddHandler("myauth", new DynamicHandler()
.AuthMethod("login", r =>
{
if ((string)r.Params["password"] == "mysecret")
{
r.TokenEvent(new { user = "admin" });
r.Ok();
}
else
{
r.InvalidParams("Wrong password");
}
}));
service.AddHandler(">", new DynamicHandler()
.Access(r =>
{
if (r.Token != null && (string)r.Token["user"] == "admin")
r.AccessGranted();
else
r.AccessDenied();
}));
service.AddHandler("store.users", new DynamicHandler()
.Get(async r =>
{
var users = await DB.QueryAsync("SELECT id FROM users");
r.Collection(users.Select(u => new Ref("store.user." + u.Id)));
}));
service.Serve("nats://127.0.0.1:4222");
Inspiration and support in development from github.com/novagen, who wrote an initial .NET library for Resgate.
The .NET library is still under development, but the API is mostly settled. Any feedback on the library API or its implementation is highly appreciated!
Once the API is fully settled, the package will be moved to the resgateio GitHub organization.
If you find any issues, feel free to report them as an Issue.