Description
Sometimes, it's useful to have a tool that can help you share a piece of code with other programmers. Actually, there is a website called Pastebin that does exactly that. A huge downside of Pastebin is that every piece of code you share automatically becomes available for the public. This could present a problem since many programmers work under the NDA (Non-disclosure agreement) which prohibits the use of such services to prevent the source code from leaking.
If there is a team of programmers who work in the same company and want to exchange pieces of code, they can solve this problem by using their own implementation of Pastebin. Such a web service is supposed to be accessible only locally, not via the Internet. In this project, you will write a service just like that.
As you're working on the project, you will implement two types of interfaces: API and web interface. The API interface should be accessed through endpoints that start with /api while web interface endpoints should start with /. The API interface should return data as JSON, while the web interface should use HTML, JS, and CSS.
Objectives
In the first stage, you need to develop a program that returns the same piece of code via the API and the web interface.
Implement two endpoints:
GET /codethat should:
- Return HTML that contains tags
<pre>with a piece of code inside and the titleCode; - Respond with a header
Content-Typewith the valuetext/html.
GET /api/codethat should:
- Return a JSON object with a single field
codethat contains the same piece of code; - Respond with a header
Content-Typewith the valueapplication/json.
In the upcoming stages, Content-Type of all the API endpoints should be equal to application/json, and of all the web endpoints should be equal to text/html.
Try to avoid duplicating the piece of code you return. Use the same object to construct both requests.
If you need some extra guidance on setting headers to the response, check out a comprehensive tutorial at baeldung.com.
Examples
Example 1
Request: GET /code
Response (you can use your own piece of code):
<html>
<head>
<title>Code</title>
</head>
<body>
<pre>
public static void main(String[] args) {
SpringApplication.run(CodeSharingPlatform.class, args);
}</pre>
</body>
</html>
Notice that the piece of code doesn't contain spaces before the lines: <pre> tags render such spaces, so keep that in mind.
Example 2
Request: GET /api/code
Response:
{
"code": "public static void main(String[] args) {\n SpringApplication.run(CodeSharingPlatform.class, args);\n}"
}