Skip to content

avinashkumar18/Rest-Api

Repository files navigation

Kaiburr-Assessment

Task 1. JAVA Rest API Example:

I have created a sample JAVA Rest Api application using Spring Boot v2.6.6. All the required code are uploaded in this git repository: master branch. Created Server Pojo with values id, name, language and framework. Also created Rest endpoints,

  1. @PostMapping("/addServer") Add new server
  2. @GetMapping("/servers") Viewing all servers
  3. @GetMapping("/servers/{id}") Find server by id
  4. @GetMapping("/server/{name}") Find server by name
  5. @DeleteMapping("/delete/{id}") Delete existing server by id For this project, I created spring boot application with web dependency. After creating pojo, a repository is needed to store all the values. For this MongoDB is chosen to store the values of the server. To work with MongoDb, First we need to add its maven dependency,
org.springframework.boot spring-boot-starter-data-mongodb After this, added few configurations like MongoDb connection uri, database name are configured in application.properties file. ServerRepo interface is created which extends MongoRepository interface with an unimplemented method findByName as a part of requirement. Created a collection “details” in a database “server”. This database is present in Mongodb shared cluster hosted by MongoDB itself in Mongodb Atlas platform. Using the @Document annotation, mentioned the collection name in the pojo. Added @EnableMongoRepositories annotation to the main class of the project. In the ServerController java file, all the logic of rest endpoints has been added. The methods are, 1. public String saveServer(@RequestBody ServerDto dto) 2. public List getAllServers() 3. public Optional getServerById(@PathVariable String id) 4. public List getServerByName(@PathVariable String name) 5. public String DeleteServerById(@PathVariable String id)

All the above endpoints are published into cloud using Heroku platform. Below are the url and the sereenshot of each endpoint. View All Servers: https://kaiburr-assessment.herokuapp.com/servers image

Find server by id https://kaiburr-assessment.herokuapp.com/servers/625afd2ee1142879597471e2

image

Find server by name https://kaiburr-assessment.herokuapp.com/server/centos

image

Add new server https://kaiburr-assessment.herokuapp.com/addServer Body: { "name": "Ubuntu", "language": "Java", "framework": "Hibernate" } image

Delete existing server https://kaiburr-assessment.herokuapp.com/delete/625c59da06d65c37b2470070

image

Task 2. Swagger Codegen:

I have tried to add the swagger for the above application by adding the following maven dependency,

	<dependency>
			<groupId>org.springdoc</groupId>
			<artifactId>springdoc-openapi-ui</artifactId>
			<version>1.6.7</version>
	</dependency>

I made this swagger exposed in different port instead of port running the application. To make this possible, I added actuator maven dependency and few configuration in application.properties file,

	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency> 

springdoc.use-management-port=true management.server.port=8090 management.endpoints.web.exposure.include=openapi, swaggerui Now, api-docs and swaggerui are exposed on the port 8090.

{ "openapi": "3.0.1", "info": { "title": "OpenAPI definition", "version": "v0" }, "servers": [ { "url": "http://localhost:8080", "description": "Generated server url" } ], "paths": { "/addServer": { "post": { "tags": [ "server-controller" ], "operationId": "saveServer", "requestBody": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ServerDto" } } }, "required": true }, "responses": { "200": { "description": "OK", "content": { "/": { "schema": { "type": "string" } } } } } } }, "/servers": { "get": { "tags": [ "server-controller" ], "operationId": "getAllServers", "responses": { "200": { "description": "OK", "content": { "/": { "schema": { "type": "array", "items": { "$ref": "#/components/schemas/ServerDto" } } } } } } } }, "/servers/{id}": { "get": { "tags": [ "server-controller" ], "operationId": "getServerById", "parameters": [ { "name": "id", "in": "path", "required": true, "schema": { "type": "string" } } ], "responses": { "200": { "description": "OK", "content": { "/": { "schema": { "$ref": "#/components/schemas/ServerDto" } } } } } } }, "/server/{name}": { "get": { "tags": [ "server-controller" ], "operationId": "getServerByName", "parameters": [ { "name": "name", "in": "path", "required": true, "schema": { "type": "string" } } ], "responses": { "200": { "description": "OK", "content": { "/": { "schema": { "type": "array", "items": { "$ref": "#/components/schemas/ServerDto" } } } } } } } }, "/hello": { "get": { "tags": [ "server-controller" ], "operationId": "name", "responses": { "200": { "description": "OK", "content": { "/": { "schema": { "type": "string" } } } } } } }, "/delete/{id}": { "delete": { "tags": [ "server-controller" ], "operationId": "DeleteServerById", "parameters": [ { "name": "id", "in": "path", "required": true, "schema": { "type": "string" } } ], "responses": { "200": { "description": "OK", "content": { "/": { "schema": { "type": "string" } } } } } } } }, "components": { "schemas": { "ServerDto": { "type": "object", "properties": { "id": { "type": "string" }, "name": { "type": "string" }, "language": { "type": "string" }, "framework": { "type": "string" } } } } } } image image

In this interactive swaggerui we can test all the endpoints as it provides all the functionalities to document and test APIs image image image

Task 3. Dockerfiles:

For this task, I used the first application to create a docker image into https://hub.docker.com I added the Jib plugin into my project, Jib is an interesting plugin developed by Google. https://cloud.google.com/java/getting-started/jib This plugin will help to create docker image directly into the docker hub without any Dockerfile

	<plugin>
  			<groupId>com.google.cloud.tools</groupId>
    		<artifactId>jib-maven-plugin</artifactId>
    		<version>2.8.0</version>
  		</plugin>

I was able to create docker image locally and then I pushed the image to the Docker hub

mvn compile jib:dockerBuild

image image

Task 4. Web UI Forms:

I used the first application to create web UI. The framework I chose to create web UI was Vaadin. Vaadin is a application platform for java. We just need to add its maven dependency which will install all the node modules in the runtime. Two classes are created, MainView.java and ServerEditor. MainView file deals with all the UI parts that are needed for the application. ServerEditor file calls the controller for all the logics in the backend. Altogether, the frontend UI is developed using vaadin as a platform.

To View all the servers image

To add new Server “New Server” Button is clicked image

  New server details are filled in the respective boxes

image

New server is added to the list

image

Finding server by name

image

Task 5. CI-CD pipeline:

I used Jenkins to build and publish docker image into the registry. I downloaded all the necessary plugins in Jenkins to configure the docker build. The configurations were setup correctly in Jenkins and the code is cloned from this github repository. The application building part was successful while the docker image build was a failure. Due to some unknown exceptions, my docker build failed and was not able to create image. image #UPDATE

I was able to build Docker image uploaded in the docker hub via Jenkins

image image

Task 6. Machine Learning Program

The Machine Learning program is present in the branch “MachineLearning”. I imported brest_cancer dataset for the implementation. Did some preprocessing of data by separating target variable and viewing target classes ['malignant', 'benign'] and then finding the class counts. Used sklearn.model_selection package for splitting train and test data. The data is converted into binarized format then applied logistic regression since its 2 class problem

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published