Welcome to the next step in our journey of building a chatbot service with Fiber. In the previous lesson, we focused on the ChatController
, which manages chat sessions and handles messages by interacting with both the model and service layers. Now, we will take a significant step forward by creating a RESTful API for our chatbot service using Fiber. We'll start by setting up the main Fiber application, then adapt the ChatController
to integrate with Fiber's session management.
RESTful APIs are a way for different software systems to talk to each other over the internet. Think of them as a set of rules that allow programs to communicate and share data. Fiber is a web framework that helps you create web applications quickly and efficiently.
To get started with Fiber, you can install it using Go modules by running the following command in your terminal:
Now, we can use it to connect the components we've already built, allowing users to interact with our chatbot service through a web interface. This will enable seamless communication between users and our chatbot service.
First, we need to initialize the Fiber application, load environment variables, and set a secret key for session management.
Here's a breakdown of the steps involved in this initialization process:
-
Initialize the Fiber Application: We start by importing the necessary modules, including Fiber itself. The
fiber.New()
function is then called to create our application object,app
. This object will be used to configure and run our web application. -
Initialize Session Middleware: We use Fiber's session middleware to manage user sessions. The
session.New()
function initializes a new session store, which will be used to handle session data securely. -
Create an Instance of the Controller: Finally, we create an instance of our
ChatController
. This controller will handle the core operations of our chatbot service, such as managing chat sessions and processing messages.
Next, we define the routes for our chatbot service. These routes will handle requests for creating a new chat session and sending messages.
Here's how each route functions within our chatbot service:
-
Index Route (
/
): This route acts as the entry point to our chatbot service. When a user accesses this route, it ensures that a user session is established by checking the session store. If no session exists, a new UUID is generated and stored. After setting up the session, it returns a welcome message to the user, indicating that the service is ready for interaction. -
Create Chat Route (
/api/create_chat
): This route is designed to initiate a new chat session. It listens forPOST
requests and delegates the task of creating a chat session to theChatController
by calling itsCreateChat
method. The controller handles the session creation and returns a response, which is then sent back to the client. -
Send Message Route (
/api/send_message
): This route facilitates sending messages within an existing chat session. It also listens forPOST
requests and delegates the message processing to theChatController
by invoking itsSendMessage
method. The controller processes the message and returns the AI's response, which is then delivered to the client. We will link the defined routers to endpoints in our main method comming up next.
Finally, we set up the Fiber application to run on a specified port when executed directly.
The app.Listen(":3000")
method is called to start the Fiber server, specifying the port on which the server will listen for incoming requests. This allows users to access the application via http://localhost:3000
.
To integrate the ChatController
with Fiber, we need to update it to use Fiber's session management. We start by importing necessary Fiber modules and updating the constructor to remove the test session.
Next, let's break down how to update the ChatController
methods to utilize Fiber's session management.
In the Index
method, we update the way user sessions are managed by leveraging Fiber's built-in session middleware. We check if a user_id
exists in the session. If it doesn't, we generate a new UUID and store it in the session.
This change allows us to utilize Fiber's session management capabilities, providing a more robust and scalable solution for maintaining user sessions.
In the CreateChat
method, we update the code to now retrieve the user_id
directly from the Fiber session, ensuring that the request is associated with a valid user session.
If the session is expired, it returns an error response. Otherwise, it creates a new chat session using the ChatService
and returns a JSON response with the chat ID and a success message.
The SendMessage
method is updated to handle requests using Fiber's Ctx
and session objects. We now extract chat_id
and user_message
from the incoming request using the BodyParser
method.
If any required data is missing, the method returns an error response. The message is processed using the ChatService
, and the AI's response is returned as a JSON object. We've also implemented error handling to provide appropriate JSON responses for different scenarios, ensuring that all interactions with the API are consistent and informative.
To interact with our chatbot service, a client can follow these steps to send a message using the API:
-
Access the Index Route (
/
): The client begins by accessing the index route of the API. This step ensures that a user session is established. A user session is a way to keep track of the user's interactions with the service. It is stored using Fiber's session management, which utilizes cookies to maintain session data on the client side. The server responds with a welcome message, indicating that the service is ready for interaction. -
Create a Chat Session (
/api/create_chat
): Before sending a message, the client needs to create a new chat session. This is done by sending a request to the/api/create_chat
route. The server will respond with a unique chat ID, which is essential for identifying the chat session in subsequent interactions. The user session ensures that the chat session is associated with the correct user, allowing for personalized interactions. -
Send a Message (
/api/send_message
): With the chat session established, the client can now send a message to the chatbot. This involves sending a request to the/api/send_message
route, including the chat ID and the message content. The server processes the message and responds with the AI's reply, allowing the client to continue the conversation. The user session helps maintain continuity in the conversation by linking the messages to the correct user and chat session.
By following these steps, a client can effectively communicate with the chatbot service, leveraging the RESTful API to manage chat sessions and exchange messages while utilizing user sessions for a seamless experience.
In this lesson, we successfully built a RESTful API for our chatbot service using Fiber. We set up a Fiber application, defined routes for chat operations, and updated the ChatController
to utilize Fiber's session management. This lesson marks a significant milestone in our course, as it brings together all the components we've developed so far into a functional web application.
As you move on to the practice exercises, take the opportunity to experiment with the Fiber API and reinforce the concepts covered in this lesson. This hands-on practice will prepare you for further development and exploration of additional Fiber features and RESTful API concepts. Keep up the great work, and I look forward to seeing your progress!
