Giter VIP home page Giter VIP logo

shelf_virtual_directory's Introduction

Virtual Directory for Shelf

Pub Version

This package provides Handler, Router and Cascade to serve static files from the dart shelf server and can work with websockets.

Provider UseCase
Handler Can be used to serve as a handler for Pipeline or for Cascade
Router Can be used to mount as a subroute
Cascade Can be used directly serve from shelf server

Setup

  1. Add to pubspec.yaml file

    dependencies:
        shelf_virtual_directory: latest_version
  2. Get dependencies

    pub get
  3. Import

    import 'package:shelf_virtual_directory/shelf_virtual_directory.dart';
  4. Create a instance of ShelfVirtualDirectory with a directory ../web

    final virtualDir = ShelfVirtualDirectory(
         '../web'
         defaultFile:'index.html',
         default404File:'404.html',
         showLogs:true,
     );

    Note: index.html and 404.html files must be directly under root folder. In above case under web/ folder.

Handling different cases

  • Using as a Handler

    final virDirHandler = ShelfVirtualDirectory('../web').handler;
    
    final staticFileHandler = const Pipeline()
        .addMiddleware(logRequests())
        .addHandler(virDirHandler);//used as a handler
    
    io.serve(Cascade().add(staticFileHandler).handler,address,port)
    .then((server){
        print('Server is sunning at ${server.address}:${server.port}'),
    });
  • Using as a Router

    //As a subroute
    final router = Router()
    ..get('/otherroute',otherRoutehandler)
    ..mount('/',ShelfVirtualDirectory('../web').router); // localhost:8080/
    //or
    
    final router = Router()
    ..get('/otherroute',otherRouteHandler)
    ..mount('/home/',ShelfVirtualDirectory('../web').router);//localhost:8080/home/
    //or
    
    final router = Router()
    ..get('/otherroute',otherRouteHandler)
    ..get('/',ShelfVirtualDirectory('../web').handler)//at end

    Note: If your are planning to use it under home directory('/'), always mount or handle the ShelfVirtualDirectory at the end.

    Example

    final mainRoute = Router()
    ..get('/rest',(_)=>Respond.ok('Other routes'))
    ..mount('/',ShelfVirtualDirectory('../web').router);
  • Using as a Cascade

    import 'package:shelf/shelf_io.dart' as io show serve;
    
    // You can add other handlers to this cascade
    final virDirCascade = ShelfVirtualDirectory('web').cascade;
    io.serve(virDirCascade.add(someOtherHandler),'localhost',8080)
    .then((server){
      print('Server is sunning at ${server.address}:${server.port}'),
    })

How it works?

It adds all the files under the given root directory as a route to a Router instance.

Example

web/
    - index.html
    - 404.html
    - js/
        - index.js
    - style/
        - index.css

All this will turn in to.

GET     /index.html
GET     /404.html
GET     /js/index.js
GET     /style/index.css

If user try to access other routes it will serve 404.html.

Contrubitions

All contrubitions are welcomed.

shelf_virtual_directory's People

Contributors

yeungkc 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.