Giter Site home page Giter Site logo

scottoffen / grapevine Goto Github PK

View Code? Open in Web Editor NEW
99.0 8.0 15.0 2.79 MB

Fast, unopinionated, embeddable, minimalist web framework for .NET

Home Page: https://scottoffen.github.io/grapevine/

License: MIT License

C# 95.98% HTML 2.04% CSS 1.80% JavaScript 0.19%
rest-api rest restful-api restful restapi restful-webservices rest-server rest-services rest-service rest-framework

grapevine's Introduction

Grapevine

Grapevine is a fast, unopinionated, embeddable, minimalist web framework for .NET. Grapevine is not intended to be a replacement for IIS or ASP.NET, but rather to function as an embedded REST/HTTP server in non-ASP.NET projects.

Installation

Grapevine is available on NuGet.org and can be installed using a NuGet package manager or the .NET CLI.

Powershell:

Install-Package Grapevine -Version 5.0.0-rc.10

.NET CLI

> dotnet add package Grapevine --version 5.0.0-rc.10

Usage

Grapevine is easy to get started with.

Create a simple route. This is the code that you want to run when a request comes in using the specified HTTP verb and path. Route methods must be asynchronous!

[RestResource]
public class MyResource
{
    [RestRoute("Get", "/api/test")]
    public async Task Test(IHttpContext context)
    {
        await context.Response.SendResponseAsync("Successfully hit the test route!");
    }
}

Next, create your first server using provided defaults (it's recommended to use the RestServerBuilder class to do this) and start it up!

using (var server = RestServerBuilder.UseDefaults().Build())
{
    server.Start();

    Console.WriteLine("Press enter to stop the server");
    Console.ReadLine();
}

Open your preferred browser and go to http://localhost:1234/api/test. You should see the following output in your browser:

Successfully hit the test route!

You'll see a lot of output in the console as well, because the defaults inject a console logger with the minimum level set to trace.

Support

  • Check out the project documentation https://scottoffen.github.io/grapevine.

  • Want to see a working project in action? Clone this repository and take a look at the Samples project.

  • Engage in our community discussions for Q&A, ideas, and show and tell!

  • Have a question you can't find an answer for in the documentation? For the fastest and best results, ask your questions on StackOverflow using #grapevine. Make sure you include the version of Grapevine you are using, the platform you using it on, code samples and any specific error messages you are seeing.

  • Issues created to ask "how to" questions will be closed.

Contributing

We welcome contributions from the community! In order to ensure the best experience for everyone, before creating an issue or submitting a pull request, please see the contributing guidelines and the code of conduct. Failure to adhere to these guidelines can result in significant delays in getting your contributions included in the project.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

Grapevine 5 is licensed under the MIT license.

Using Grapevine? We'd Love To Hear About It!

Few thing are as satisfying as hearing that your open source project is being used and appreciated by others. (Except for a nice MLT – mutton, lettuce and tomato sandwich, where the mutton is nice and lean and the tomato is ripe. They're so perky, I love that.) Jump over to the discussion boards and share the love!

grapevine's People

Contributors

rajeesh85 avatar sanchezpablo avatar scottoffen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grapevine's Issues

Access Violation

Hello and thanks for this wonderful project @scottoffen !
I started implementing in my project grapevine 4 since 5 was in rc. It was working quite fine but all my methods are async to I decided to move and try grapevine5.
I reimplemented routes as specified in the docs, added * as listening host through prefixes and all (I hope you can finish documentation because it is VERY scarce :-( ) but with grapevine5-rc7 as soon as I hit a route (for example a get route) the c# program containing the server immediately crashes with memory access violation.

has exited with code -1073741819 (0xc0000005) 'Access violation'.
Am I missing something? Thanks in advance!

EDIT: For what I can see the access violation crash is completely random, it happens on like 3/5 starts of the server both on get and post routes. Sometimes it is completely fine and works!

EDIT2: I found out rc6 is completely stable, so the problem must reside in some changes made in between, hope that can help ;)

More examples?

Trying to setup something simple for external logging purposes using rest endpoints on a server built in .NET.
Are there any examples of a more complete setup using Grapevine? Something that goes beyound localhost testing?
Like being able to set the host IP and port? I'm not sure where this is done.

Custom middleware, adding JSON parser

Hey, is it possible to add custom middleware? In my example, I need to parse the request body as JSON, so I don't wanna do it every time in each route, would be great if it can be included as a middleware and an extra string field where it contains a JSON that can be parsed from there, or even a generic parameter specifier that can be passed via attributes or something, so that the body is read as the specified type directly.

Thanks!

How to set the character set value of the Content-Type response header

Hi! I started playing with Grapevine recently, but still couldn't figure out how to properly respond with UTF-8 characters.
For example, "Injeção" becomes "Injeção".
Among other things, I tried setting Response.ContentEncoding to Text.Encoding.UTF8, but nothing seemed to work.
What am I doing wrong?

High CPU Usage

Hi all

The app that I am developing, is using 50% of my CPU (virtual machine) and the version that I use is V 5.0.0 rc.7 and I reduced the version to rc.6, then the cpu usage becomes 0% :)

So there is an issue in the latest pre-release (rc.7). Could you please check the problem?

I see that, a similar problem happened here
scottoffen/grapevine-legacy#138

Anyway I am using rc.6 for now and there is no problem in my side. Just want to mention that the rc.7 uses high cpu resource!

thanks

Feedback - dependencies - no thanks

Hello. I've found Grapevine V4. It looks great. Some feedback from one person.. I won't use Grapevine V5 because of the dependencies:

Grapeseed >= 5.0.0-rc.10)
Microsoft.Extensions.DependencyInjection >= 5.0.1
Microsoft.Extensions.DependencyInjection.Abstractions >= 5.0.0
Microsoft.Extensions.Logging.Abstractions >= 5.0.0
Microsoft.Extensions.Logging.Console >= 5.0.0

