Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 4, 2022 03:51 pm GMT

Cruky library first README

Info

Cruky is a server-side library for the dart ecosystem to help you create your API as fast as possible. We want to make server-side apps with modern style and fast high performance.

The main reason why I built this is that all libraries are focused on the Flutter ecosystem and not on dart lang
and this makes the library have fewer futures than other frameworks or libraries like (Django, FastAPI, Express, ..etc)
So I decided that I will make a new library that focuses on Dart and get the maximum performance using dart:mirrors and code generators together to get the best usage of the dart.

Inspired by Pythod server-side frameworks (Django, Flask, FastAPI)

Get started

You can see the todo example in the examples file it's very clear to understand.

  1. Install Dart from Dart.dev

  2. Install the Cruky package with dart pub global activate cruky_cli

  3. Create dart project with dart create nameOfProject

  4. open the project with your favorite IDE like vscode

  5. And let's get started

Start adding the entrypoint app

import 'package:cruky/cruky.dart';class MyApp extends AppMaterial {  @override  List get routes => [        exampleWithGETRequest,      ];}

Now let's add our first route method:

@Route.get('/')exampleWithGetRequest(ReqCTX req) {  return Json({});}

Add the Route annotation to specify the route path, and add the method under it we can use the Future method or regular method (async or sync).

Return data from the method

You can return a List or map for now and the response content type is just JSON for now.

Return specific status code

you can return the specific status code with the map like that:

@Route.get('/')exampleWithGetRequest(ReqCTX req) {  return Json({}, 201);}

Now serve the app

we can serve a simple app with this code

void main() => run(MyApp());

and now run the dart file with cruky run filename.

This will run the file in ./bin/filename.dart with hot-reload.

You can run with another folder with cruky run filename -d example

This will run the file in ./example/filename.dart

Now Cruky will run the app with hot-reload if any thing changed in lib folder.

Let's add some middleware

We can add a before and after middleware.
The before middleware runs before calling the main route method handler,
And the after middleware is the opposite.

@BeforeMW()middlewareExample(ReqCTX req) {  if (req.headerValue('Token') == null) {    return Text('Not Auth', 401);  }}

The MW is the short of MiddleWare.
The annotiation defines the type of middleware, There is two types BeforeMW amd AfterMW.

If you want to not execute the next function you can (The main route method) you can return a response like in the example.

Now we will add this middleware to global middlewares in the app and any route under it well have the same middleware.

class MyApp extends AppMaterial {  @override  List get routes => [        exampleWithGETRequest,      ];  @override  List get middlewares => [middlewareExample]; /// add this}

Or you can add the middleware scoped for a route like this:

@Route.get('/', [middlewareExample])exampleWithGetRequest(ReqCTX req) {  return Json({}, 201);}

Original Link: https://dev.to/seifalmotaz/cruky-library-first-readme-1ja2

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To