GithubHelp home page GithubHelp logo

usox / sharesta Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 1.0 98 KB

Sharesta (Strict HAck RESTful Apis) is a micro framework to build simple and easy to use rest-like applications - written in strict hack.

License: MIT License

Makefile 0.36% Hack 98.05% Shell 1.59%
hhvm hacklang hack rest api restful framework microframework hhvm-site

sharesta's Introduction

Sharesta - Strict HAck RESTful Apis

A micro framework to build simple and easy to use rest-like apis - written in strict hack (Hack).

Usage

First, build some classes containing your logic..

final class HomeRoute implements \JsonSerializable {

    public function jsonSerialize(): string {
        return 'Welcome home';
    }
}

final class UpdateUserRoute implements \JsonSerializable {

    public function __construct(
        private int $user_id,
        private \Usox\Sharesta\RequestInterface $request
    ): void {
    }

    public function jsonSerialize(): bool {
        // do some magic, e.g. access the request body by $this->request
        return true;
    }

}

Now create a class containing your route configurations.

final class Routes implements Usox\Sharesta\RoutesInterface {

    public function registerRoutes(Usox\Sharesta\RouterInterface $router): void {
        $router->get(
            '/',
            (Usox\Sharesta\RequestInterface $request): \JsonSerializable ==> {
                return new HomeRoute();
            }
        );

        /**
         * Get variables from the path (e.g. `http://some.tld/users/123`)
         */
        $router->post(
            '/users/:id',
            (Usox\Sharesta\RequestInterface $request): \JsonSerializable ==> {
                return new UpdateUserRoute(
                    $request->getUriValues('id'),
                    $request->getRequestBody()
                );
            }
        );
    }
}

Setup sharesta, register your routes and let the application controller handle your requests.

<?hh // strict

require_once 'vendor/autoload.php';

<<__Entrypoint>>
function main(): noreturn {
	/* HH_IGNORE_ERROR[2050] */ $get_vars = dict($_GET);
	/* HH_IGNORE_ERROR[2050] */ $server_vars = dict($_SERVER);

    $factory = new Usox\Sharesta\ApiFactory();
    $router = $factory->createRouter(
        $server_vars,
        $get_vars
    );

    $routes = new Routes();
    $routes->registerRoutes($router);

    $router->route(
        '' // path to the file. Leave it empty if your server configuration defaults to index.hh
    );

    die(0);
}

Example

See example/webroot/index.hh for an example.

sharesta's People

Contributors

lexidor avatar usox avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

franciscojbl

sharesta's Issues

Introduce a factory

To simplify the class creation process and to allow more unit tests, a Factory class should be introduced

Methods to retrieve values from RequestBody with specific type

Currently, the only way to retrieve request values from the request body is accessing getBody(): Map<string, mixed> within RequestBody. All values within this Map are mixed, therefor various type-checks within the routes/handlers are necessary to maintain strict typing which results in code duplications.
Maybe it would make sense to provide a bunch of methods which retrieve the values from the Map and cast them automatically, e.g. for scalars and traversables.

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.