Backend Server

 import logging

from fastapi import FastAPI
from fastapi.responses import RedirectResponse

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI()


@app.get("/", include_in_schema=False)
def root():
    return RedirectResponse(url="/docs")


@app.get("/list")
def list_entrypoint():
    some_list = ["data1", "data2"]
    return some_list


# Do not remove the main function while updating the app.
if __name__ == "__main__":

About this template

This skeleton is streamlined for creating backend services using FastAPI. It's an excellent choice for building microservices or APIs with minimal frontend requirements.

Introduction to the Backend Server Template

Welcome to the step-by-step guide on how to use the Backend Server Template on Lazy. This template is designed to help you create a backend service using FastAPI, which is perfect for building microservices or APIs with minimal frontend requirements. By the end of this article, you will understand how to use this template to set up your backend server and integrate it with other services or frontends if necessary.

Clicking Start with this Template

To begin using this template, simply click on the "Start with this Template" button on the Lazy platform. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy, paste, or delete any code manually.

Test: Pressing the Test Button

Once you have started with the template, the next step is to press the "Test" button. This will initiate the deployment of your app and launch the Lazy CLI. The deployment process is handled entirely by Lazy, so you don't need to worry about installing libraries or setting up your environment.

Using the App

After pressing the "Test" button and the deployment is complete, Lazy will provide you with a dedicated server link to use your API. Since this template uses FastAPI, you will also receive a link to the autogenerated documentation at the "/docs" endpoint, which you can use to interact with your API.

Here's how you can use the different endpoints provided by the template:

  • The root endpoint ("/") will redirect you to the autogenerated documentation.
  • The "/list" endpoint will return a list of data when accessed via a GET request.

For example, to use the "/list" endpoint, you would send a GET request to:


http://your-server-link/list

And you would receive a response like:


["data1", "data2"]

Integrating the App

If you need to integrate this backend server with a frontend or another service, you will use the server link provided by Lazy. For instance, you can make HTTP requests from your frontend application to the endpoints defined in the FastAPI app to retrieve or send data.

If you need to handle POST requests, you can integrate the provided "handle_post_endpoint" function from the "some_post_request_handler.py" file. Here's a sample code snippet that you could use in your frontend to send a POST request:


fetch('http://your-server-link/endpoint', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ field: 'value' }),
})
.then(response => response.json())
.then(data => console.log(data));

Replace 'http://your-server-link/endpoint' with the actual endpoint URL provided by Lazy and 'field' with the actual data field you want to send.

Remember, this template is just the starting point. You can customize the code to add more endpoints, integrate with databases, or implement additional logic as needed for your application.

By following these steps, you should now have a functional backend server ready to be integrated with your desired services or frontends. Happy building!

Category
Technology
Last published
July 20, 2024

More templates like this

MP3ify: Youtube to MP3 Converter

A web application that allows users to download YouTube videos from URLs and provides the option to convert them to MP3 format.

Python
Flask

Discord Moderation Bot

The Discord bot monitors all messages in the server. If a message contains profanity, the bot deletes it and sends a warning to the channel. The bot also notifies the host about the deletion via a direct message. Additionally, the bot outputs a helpful error message to a channel if there is a permissions error, guiding the server admin to enable the required permission in the Discord Developer portal.

Discord
Python
Flask

CLI App

Not all apps require sophisticated code and many only require some interaction from inputs and run similarly to the interactions that would exist in a shell terminal. This powerful app skeleton can be suitable for these apps. Examples: * app that does some processing and just prints results or sends it to some server or storage. * app that does some data crunching, either from user input or from external sources and then posts something on other platforms. * app that transforms the data in some way and outputs it using some libs.

Python
Flask
Home
/
Backend Server