Add Dockerfile, update README, minor refactoring.

main
Francesco Minnocci 1 year ago
parent f5e12d2cae
commit 7e17494f20
Signed by: BachoSeven
GPG Key ID: 2BE4AB7FDAD828A4

@ -0,0 +1,11 @@
FROM golang:latest
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download && go mod verify
COPY . .
RUN go build -v -o ./bin/drone-example .
CMD ["./bin/drone-example"]

@ -1,29 +1,25 @@
# API to check for Maths students # Go Maths students API
## Server Prerequisites A simple Go API whose only purpose is to check whether an user is a Maths student or not.
- Having `go` installed ## Prerequisites
- An environment variable `AUTHPDS_TOKEN` containing the authentication token for the external API. - An environment variable `AUTHPDS_TOKEN` containing the authentication token for the external API.
- An environment variable named `SECRET` containing the secret token for authorization. - An environment variable named `SECRET` containing the secret token for authorization.
## Server Setup ## Docker
- Clone the repository: To build and run the image from the provided `Dockerfile`:
```
git clone https://git.phc.dm.unipi.it/phc/go-maths-api ```bash shell
``` $ docker build -t go-maths-api .
- Change directory and build the project: $ docker run -d --restart always -p 127.235.203.11:1099 --env 'AUTHPDS_TOKEN' --env 'SECRET' --name go-maths-api go-maths-api
```
cd go-maths-api
go build
``` ```
- Run with `./go-maths-api`
## Client Usage ## Client Usage
An example request (with `SECRET` defined in your environment): An example request to the local Docker image (with `SECRET` defined in your environment):
``` ```
curl -X GET "http://localhost:8080/check-maths-user?SANITIZED_USER=f.minnocci" -H "Authorization: Bearer $SECRET" curl -X GET "http://asdf:8080/check-maths-user?user=f.minnocci" -H "Authorization: Bearer $SECRET"
``` ```
Successful JSON response: Successful JSON response:

@ -0,0 +1,3 @@
module git.phc.dm.unipi.it/phc/go-maths-api
go 1.21.1

@ -17,8 +17,8 @@ func main() {
} }
func CheckMathsUserHandler(w http.ResponseWriter, r *http.Request) { func CheckMathsUserHandler(w http.ResponseWriter, r *http.Request) {
// Get the SANITIZED_USER from the query parameters // Get the username from the query parameters
sanitizedUser := r.URL.Query().Get("SANITIZED_USER") userName := r.URL.Query().Get("user")
// Get the AUTHORIZATION header value, which should include the secret token // Get the AUTHORIZATION header value, which should include the secret token
authHeader := r.Header.Get("Authorization") authHeader := r.Header.Get("Authorization")
@ -34,7 +34,7 @@ func CheckMathsUserHandler(w http.ResponseWriter, r *http.Request) {
authPdsToken := os.Getenv("AUTHPDS_TOKEN") authPdsToken := os.Getenv("AUTHPDS_TOKEN")
// Make a GET request to the external API // Make a GET request to the external API
apiURL := fmt.Sprintf("https://api.unipi.it/authPds/api/Carriera/studente/uid/%s/", sanitizedUser) apiURL := fmt.Sprintf("https://api.unipi.it/authPds/api/Carriera/studente/uid/%s/", userName)
req, err := http.NewRequest("GET", apiURL, nil) req, err := http.NewRequest("GET", apiURL, nil)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)

Loading…
Cancel
Save