Skip to content
Snippets Groups Projects
Commit 8b879eee authored by Andrej Rasevic's avatar Andrej Rasevic
Browse files

adding Project2

parent 02961beb
Branches main
No related tags found
No related merge requests found
# Project 2:
## Due Date: Wednesday, January 22nd, 2025 11:55 PM EST
## Objectives: To get familiar with creating a restful service that handles basic authentication and implements simple authorization. To gain familiarity with data persistence with a Restful service.
## Specifications/Requirements
In this exercise you will be creating a REST API in Go that will check for authentication and then if properly authenticated, check for the proper authorization when making requests to the endpoints the service provides. You have been provided a basic file layout structure but if you need to add more files for your design feel free to do so. You must use the __net/http__ package that is part of the standard Go library. You cannot use any third party Go Rest Library packages.
Details:
1. You will be using a postgres database like you did in Project 1, only instead of interacting with it from the command line you will be creating a Restful http service. You will be creating endpoints that correspond to the basic CRUD methods on a restful resource: GET, POST, PUT and DELETE. You will be defining the data you want to store inside of the models direcotry. It should be a struct and has to have at least 4 fields, one of which is an id. The id type should be jsut look you used in Project 1. For your data type you need to create the following endpoints:
* /data This should respond to both a GET and POST request. If it is a GET request you should respond with the current state of the database. If is a POST request, you should respond with the new resource you just added to your database as well as updating the database with the new entry. The parameters for your POST must be in the body of the request.
* /data/:id This should respond to GET, PUT and DELETE. You need to query the database to find the resource that matches the id you are passing. If it does not exist you must respond with a 404 and message saying "Resouce not found". If the resource does exist, you should respond with the resource for a GET request. If your request is a PUT then you should update the resource with the values in the body of the request and update the database with the new resource. If your request is a DELETE, then you should return the resource in your response and remove it from the database.
* /authenticate This endpoint will check for the Authentication header being present in the request, and for the Basic and value for the authetication parameters. Your application will only authenticate a request if it passes the password "Let's go Terps!" encoded in Base64. Once successfully authenticated, your server should set a header named IS_AUTHENTICATED to be true.
Your sever should run on port 8090 of localhost.
2. You need to define the following Header values to serve as your authorization mechanism:
* CAN_READ
* CAN_WRITE
Your application will need to check that for any authenticated request the permissions. A GET request should only be able to be reached if the request is authenticated AND has the CAN_READ header set to true. All other requests should only be reachable if the the request is authenticated and CAN_WRITE is true. No request should be able to proceed unless authenticated first.
3. Lastly, you will need to define functions that validate the data in your requests. If the data does not match the fields in the struct you are storing you should respond with a 404 status error along with the message: "Invalid data for type <your type>"
4. You need to create a readme file named __DataAndRoutes.md__ in which you indicate what the data is your api is modeling (i.e. what is the struct you define) and what are all the routes you created.
5. To test your api you should use Postman.
6. To get help please look inside of the HTTP directory inside of the LectureCodeExamples in the repository.
7. To get help with SQL or Postgres specifically please take a look at https://www.w3schools.com/sql/default.asp and https://www.w3schools.com/postgresql/index.php
To deliver your submission you will need to commit your changes locally and push to your repo on the university gitlab server. You __MUST__ verify that your push was successful by logging into gitlab and verifying that you can see your files. You also __should not__ commit the binary your program generates.
version: '3.8'
services:
postgres:
image: postgres:latest
container_name: PostgresCont
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: dbpassword
POSTGRES_DB: project2
ports:
- "5432:5432"
volumes:
- postgres_db:/var/lib/postgresql/data
volumes:
postgres_db:
driver: local
module cmsc398b/project2
go 1.20
require github.com/lib/pq v1.10.9
\ No newline at end of file
package main
import (
"net/http"
"fmt"
"os"
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment