Introduction: Initializing the Game Application

Welcome back! So far, you have learned how to score word guesses using semantic similarity, how to create a leaderboard to make your word prediction game more engaging, and how to set up routes for your game. Now, it’s time to bring everything together by initializing the main application structure. This step is essential because it sets up the core of your web service, connects all the features you’ve built, and prepares your game to run and handle requests from players.

By the end of this lesson, you’ll understand how to set up the main application object, configure it to use a database, and organize your code so that your game can grow with new features in the future.

Creating the Main Application Object

The first step in any web project is to create the main application object. This object is the core of your web app — it handles requests, manages configuration, and ties everything together.

Let’s start by importing the necessary tools and creating the application instance:

In this very simple function:

  • app = FastAPI() creates a new application object. This object will be used to add routes, configure settings, and more.

This app object will be the central place where you connect all your game’s features.

Adding CORS to Our App

To make your FastAPI app accessible from different domains (for example, if your frontend is hosted separately from your backend), you need to enable CORS (Cross-Origin Resource Sharing). This is important for modern web applications where the frontend and backend often run on different servers during development or in production.

Here’s how you add CORS support to your app:

Here’s what these settings mean:

  • allow_origins=["*"] allows requests from any domain. (You can restrict this to specific domains in production.)
  • allow_credentials=True allows cookies and authentication headers to be included in requests.
  • allow_methods=["*"] allows all HTTP methods (GET, POST, etc.).
  • allow_headers=["*"] allows all headers in requests.

Adding this middleware ensures that your app can communicate with frontends or other services running on different origins.

Organizing Routes and Initializing the Database

Now, let’s organize your code and set up the database. In this structure, you use routers to keep your routes organized, especially as your app grows. You also need to initialize the database so it’s ready to store data.

Here’s how you do it:

Let’s break this down:

  • app.include_router(router) registers a set of routes (endpoints) with your application. The router object contains all the route definitions for your game, keeping your code organized and modular.
  • Base.metadata.create_all(bind=engine) creates all the database tables defined by your SQLAlchemy models if they do not already exist. Base is the base class for your models, and engine is the database connection. This ensures your database is ready to store data when the app starts.

This setup keeps your code organized and makes it easy to add new features later.

Running the Application

The final step is to run your application so you can see it in action. Here’s how you do it:

Here’s what’s happening:

  • if __name__ == "__main__": checks if you’re running this file directly (not importing it from somewhere else).
  • app = create_app() creates your application using the function you just built.
  • uvicorn.run(app, host="0.0.0.0", port=3000, reload=True) starts the web server. The app will be available on port 3000, and reload=True helps you see errors and automatically reloads the app when you make changes.

Expected output:

When you run this code, you should see something like:

This means your app is running and ready to handle requests!

Summary and What’s Next

In this lesson, you learned how to initialize your main application object, configure it for database use, organize your routes, and start the server. These steps bring together everything you’ve built so far and set the stage for adding even more features.

Next, you’ll get a chance to practice these steps yourself. You’ll see how each part fits together and get hands-on experience running your own application. This foundation will make it much easier to add new game features and improvements in the future. Keep going — you’re making great progress!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal