computer-manager-api

command module
v0.0.0-...-8c8748e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2023 License: MIT Imports: 1 Imported by: 0

README

Computer Manager API

A basic application for system administrators to keep track of the computers issued by their company. The application provides a REST API to manage computer-related datasets that are stored in a postgres database.

Build
Build binary
go build

Creates the binary computer-manager-api.

Build Docker image
docker build . -t computer-manager-api
Run
docker-compose up

The application listens on port 8080.

http://localhost:8080/

The database can be reached via the DSN postgres://postgres:postgres@localhost:5433/computer_manager_api.

psql postgres://postgres:postgres@localhost:5433/computer_manager_api
Endpoints
Action HTTP Method Path Content-Type Request Body Description
Create(Computers) POST /computers application/json see JSON Store the data of a new computer
Read(Computers) GET /computers/{mac} none empty Get the data of a computer
Update(Computers) PUT /computers/{mac} application/json like JSON, but field macAddr can be omitted Update the data of a computer
Delete(Computers) DELETE /computers/{mac} none empty Delete the data of a computer
Read All(Computers) GET /computers none empty Get the data of all computers
Read All Computers(Employee) GET /employees/{employee-abbreviation}/computers none empty Get the data of all assigned computers for an employee

Update(Computers):

  • operation does not support update of the MAC address
  • operation will fail if any required field (other than MAC address) is not provided in request body
  • operation will overwrite optional fields with empty value if field is not provided in request body

Delete(Computers):

  • repeatedly calling delete on same resource will return 200
Computer Model JSON
{
  "macAddr": "<MAC address (required)>",
  "computerName": "<computer name (required)>",
  "ipAddr": "<IP address (required)>",
  "employeeAbbr": "<employee abbreviation (optional)>",
  "description": "<description (optional)>"
}
Example Requests
Create
curl localhost:8080/computers \
-X POST \
-H "Content-Type: application/json" \
-d '{"macAddr": "00:1B:44:11:3A:B7", "computerName": "localhorst", "ipAddr": "127.0.0.1", "employeeAbbr": "rpm", "description": "hello :)"}' \
-i
Read
curl localhost:8080/computers/00:1B:44:11:3A:B7 \
-X GET \
-i
Update
curl localhost:8080/computers/00:1B:44:11:3A:B7 \
-X PUT \
-H "Content-Type: application/json" \
-d '{"computerName": "localhorst", "ipAddr": "127.0.0.2", "employeeAbbr": "mmu"}' \
-i
Delete
curl localhost:8080/computers/00:1B:44:11:3A:B7 \
-X DELETE \
-i
Read All
curl localhost:8080/computers \
-X GET \
-i
Read All For Employee
curl localhost:8080/employees/rpm/computers \
-X GET \
-i
Limitations
  • MAC address is considered to be unique and used as primary key in the database table
  • employee abbreviation is considered to be unique for each employee
Next Steps
  • add more tests
  • validate and normalize incoming data, i.e. parse types, check constraints, store in standardized representation etc.
    • sanitizing input to prevent SQL injection is not needed since GORM takes care of this
  • return meaningful HTTP status codes and responses, especially for client-sided errors
  • add client authentication, maybe user management
  • enable TLS
  • make service more robust, for example with request timeouts and throttling
  • add exhaustive logging
  • add metrics (Prometheus)
  • add readiness and health check endpoints
  • support graceful shutdown
  • consider using advanced database migration framework like github.com/golang-migrate/migrate
  • automate workflows
    • add makefile
    • add CI (GitHub Actions)

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL