Chapter 1: Hello Starberry!

Installation:

You can install starberry by using the following commandL

cargo install starberry 

Once it is installed, you can run the following command to create a new starberry project:

starberry new HelloStarberry 

This will automatically creates a folder called HelloStarberry. A minimal starberry project will be generated

The simpliest project

Go to src/main.rs, let’s dig dive into each line of code and see what they do.

use starberry::prelude::*;

The first line of code shown above imports everything from starberry::prelude, this is basically everything you need for a standard starberry project

#[tokio::main]
async fn main() {
    APP.clone().run().await;
} 

In the tokio environment (note you don’t need to import tokio because starberry did that for you automatically), you clone the App instance called APP and run it.

Note that you must clone the instance before running it since APP is a global instance wrapped with Lazy.

pub static APP: SApp = once_cell::sync::Lazy::new(|| {
    App::new().build()
}); 

This defines a Static App variable by using lazy. Note that you also don’t need to import Lazy since starberry did this automatically.

#[url(APP.lit_url("/"))] 

APP.lit_url('/') meaning that generate a Url instance from APP where the url is “/”. By passing this into the #[url()] macro this links the function below with this url instance.

async fn home_route() -> HttpResponse {
    text_response("Hello from Starberry!")
} 

This function returns a text HttpResponse which is Hello from Starberry!

After compiling the app and run it, you may visit localhost:3003 (the default port for starberry), it will returns “Hello from Starberry!”