https://github.com/usox/sharesta
Sharesta (Strict HAck RESTful Apis) is a micro framework to build simple and easy to use rest-like applications - written in strict hack.
https://github.com/usox/sharesta
api framework hack hacklang hhvm hhvm-site microframework rest restful
Last synced: 6 months ago
JSON representation
Sharesta (Strict HAck RESTful Apis) is a micro framework to build simple and easy to use rest-like applications - written in strict hack.
- Host: GitHub
- URL: https://github.com/usox/sharesta
- Owner: usox
- License: mit
- Created: 2016-08-07T15:07:33.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2020-05-15T06:00:37.000Z (over 5 years ago)
- Last Synced: 2025-02-08T05:44:44.809Z (8 months ago)
- Topics: api, framework, hack, hacklang, hhvm, hhvm-site, microframework, rest, restful
- Language: Hack
- Homepage:
- Size: 95.7 KB
- Stars: 5
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Sharesta - Strict HAck RESTful Apis
A micro framework to build simple and easy to use rest-like apis - written in
strict hack ([Hack](http://hacklang.org)).## Usage
First, build some classes containing your logic..
```php
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.
```php
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.
```php
>
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.