Git Product home page Git Product logo

sharpwebserver's Introduction

SharpWebServer

Simple and small library built on top of HttpListener with fast routing and built-in session storage.

Install

Copy the SharpWebServer folder to your solution and you're done.

Example

using System;
using System.Collections.Generic;
using SharpWebServer;

class Program {
    static Random random = new Random();

    static void Main(string[] args)
    {
        /* instantiate new router */
        Router router = new Router();


        /* 
         *  GET /
         */
        router.Routes.Add(new Route("GET", null, @"^\/$", request => {
            request.Respond("Hello world!");
        }));


        /* 
         *  GET /random/(min)/(max)
         */
        router.Routes.Add(new Route("GET", null, @"^\/random\/([1-9]{1}[0-9]*)\/([1-9]{1}[0-9]*)$", request => {
            int min = int.Parse(request.Groups[0]);
            int max = int.Parse(request.Groups[1]);

            request.SetContentType("text/plain");

            if (min <= max)
            {
                // respond with random number between (min) and (max)
                request.Respond(random.Next(min, max+1).ToString());
            } else
            {
                // error
                request.Respond("Error: (min) must be smaller or equal to (max)");
            }
        }));
        
        
        /* 
         *  GET /set/(value)
         */
        router.Routes.Add(new Route("GET", null, @"^\/set\/([^\/]*)$", request => {
            if (request.Session == null)
                request.CreateSession();

            string value = request.Groups[0];
            request.Session.Set("value", value);

            request.SetContentType("text/plain");
            request.Respond($"success: value set");
        }));

        /* 
         *  GET /get
         */
        router.Routes.Add(new Route("GET", null, @"^\/get$", request => {
            request.SetContentType("text/plain");

            if (request.Session != null)
            {
                object value = request.Session.Get("value");

                if (value != null)
                {
                    request.Respond($"value = {value}");
                }
                else
                {
                    request.Respond($"error: value is not set");
                }
            } else
            {
                request.Respond($"error: no session");
            }
        }));
        
        
        /* 
         *  GET /add/(item)
         */
        router.Routes.Add(new Route("GET", null, @"^\/add/([^\/]*)$", request => {
            string item = request.Groups[0];

            if (request.Session == null)
            {
                request.CreateSession()
                    .Set("firstName", "Robert")
                    .Set("lastName", "Robertson")
                    .Set("cartItems", new List<string>());
            }

            List<string> cartItems = request.Session.Get<List<string>>("cartItems");
            cartItems.Add(item);

            JArray array = new JArray(cartItems.ToArray());

            request.SetContentType("application/json");
            request.Respond(array.ToString());
        }));


        /* 
         *  GET /stream
         *  - note that this expects to find file 'test.mp4' 
         *    from the program's directory
         */
        router.Routes.Add(new Route("GET", null, @"^\/stream$", request => {
            request.SetContentType("video/mp4");
            request.StreamMedia("test.mp4");
        }));
        

        /* instantiate new server */
        string[] prefixes = new string[] {
            "http://*:80/",
        };
        Server server = new Server(prefixes, router);

        /* prevent console from exiting */
        Console.ReadLine();
    }
}

sharpwebserver's People

Contributors

heapoverride avatar

Stargazers

 avatar Spuqe avatar

Watchers

 avatar

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.