4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | React App
28 |
29 |
30 |
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/api/main.py:
--------------------------------------------------------------------------------
1 | from fastapi import FastAPI, HTTPException, Query
2 | from fastapi.middleware.cors import CORSMiddleware
3 | from azure.cosmos import CosmosClient
4 | from dotenv import load_dotenv
5 | import os
6 | from typing import Optional
7 |
8 | # Load environment variables from .env file
9 | load_dotenv()
10 |
11 | app = FastAPI()
12 |
13 |
14 | # Enable CORS
15 | app.add_middleware(
16 | CORSMiddleware,
17 | allow_origins=["http://localhost:3000"], # React app's URL
18 | allow_credentials=True,
19 | allow_methods=["*"],
20 | allow_headers=["*"],
21 | )
22 |
23 | # Initialize the Cosmos DB client using environment variables
24 | endpoint = os.getenv("CosmosDBEndpoint")
25 | key = os.getenv("CosmosDBKey")
26 |
27 | if not endpoint or not key:
28 | raise ValueError("CosmosDBEndpoint and CosmosDBKey must be set in the .env file")
29 |
30 | client = CosmosClient(endpoint, key)
31 |
32 | # Get a reference to the database and container
33 | database = client.get_database_client("TvShows")
34 | container = database.get_container_client("TvShowsContainer")
35 |
36 | @app.get("/")
37 | async def root():
38 | return {"message": "Welcome to the TV Shows API!"}
39 |
40 | @app.get("/api/shows")
41 | async def get_tv_shows():
42 | try:
43 | query = "SELECT c.id, c.title FROM c"
44 | items = list(container.query_items(query=query, enable_cross_partition_query=True))
45 | return items
46 | except Exception as e:
47 | raise HTTPException(status_code=500, detail="Error accessing Cosmos DB")
48 |
49 | @app.get("/api/seasons")
50 | async def get_seasons(show_id: Optional[str] = Query(None, title="Show ID")):
51 | if not show_id:
52 | raise HTTPException(status_code=400, detail="Please provide a show_id query parameter. If unaware of the showId, check out the /api/shows endpoint.")
53 |
54 | try:
55 | query = "SELECT c.seasons FROM c WHERE c.id = @showId"
56 | parameters = [{"name": "@showId", "value": show_id}]
57 |
58 | items = list(container.query_items(
59 | query=query,
60 | parameters=parameters,
61 | enable_cross_partition_query=True
62 | ))
63 |
64 | if not items:
65 | return {"message": "No seasons found for the given show ID"}
66 |
67 | return items[0]
68 | except Exception as e:
69 | raise HTTPException(status_code=500, detail=f"Error querying Cosmos DB: {str(e)}")
--------------------------------------------------------------------------------
/frontend/src/logo.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # LTC DevOps Project
2 |
3 | This is the sample application for the DevOps Capstone Project on [Learn to Cloud.](https://learntocloud.guide)
4 | It shows your favorite TV Shows or movies, the front-end is in React and the API is written in Python using FastAPI.
5 |
6 | ## Application
7 |
8 | **Front-End** - A web application where users can view.
9 | 
10 |
11 | **API**: API that receives request to fetch TV Shows or Movies data, stored in NoSQL Database. For this sample app I am using CosmosDB from Azure. But you can modify the code according to your provider within the API.
12 | 
13 |
14 | ## Running locally
15 |
16 | ### API
17 |
18 | The API code exists in the `api` directory. You can run the API server locally:
19 |
20 | - Clone this repo
21 | - Make sure you are in the `api` directory
22 | - Create a virtualenv by typing in the following command: `python -m venv .venv`
23 | - Install the required packages: `pip install -r requirements.txt`
24 | - Create a `.env` file, and add your CosmosDB connection details(only if you are using Azure CosmosDB), check `.env.example`
25 | - Also, change the API code depending on the data stored in your NoSQL Database in `main.py` accordingly
26 | - Run the API server: `uvicorn main:app --reload`
27 | - Your API Server should be running on port `http://localhost:8000`
28 |
29 | ### Front-end
30 |
31 | The front-end code exits in the `frontend` directory. You can run the front-end server locally:
32 |
33 | - Clone this repo
34 | - Make sure you are in the `frontend` directory
35 | - Install the dependencies: `npm install`
36 | - Run the NextJS Server: `npm start`
37 | - Your Front-end Server should be running on `http://localhost:3000`
38 |
39 | ## Goal
40 |
41 | The goal is to get hands-on with DevOps practices like Containerization, CICD and monitoring.
42 |
43 | - Containerize the Application: Create Dockerfiles for both api and frontend.
44 | - CI/CD: Setup CI/CD pipeline to push images to Dockerhub or other container registries like [ACR](https://azure.microsoft.com/en-us/products/container-registry) or [ECR](https://aws.amazon.com/ecr/).
45 | - Infrastructure as Code: Deploy a Kubernetes cluster to your choice of cloud provider, using Terraform.
46 | - Kubernetes: Create deployments for both of your containers using YAML files, and deploy it to your cluster.
47 | - Monitoring: Setup monitoring for your cluster, look into Prometheus and Grafana.
48 |
49 | Look at the [Phase 4 capstone project](https://learntocloud.guide/phase4/) for more details.
50 |
51 | ## Author
52 |
53 | [Rishab Kumar](https://github.com/rishabkumar7)
54 |
55 | ## License
56 |
57 | [MIT](./LICENSE)
58 |
--------------------------------------------------------------------------------
/frontend/README.md:
--------------------------------------------------------------------------------
1 | # Getting Started with Create React App
2 |
3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4 |
5 | ## Available Scripts
6 |
7 | In the project directory, you can run:
8 |
9 | ### `npm start`
10 |
11 | Runs the app in the development mode.\
12 | Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
13 |
14 | The page will reload when you make changes.\
15 | You may also see any lint errors in the console.
16 |
17 | ### `npm test`
18 |
19 | Launches the test runner in the interactive watch mode.\
20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21 |
22 | ### `npm run build`
23 |
24 | Builds the app for production to the `build` folder.\
25 | It correctly bundles React in production mode and optimizes the build for the best performance.
26 |
27 | The build is minified and the filenames include the hashes.\
28 | Your app is ready to be deployed!
29 |
30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31 |
32 | ### `npm run eject`
33 |
34 | **Note: this is a one-way operation. Once you `eject`, you can't go back!**
35 |
36 | If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37 |
38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
39 |
40 | You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
41 |
42 | ## Learn More
43 |
44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45 |
46 | To learn React, check out the [React documentation](https://reactjs.org/).
47 |
48 | ### Code Splitting
49 |
50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51 |
52 | ### Analyzing the Bundle Size
53 |
54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55 |
56 | ### Making a Progressive Web App
57 |
58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59 |
60 | ### Advanced Configuration
61 |
62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63 |
64 | ### Deployment
65 |
66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67 |
68 | ### `npm run build` fails to minify
69 |
70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
71 |
--------------------------------------------------------------------------------
/frontend/src/App.js:
--------------------------------------------------------------------------------
1 | import React, { useState, useEffect } from 'react';
2 | import axios from 'axios';
3 | import styles from './App.module.css';
4 |
5 | const API_BASE_URL = 'http://localhost:8000/api';
6 |
7 | const TvShowList = ({ onSelectShow }) => {
8 | const [shows, setShows] = useState([]);
9 |
10 | useEffect(() => {
11 | const fetchShows = async () => {
12 | try {
13 | const response = await axios.get(`${API_BASE_URL}/shows`);
14 | setShows(response.data);
15 | } catch (error) {
16 | console.error('Error fetching TV shows:', error);
17 | }
18 | };
19 |
20 | fetchShows();
21 | }, []);
22 |
23 | return (
24 |