0 purchases
dartion
DARTION
A RESTful mini web server based on JSON.
This is not just a port of the popular json-server for Dart, it also adds some more features like JWT Authentication.
Explore the docs »
·
Report Bug
·
Request Feature
Table of Contents
About The Project
Sponsors
Getting Started
How to Use
Features
Contributing
Contact
Acknowledgements
About The Project #
Get your backend up in 5 seconds!
Dartion aims to make it easier for those who take front-end video lessons in languages like Flutter and need a server to implement and test their codes.
Just get the DartSDK, install Dartion, initialize the server and start it, that's all!
It comes with support to HTTP requests and JWT Authentication.
After installing, just populate the db.json file and you will have a simple and ready to use database.
This project is distributed under the MIT License. See LICENSE.txt for more information.
(back to top)
Sponsors #
(back to top)
Getting Started #
Get the Dart SDK
Activate Dartion using pub:
dart pub global activate dartion
copied to clipboard
(back to top)
How to Use #
Commands #
Upgrade:
Updates Dartion's version:
dartion upgrade
copied to clipboard
Init server:
Execute the command below in an empty folder, it will create the base configuration files for your server:
dartion init
copied to clipboard
Start server:
The command below will boot the server based on the settings configured in the config.yaml you have now on your created folder.
dartion serve
copied to clipboard
If you want to change those settings, you can edit the config.yaml as you like. Refer to the documentation on the init and serve commands, and also the Database class, to know a little bit more about these configurations.
Route system #
When running Dartion we have a structure based on RESTful, while the data persists in a JSON file in the folder, named by default db.json.
GET /products -> Get all products
GET /products/1 -> Get one product
POST /products -> Add more one product
PUT /products/1 -> Edit one product
PATCH /products/1 -> Edit one product
DELETE /products/1 -> Delete one product
copied to clipboard
POST, PUT and PATH requests must have their body as JSON. It is not necessary to pass the ID, it is incremented automatically.
File Upload #
You can configure Dartion to accept upload files such as images, pdf's, etc...
Adds storage properties to config.yaml
storage:
name: "file"
folder: storage/
copied to clipboard
File uploads work with "Multipart-form", so you can use the name property to name your upload.
We can choose which folder the uploaded files will be on the server using the folder property.
Then you will have two reserved routes, one to upload files and the other to retrieve those binaries.
POST /storage -> Send files in Multipart-form
GET /file/:your-file.ext -> Retrieve file
copied to clipboard
NOTE: The /storage route returns the file name.
Authetication #
You can use JWT Authentication in two steps.
Use the auth property in your config.yaml
name: Test
port: 3031
db: db.json
statics: public
auth:
key: dajdi3cdj8jw40jv89cj4uybfg9wh9vcnvb
exp: 3600
escape:
- animals
- cities
copied to clipboard
That's enough to protect your routes.
The auth property takes some configuration parameters:
key -> To sign your token
exp -> Token expiration time in seconds
escape -> List of routes that will not be affected by token protection
copied to clipboard
Login using the /auth route:
To retrieve the token you need a credential.
A credential is basically base64(email:password)
See an example in Dart:
String email = "[email protected]";
String password = "123";
String info = "$email:$password";
String encode = base64Encode(info.codeUnits);
String credencials = "Basic $encode";
copied to clipboard
You can now make a GET request to /auth, passing the credentials in the authorization header.
exemple in dart
// Using the package http
Response response = await http.get(
'http://localhost:3031/auth',
headers: {'authorization': credencials},
);
copied to clipboard
This will return the token and some user information.
{
"user": {
"name": "Jose",
"email": "[email protected]"
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODc5NjQ1MTAsImlhdCI6MTU4Nzk2MDkxMCwiaXNzIjoiZGFydGlvIiwic3ViIjoibnVsbCJ9.5AeEIpYeu04fKINg6e8Ic5fpT0-KyZH8yPLOO6HoLVA",
"exp": 3600
}
copied to clipboard
That's it! Now, just use the token to access the routes:
Response response = await http.get(
'http://localhost:3031/products',
headers: {'authorization': "Bearer $token"},
);
copied to clipboard
NOTE:
When using Authentication, you will need to have a users property in your db.json with a user list containing at least email and password in order to access.
NOTE 2:
The db.json on the root folder of this package was generated by running the tests contained on the test folder.
For more details, please refer to the Documentation
(back to top)
Features #
✅ Easy to install and use backend mock server
✅ Database personalization through db.json
✅ HTTP request handlers
✅ AuthService mock
✅
Right now this package has concluded all his intended features. If you have any suggestions or find something to report, see below how to contribute to it.
(back to top)
Contributing #
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the appropriate tag.
Don't forget to give the project a star! Thanks again!
Fork the Project
Create your Feature Branch (git checkout -b feature/AmazingFeature)
Commit your Changes (git commit -m 'Add some AmazingFeature')
Push to the Branch (git push origin feature/AmazingFeature)
Open a Pull Request
Remember to include a tag, and to follow Conventional Commits and Semantic Versioning when uploading your commit and/or creating the issue.
(back to top)
Contact #
Flutterando Community
Discord
Telegram
Website
Youtube Channel
Other useful links
(back to top)
Acknowledgements #
Thank you to all the people who contributed to this project, whitout you this project would not be here today.
(back to top)
Maintaned by #
Built and maintained by Flutterando.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.