I don't want a nuget package which pulls in 5 others, then potentially more transitive dependencies. No thanks. Please just give me a nuget package with no dependencies like V4.

Regression from 4.0: You can no longer send a streamed response

In 4.0 I could write to the output stream in chunks, and then close the stream and the response. But I guess now, Grapevine checks and tries to close the response without checking if it's already closed resulting in an exception at the end of the response (after all the data has already been sent)

In theory just checking if the response has closed first would be sufficient, but a more sensible system would be to have a "MarkComplete"-like method so we can manually handle the outputstream and then let Grapevine know that the response is finished.

Grapevine v5 HTTPS support when run inside a .NET Linux container?

We have used Grapevine v4 for awhile and recently found an issue whereby our applications don't support HTTPS connections when they are run as .NET core apps in Linux containers. We have attributed this to dotnet/runtime#32721 (ignore the title of the post).

Can anyone tell me whether this is resolved in v5? And if so, how is Grapevine told which certificate to use (netsh.exe not available on a Linux container host)?

Thanks in anticipation!

Is it possible to use Grapevine in Windows Form apps?

I downloaded the source and Import Grapeseed and Grapevine into my solution that contain my own Windows Form Project.

I wrote this codes:

private void Main_Load(object sender, EventArgs e)
{
  LoadApiServer();
}

private void LoadApiServer()
{
  using (var server = RestServerBuilder.UseDefaults().Build())
  {
    server.Start();
    MyLogBox.WriteLine("API Server Started.");
  }
}

This is MyResource Class:

namespace MyWinFormApplication
{
    [RestResource]
    public class MyResource
    {
        [RestRoute("Get", "/api/info")]
        public async Task Test(IHttpContext context)
        {
            await context.Response.SendResponseAsync("Info Api Called");
        }
    }
}

After run the Winform Application, server not starts and not listening on default port (localhost:1234) and I can't use it.

Is it possible to use Grapevine in Windows Form apps?

StackOverflowException when running Readme sample code

I can start the server and even get the correct response (using Mozilla Firefox), but right after receiving it, the server just crashes.
I'm not exactly sure what I'm doing wrong and Google hasn't helped, sorry.

image

