GithubHelp home page GithubHelp logo

blog-service's Introduction

Blog Service

This is a Spring Boot application that provides a RESTful API for uploading and retrieving blog posts. Blog posts are stored in a MongoDB database. The application allows uploading blog posts in Markdown format, which are then stored with additional metadata.

Features

  • Upload a blog post with metadata and Markdown content
  • Retrieve blog posts by title

Technologies Used

  • Spring Boot
  • Spring Data MongoDB
  • MongoDB
  • Maven

Getting Started

Prerequisites

  • Java 17 or higher
  • Maven 3.6.0 or higher
  • MongoDB

Installation

  1. Clone the repository:

    git clone https://github.com/salah-mo/blog-service.git
    cd blog-service
  2. Configure MongoDB connection in src/main/resources/application.properties:

    spring.data.mongodb.uri=mongodb://localhost:27017/blogdb
  3. Build the project using Maven:

    mvn clean install
  4. Run the application:

    mvn spring-boot:run

API Endpoints

Upload a Blog Post

  • URL: /api/blogs/upload

  • Method: POST

  • Content-Type: multipart/form-data

  • Parameters:

    • file (required): The Markdown file of the blog post.
    • title (required): The title of the blog post.
    • summary (required): A short summary of the blog post.
    • image (required): The filename or URL of the blog post image.
    • author (required): The ID of the author.
    • keywords (required): Comma-separated keywords associated with the blog post.
  • Example:

    curl -F "file=@path/to/sample_blog.md" -F "title=Sample Blog Post" -F "summary=This is a sample blog post" -F "image=112.jpg" -F "author=1234567890abcdef" -F "keywords=sample,dummy,data" http://localhost:8080/api/blogs/upload

Get Blogs by Title

  • URL: /api/blogs/title/{title}

  • Method: GET

  • Parameters:

    • title (required): The title of the blog post.
  • Example:

    curl -X GET "http://localhost:8080/api/blogs/title/Sample%20Blog%20Post"

Database Initialization

To initialize the MongoDB database, follow these steps:

  1. Ensure MongoDB is running on your machine. By default, it runs on mongodb://localhost:27017.

  2. Create the initialization script initBlogDB.js with the following content:

    // Connect to the MongoDB instance
    const conn = new Mongo();
    const db = conn.getDB("blogdb");
    
    // Create an initial collection and insert a sample document
    db.blogs.insertOne({
        title: "Sample Blog Post",
        image: "sample.jpg",
        summary: "This is a sample blog post",
        content: [
            { type: "text", data: "Lorem ipsum dolor sit amet" },
            { type: "image", data: "image.jpg" }
        ],
        author: ObjectId("1234567890abcdef12345678"),
        createdAt: new Date(),
        keywords: ["sample", "dummy", "data"],
        enabled: true
    });
    
    // Create indexes for the blogs collection
    db.blogs.createIndex({ title: "text" });
    db.blogs.createIndex({ author: 1 });
    db.blogs.createIndex({ createdAt: -1 });
    
    // Print a success message
    print("Database and collection initialized successfully.");
  3. Run the script using the mongo shell:

    mongo <path_to_initBlogDB.scripts.js>

    Replace <path_to_initBlogDB.js> with the actual path to your initBlogDB.js file. For example, if the script is in your current directory, you can use:

    mongo initBlogDB.scripts.js
  4. Verify the database initialization by connecting to MongoDB and checking the contents:

    mongo
    use blogdb
    show collections
    db.blogs.find().pretty()

By following these instructions, you will have your MongoDB database initialized and ready for use with the Blog Service application.

blog-service's People

Contributors

salah-mo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.