File size: 1,935 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Implement a pluggable HTTP log service

Extend the supplied Go `loganalyzer` project. Separate log and tagging rules, application processing, persistence and the `net/http` interface so processing can be tested with an injected repository. Build and test offline with `go test ./...`.

## Interfaces

- Keep `Log` with its string JSON fields `id`, `message`, `level`, `timestamp` and `tag`.
- Keep `Repository` with `Save(Log) error` and `Get(id string) (Log, bool)`, and the constructors `NewMemoryRepository()`, `NewFileRepository(path)` and `NewRepositoryFromConfig(kind,path)` returning `Repository`.
- Keep `GenerateTag(Log) string`, `NewService(repo) *Service`, `Service.Process(Log) (Log,error)` and `NewHandler(repo) http.Handler`. Generate a nonempty tag from the log (for example by level); no tagging taxonomy is prescribed.
- Run the server with `go run ./cmd/server`. Default port is 8081; configure `PORT`, `STORAGE=memory|file` and the file path `LOG_FILE` through the environment.

## Acceptance Checklist

- `POST /logs` accepts one JSON object with nonempty `id`, `message`, `level` and RFC3339 `timestamp`. Generate a tag, save the complete record and return it as JSON with a successful 2xx status. Trailing whitespace is allowed, but extra JSON values or other junk are not.
- Malformed JSON or invalid fields return 400, leave no retrievable record, and do not disrupt later valid requests. `GET /log/:id` returns the full stored JSON record and 200 when found, or 404 when missing. Unsupported methods on either route return 405.
- Memory storage supports immediate reads; file storage persists records in JSON or CSV and loads them after reopening or restarting the server. Preserve messages of at least 100 KiB in full.
- A failed file write returns an error and leaves no unsaved record in memory. Creating missing parent directories is allowed; an unavailable destination must not give a success response.