trce: Grapevine.IRouteScanner[0]
Begin Global Route Scanning
trce: Grapevine.IRouteScanner[0]
Generated route Grapevine.Route`1[MainServer.MyResource]
trce: Grapevine.IRouteScanner[0]
Scan of method Test complete: 1 total routes found
trce: Grapevine.IRouteScanner[0]
Scan of type MyResource complete: 1 total routes found
trce: Grapevine.IRouteScanner[0]
Global Route Scanning Complete: 1 total routes found
trce: Grapevine.IRestServer[0]
c29054bd-64b1-4891-8350-ade2a69cca22 : Request Received Get /api/test
trce: Grapevine.IRestServer[0]
c29054bd-64b1-4891-8350-ade2a69cca22 : Invoking OnRequest Handlers for Get /api/test
trce: Grapevine.IRestServer[0]
c29054bd-64b1-4891-8350-ade2a69cca22 : OnRequest Handlers Invoked for Get /api/test
trce: Grapevine.IRestServer[0]
c29054bd-64b1-4891-8350-ade2a69cca22 : Routing request Get /api/test
dbug: Grapevine.IRouter[0]
c29054bd-64b1-4891-8350-ade2a69cca22 : Routing Get /api/test
dbug: Grapevine.IRouter[0]
c29054bd-64b1-4891-8350-ade2a69cca22 : Matching routes discovered for Get /api/test
trce: Grapevine.IRouter[0]
c29054bd-64b1-4891-8350-ade2a69cca22 : Invoking before routing handlers for Get /api/test
trce: Grapevine.IRouter[0]
c29054bd-64b1-4891-8350-ade2a69cca22 : Before routing handlers invoked for Get /api/test
dbug: Grapevine.IRouter[0]
c29054bd-64b1-4891-8350-ade2a69cca22 : Executing MyResource.Test for Get /api/test
dbug: Grapevine.IRouter[0]
c29054bd-64b1-4891-8350-ade2a69cca22 : 1 of 1 routes invoked
trce: Grapevine.IRouter[0]
c29054bd-64b1-4891-8350-ade2a69cca22 : Invoking after routing handlers for Get /api/test
trce: Grapevine.IRouter[0]
c29054bd-64b1-4891-8350-ade2a69cca22 : After routing handlers invoked for Get /api/test
trce: Grapevine.IRestServer[0]
514cd167-113e-44ee-b52f-9fd95b1c3bb5 : Request Received Get /favicon.ico
trce: Grapevine.IRestServer[0]
514cd167-113e-44ee-b52f-9fd95b1c3bb5 : Invoking OnRequest Handlers for Get /favicon.ico
trce: Grapevine.IRestServer[0]
514cd167-113e-44ee-b52f-9fd95b1c3bb5 : OnRequest Handlers Invoked for Get /favicon.ico
trce: Grapevine.IRestServer[0]
514cd167-113e-44ee-b52f-9fd95b1c3bb5 : Routing request Get /favicon.ico
dbug: Grapevine.IRouter[0]
514cd167-113e-44ee-b52f-9fd95b1c3bb5 : Routing Get /favicon.ico

Process is terminated due to StackOverflowException.
An unhandled exception of type 'System.StackOverflowException' occurred in Grapevine.dll

javascript modules

I'm trying to serve up a html site using javascript modules, but it won't run the javascript. How do a serve them with a
Content-Type header text/javascript ??
Thanks

Please provide assemblies with strong name

Hello Scott,
using your great grapevine / grapeseed assemblies from a COM exe via COM-Interop, referenced assemblies need to have a strong name.
Could you please provide signed assemblies, e.g. using sn tool to create your own sn-file.
Hint: in your AssemblyInfo.cs I had to insert the public key from sn-file.
[assembly: System.Runtime.CompilerServices.InternalsVisibleToAttribute("Grapevine.Tests,PublicKey=xxx")]
You can easily extract the public key "xxx", refer to: https://stackoverflow.com/questions/1123683/how-to-declare-a-friend-assembly
Thank you very much in advance!
Michael.

Websocket Support

Do we have WebSocket support in Grapevine 5? Or server sent events? If no, are they planned?

Prefixes.Add throw exception

server.Prefixes.Add("http://*:50055");

That line of code throws an exception for me.
'Only Uri prefixes ending in '/' are allowed. (Parameter 'uriPrefix')'
Running 5.0.0-rc

Access to RestServer in RestResources class

RestResources in older versions seem to inherit the RestServer from a base class.
In the latest build, this feature seems to have disappeared, and RestResource is an attribute.

I run multiple instances of RestServer and need the RestResources to behave a little different for each RestServer instance.

Any hints on how to pass RestServer into RestResources?
Seems a little too complex for me to figure out.

Stack overflow crash when unknown route received

Stack Trace

This happens with the sample project if you provide a route not defined. http://localhost:1234/asdfg for example.

This will also happen with the sample code shown in the project readme if the request is made in the browser. This is because the browser requests for the favicon.ico to which there is no route for that defined.

How can I get the request payload?

IHttpContext context.Request.Payload was available in 4.2 version.
But it seems to have been removed.
How do I go about getting the request payload?

I tried this code below. Sometimes it works. Sometimes it doesn't return any data (even when the content-length is greater than 0.

using var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding);
var payload = await reader.ReadToEndAsync();

Injected IRestServer is not the running server

Found in 5.0.0-rc.7

The sample project route http://localhost:1234/api/stop fails to stop the running server because the instance is being pulled from a different service provider than the one used to generate the running server (ergo, different instance).

The following fix will be in the RestServerBuilder.Build() method of the next version:

  // Override with instances
  Services.AddSingleton<IRestServer>(server);
  Services.AddSingleton<IRouter>(server.Router);
  Services.AddSingleton<IRouteScanner>(server.RouteScanner);

This will override the previous service registration of the interface to a concretion with a registration to an instance.

[bug] RestServer not working on localhost

I have built a server in the past that works on localhost, but today after pasting a sample code of it i could get it work on in new project.
here is my code:

ServerSettings setting;
RestServer server;

public Core()
{
    setting = new ServerSettings();
    setting.Port = "50055";
    setting.Host = "*";
    Start();
}

private void Start()
{
    using (server = new RestServer(setting))
    {
        server.LogToConsole(Grapevine.Interfaces.Shared.LogLevel.Debug);
        server.Start();
    }
}

this code works on my previous app(localhost) but not with new app. i have no idea.

Can not run on window server when login with user domain

Hi,

I use grapevine to make a rest server.
When logging into windows with local admin rights, everything works normally.
But when logging into windows with the domain and user rights, the rest server cannot start.
I followed this instruction https://scottoffen.github.io/grapevine-legacy/en/using-httplistener.html
to successfully execute the command:
netsh http add urlacl url=http://+:1234/ user=abc.com.vn\thangnv
Please help me resolve this issue

Need legacy framework support

Hi,
I'm discovering this project and plan to use it.
But I'm developing in a context where I can't migrate to new framework Core.
For that, I will go to version 4 but I don't find any documentation or sample or Wiki page for version 4.
Where can I get it?
Thanks

Parsing Form Data

Is there a way to correctly parse Form Data? I have a form that accepts one file and I'm unable to parse and save the file effectively although I can do it easily on an ASP.NET API

For more context, I saw this response and tried to do the same without success. Must be because it's old.
scottoffen/grapevine-legacy#193

Additional extension methods for sending responses

Add the following extension methods to IHttpResponse for sending responses with the status code as the first parameter:

  • IHttpResponse.SendResponseAsync(HttpStatusCode statusCode, byte[] content)
  • IHttpResponse.SendResponseAsync(HttpStatusCode statusCode, string content)
  • IHttpResponse.SendResponseAsync(HttpStatusCode statusCode, Stream content)

In response to #87

Handling multiple GET request parameters

The documentation leaves this really unclear

Say I have a route like this
[RestRoute("Get", "/updatetasks/{blockID}/{isChecked}")]

What's the proper way to hit this route?? The below did not work.
localhost/updatetasks/myblockid/false

I've tried so many variations and none work. Using ?blockID params don't work either...

Please advise.

Unable to embed ssl certificates.

Feature request: Swap usage of HttpListeners for other libraries.

Quickly realized upon trying to use this library that you are heavily dependent on MS HttpListener functionality for routing as well as your current iteration of "builder" i.e. factory classes.

In particular there is no way to use embedded certs (for a use case for embedded certs where installing certs is counterproductive and undesirable). This is entirely possible and multiple other C# libraries do this without a problem. However the use of HttpListener makes this difficult if not impossible to achieve with this library. As a result I am evaluating different more capable libraries...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.