Git Product home page Git Product logo

scriptcs-nancy's Introduction

ScriptCs.Nancy

A scriptcs script pack for Nancy.

Continue your journey down the super-duper-happy-path by creating a self-hosted Nancy website with a single line of code:

Require<NancyPack>().Get("/", _ => "Hello world").Host();

Get it on Nuget.

Quickstart (Interactive)

  • Ensure you have scriptcs installed.
  • Open a command prompt as an administrator.
  • Create a folder, e.g. mkdir C:\hellonancy.
  • Navigate to your folder and run scriptcs -install ScriptCs.Nancy.
  • Run scriptcs.
  • Enter Require<NancyPack>().Get("/", _ => "Hi!").Go().Browse();

Congratulations! You've created your first interactively self-hosted website using scriptcs and Nancy! ๐Ÿ†

Quickstart (Script)

  • Ensure you have scriptcs installed.
  • Open a command prompt as an administrator.
  • Create a folder, e.g. mkdir C:\hellonancy.
  • Navigate to your folder and run scriptcs -install ScriptCs.Nancy.
  • Create a file named start.csx containing:
Require<NancyPack>().Get("/", _ => "Hello world").Host();
  • Run scriptcs start.csx.
  • Browse to http://localhost:8888/.

Congratulations! You've created your first scripted self-hosted website using scriptcs and Nancy! ๐Ÿ†

How it works

As well as any routes registered using the above in-line syntax, ScriptCs.Nancy also auto-magically finds all modules in your script/session (including those loaded with #load) and all modules in any referenced assemblies. The names of all modules found are printed in the console window. If any modules have dependencies, they are also auto-magically wired up! ๐Ÿ˜Ž

Take a look at some samples demonstrating these behaviours in the sample folder.

Go() and Stop() start and stop hosting. Host() is a convenience for script writing. It calls Go(), waits for the user to press a key and then calls Stop(). Use it in place of Go() in any examples if you need this behaviour.

By default, the base URL of your site is http://localhost:8888/. You can easily change this to another URL or even multiple URL's (see 'Advanced Usage'). All base URL's being used are printed in the console window.

The most commonly used Nancy namespaces are also imported into your script/session:

  • Nancy
  • Nancy.Bootstrapper
  • Nancy.Conventions
  • Nancy.Cryptography
  • Nancy.ErrorHandling
  • Nancy.Hosting.Self
  • Nancy.ModelBinding
  • Nancy.Security
  • Nancy.Validation

You can import more namespaces with your own using statements. If you think another namespace should be imported by default, please create an issue or send a pull request.

Advanced Usage

Interactive Tips

When using scriptcs interactively, assign a variable to NancyPack to save your wrists:

> var n = Require<NancyPack>();
> n.Get("/", _ => "Hello world");
> n.Go();
> n.Browse();
> #load "hhgtg.csx"
> n.At(42, 54, 66).Go().BrowseAll("vogons/poetry");     // also chain method calls whenever possible

(See below for an explanation of these methods.)

Note that if you call any methods which alter the configuration of the host whist it is already running, you need to call Go() again for the changes to be reflected.

In-line routes

At the top level, ScriptCs.Nancy provides convenience methods for registering routes without having to write a module class. E.g.

var n = Require<NancyPack>();
n.Get("/", _ => "Hello world");
n.Post("/", _ => ... ); // do something else cool

All the common overloads for defining DELETE, GET, OPTIONS, PATCH, POST and PUT including async variants are provided for you.

Dropping down one level, you can get a reference to the RouteBuilder instances themselves:

n.Get(g => g["/"] = _ => "Hello world");

This is useful if you want to call any methods or extension methods on RouteBuilder which are not wrapped by the top level convenience methods.

Dropping down another level, you can get a reference to the default module instance which is used to host in-line routes:

n.Module(m => m.Get["/"] = _ => "Hello world");

This is useful if you want to configure the module in ways that the RouteBuilder does not allow for. The action that you pass to the Module() method will be invoked during the construction of the DefaultModule instance the next time you call NancyPack.Go().

Custom URL's

At(777)                                                 // http://localhost:777
At(777, 888)                                            // http://localhost:777 and http://localhost:888
At("http://localhost/abc/")
At("http://localhost/abc/", "http://localhost/def/")

Interactive browsing

Browse() opens your default browser using the root URL of the host (or the first URL if multiple URLs are being used).

BrowseAll() opens a browser window for each root URL when multiple URLs are being used.

To browse to a specific resource, pass the relative address of the resource as an argument, e.g. Browse("hello") or BrowseAll("hello").

Manually registering dependencies

Nancy's built in auto registration works perfectly well in the scriptcs environment as demonstrated in this sample. If you don't want to take advantage of this feature you can also manually register your dependencies as shown in this sample.

Managing the host yourself

If you don't want to take advantage of the built in hosting in ScriptCs.Nancy, you can also manage the lifetime of the host yourself:

using (var host = new NancyHost(new DefaultNancyPackBootstrapper(), new Uri("http://localhost:8888/")))
{
    host.Start();    
    Console.ReadKey();
}

Note that DefaultNancyBootstrapper is not suited to scriptcs. DefaultNancyPackBootstrapper inherits from DefaultNancyBootstrapper and is specifically designed to work with scriptcs.

HttpListenerException ๐Ÿ˜ฑ

Two common causes:

  1. You do not have permission to host an HTTP listener at the desired URL. This can be solved by running your command prompt as an administrator or following the steps described here.

  2. Another process is already listening at the desired URL. Kill the other process and re-try.

Updates

Releases will be pushed regularly to NuGet. For update notifications, follow @adamralph.

To build manually, clone or fork this repository and see 'How to build'.

Can I help to improve it and/or fix bugs?

Absolutely! Please feel free to raise issues, fork the source code, send pull requests, etc.

No pull request is too small. Even whitespace fixes are appreciated. Before you contribute anything make sure you read CONTRIBUTING.md.

What do the version numbers mean?

ScriptCs.Nancy uses Semantic Versioning. The current release is 0.x which means 'initial development'. Version 1.0 will follow a stable release of scriptcs.

Sponsors

Our build server is kindly provided by CodeBetter and JetBrains.

YouTrack and TeamCity

scriptcs-nancy's People

Contributors

adamralph avatar jchannon 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.