dartion 1.2.0 dartion: ^1.2.0 copied to clipboard
Dartion is a RESTful mini web server based on JSON. Up your backend in 5 Seconds!
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 #
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.
Sponsors #
Getting Started #
-
Get the Dart SDK
-
Activate Dartion using pub:
dart pub global activate dartion
How to Use #
Commands #
Upgrade:
Updates Dartion's version:
dartion upgrade
Init server:
Execute the command below in an empty folder, it will create the base configuration files for your server:
dartion init
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
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
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/
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
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
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
- 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 = "jose@gmail.com";
String password = "123";
String info = "$email:$password";
String encode = base64Encode(info.codeUnits);
String credencials = "Basic $encode";
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},
);
This will return the token and some user information.
{
"user": {
"name": "Jose",
"email": "jose@gmail.com"
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODc5NjQ1MTAsImlhdCI6MTU4Nzk2MDkxMCwiaXNzIjoiZGFydGlvIiwic3ViIjoibnVsbCJ9.5AeEIpYeu04fKINg6e8Ic5fpT0-KyZH8yPLOO6HoLVA",
"exp": 3600
}
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"},
);
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
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.
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.
Contact #
Flutterando Community
Acknowledgements #
Thank you to all the people who contributed to this project, whitout you this project would not be here today.
Maintaned by #
Built and maintained by Flutterando.