├── Dockerfile ├── quotes.txt ├── README.md └── src └── Main.java /Dockerfile: -------------------------------------------------------------------------------- 1 | # Use the official OpenJDK 17 image as the base image 2 | FROM openjdk:17-jdk-alpine 3 | 4 | # Set metadata 5 | LABEL maintainer="trainwithshubham@gmail.com" 6 | LABEL version="1.0" 7 | LABEL description="A Java Quotes application" 8 | 9 | # Set the working directory inside the container 10 | WORKDIR /app 11 | 12 | # Copy the source code into the container 13 | COPY src/Main.java /app/Main.java 14 | 15 | COPY quotes.txt quotes.txt 16 | 17 | # Compile the Java code 18 | RUN javac Main.java 19 | 20 | # Expose port 8000 for the HTTP server 21 | EXPOSE 8000 22 | 23 | # Run the Java application when the container starts 24 | CMD ["java", "Main"] 25 | -------------------------------------------------------------------------------- /quotes.txt: -------------------------------------------------------------------------------- 1 | Believe you can and you're halfway there. – Theodore Roosevelt 2 | Don't watch the clock; do what it does. Keep going. – Sam Levenson 3 | Success is not how high you have climbed, but how you make a positive difference in the world. – Roy T. Bennett 4 | Act as if what you do makes a difference. It does. – William James 5 | It does not matter how slowly you go as long as you do not stop. – Confucius 6 | Everything you’ve ever wanted is on the other side of fear. – George Addair 7 | Opportunities don't happen. You create them. – Chris Grosser 8 | The way to get started is to quit talking and begin doing. – Walt Disney 9 | The harder the conflict, the greater the triumph. – George Washington 10 | Do what you can, with what you have, where you are. – Theodore Roosevelt 11 | Keep your face always toward the sunshine—and shadows will fall behind you. – Walt Whitman 12 | Dream big and dare to fail. – Norman Vaughan 13 | It always seems impossible until it's done. – Nelson Mandela 14 | Success is not final, failure is not fatal: It is the courage to continue that counts. – Winston Churchill 15 | The only limit to our realization of tomorrow is our doubts of today. – Franklin D. Roosevelt 16 | If you can dream it, you can do it. – Walt Disney 17 | You are never too old to set another goal or to dream a new dream. – C.S. Lewis 18 | Difficulties in life are intended to make us better, not bitter. – Dan Reeves 19 | Your time is limited, so don’t waste it living someone else’s life. – Steve Jobs 20 | Stay hungry, stay foolish. – Steve Jobs 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java Motivational Quotes App 2 | 3 | This project is a simple Java-based HTTP server that serves random motivational quotes via a REST API. The quotes are externalized to a `quotes.txt` file for easy customization. 4 | 5 | ## Features 6 | - Serves random motivational quotes in JSON format. 7 | - Uses an external `quotes.txt` file for configurable quotes. 8 | - Lightweight HTTP server using `com.sun.net.httpserver.HttpServer`. 9 | - Dockerized for easy deployment. 10 | 11 | ## Requirements 12 | - Java 17+ 13 | - Maven (if building from source) 14 | - Docker (optional, for containerized deployment) 15 | 16 | ## Setup and Usage 17 | 18 | ### Running Locally 19 | 1. Clone the repository: 20 | ```sh 21 | git clone https://github.com/LondheShubham153/java-quotes-app.git 22 | cd java-quotes-app 23 | ``` 24 | 2. Ensure `quotes.txt` exists in the project directory and contains quotes (one per line). 25 | 3. Compile and run the application: 26 | ```sh 27 | javac src/Main.java -d out 28 | java -cp out Main 29 | ``` 30 | 4. The server will start on `http://localhost:8000/`. 31 | 5. Test the API using: 32 | ```sh 33 | curl http://localhost:8000/ 34 | ``` 35 | 36 | ### Running with Docker 37 | 1. Build the Docker image: 38 | ```sh 39 | docker build -t motivational-quotes-api . 40 | ``` 41 | 2. Run the container: 42 | ```sh 43 | docker run -p 8000:8000 motivational-quotes-api 44 | ``` 45 | 3. Access the API at `http://localhost:8000/`. 46 | 47 | ## File Structure 48 | ``` 49 | project-root/ 50 | │── src/ 51 | │ └── Main.java 52 | │── quotes.txt 53 | │── Dockerfile 54 | │── README.md 55 | │── target/ 56 | │ └── myapp.jar (if using Maven build) 57 | ``` 58 | 59 | ## Customizing Quotes 60 | To customize the quotes, edit `quotes.txt` and restart the application. Each quote should be on a new line. 61 | 62 | ## License 63 | This project is licensed under the MIT License. 64 | 65 | ## Author 66 | [TrainWithShubham](https://github.com/LondheShubham153) 67 | 68 | 69 | -------------------------------------------------------------------------------- /src/Main.java: -------------------------------------------------------------------------------- 1 | import com.sun.net.httpserver.HttpServer; 2 | import com.sun.net.httpserver.HttpExchange; 3 | 4 | import java.io.IOException; 5 | import java.io.OutputStream; 6 | import java.io.BufferedReader; 7 | import java.io.FileReader; 8 | import java.net.InetSocketAddress; 9 | import java.nio.charset.StandardCharsets; 10 | import java.util.List; 11 | import java.util.Random; 12 | import java.util.stream.Collectors; 13 | 14 | public class Main { 15 | private static List quotes; 16 | 17 | public static void main(String[] args) throws IOException { 18 | // Load quotes from an external file 19 | quotes = loadQuotesFromFile("quotes.txt"); 20 | 21 | if (quotes.isEmpty()) { 22 | System.err.println("No quotes found in the file. Please ensure 'quotes.txt' has content."); 23 | return; 24 | } 25 | 26 | // Create an HTTP server listening on port 8000 27 | HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0); 28 | 29 | // Define a context for the root path ("/") 30 | server.createContext("/", exchange -> { 31 | // Get a random quote 32 | String quote = getRandomQuote(); 33 | 34 | // Create a JSON response 35 | String jsonResponse = String.format("{\"quote\": \"%s\"}", quote); 36 | 37 | // Convert the JSON response to bytes 38 | byte[] responseBytes = jsonResponse.getBytes(StandardCharsets.UTF_8); 39 | 40 | // Set the response headers 41 | exchange.getResponseHeaders().set("Content-Type", "application/json"); 42 | exchange.sendResponseHeaders(200, responseBytes.length); 43 | 44 | // Write the JSON response to the output stream 45 | try (OutputStream os = exchange.getResponseBody()) { 46 | os.write(responseBytes); 47 | } 48 | }); 49 | 50 | // Start the server 51 | server.start(); 52 | System.out.println("Server is running on port 8000..."); 53 | } 54 | 55 | // Helper method to get a random quote 56 | private static String getRandomQuote() { 57 | Random random = new Random(); 58 | return quotes.get(random.nextInt(quotes.size())); 59 | } 60 | 61 | // Helper method to load quotes from an external file 62 | private static List loadQuotesFromFile(String filename) { 63 | try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { 64 | return reader.lines().collect(Collectors.toList()); 65 | } catch (IOException e) { 66 | System.err.println("Error reading quotes file: " + e.getMessage()); 67 | return List.of(); 68 | } 69 | } 70 | } 71 | 72 | --------------------------------------------------------------------------------