This document provides essential information for AI coding agents (like yourself) to work efficiently in the **FHIR-Sandbox** repository. It covers build/test commands, code style guidelines, and the project's architecture.
---
## 1. Build, Lint, and Test Commands
### Build & Run
- **Build the binary:**
```bash
go build -o fhir-sandbox app/main.go
```
- **Run the application:**
```bash
go run app/main.go
```
The server starts on `http://localhost:8080` by default.
### Testing
- **Run all tests:**
```bash
go test ./...
```
- **Run a single test (by name):**
```bash
go test -v -run TestName ./app/db
```
- **Run tests with coverage:**
```bash
go test -cover ./...
```
*Note: New features MUST include `_test.go` files. We have 100% coverage on core DB and FHIR logic.*
### Linting & Formatting
- **Standard Go formatting:**
```bash
go fmt ./...
```
- **Import management (using goimports if available):**
```bash
goimports -w .
```
- **Static analysis (vet):**
```bash
go vet ./...
```
---
## 2. Code Style Guidelines
### General Principles
- **Simplicity:** Prefer standard library packages (e.g., `net/http`, `encoding/json`) over complex frameworks unless strictly necessary.
- **Explicit over Implicit:** Do not use magic values. Use constants or configuration fields.
- **Idiomatic Go:** Follow the patterns described in [Effective Go](https://golang.org/doc/effective_go).
### Imports
Group imports into three blocks, separated by a blank line:
- **State Parameter:** Use the `state` parameter to maintain context and prevent CSRF attacks. The current implementation uses cryptographically secure random values via `crypto/rand` and stores launch context server-side in a short-lived in-memory map (expires after 10 minutes).
- **Basic Auth:** Use `req.SetBasicAuth(clientID, clientSecret)` for the token exchange when required by the EHR.
- **Bearer Tokens:** Always include the `Authorization: Bearer <token>` header when fetching FHIR resources.
### FHIR Resources
- When fetching patient details, expect JSON and decode it into `map[string]interface{}` for flexibility, or define specific FHIR resource structs for better type safety.
- **Template Rendering:** Templates are parsed on every request (base.html + page.html) to avoid global template state conflicts. This is correct Go best practice.
- **Handler HTTP Methods:** All handler methods check the request method explicitly. For example, `/dashboard/sync` accepts both GET (auto-sync on first load) and POST (manual sync from UI).
- **State Store:** The in-memory state store in `launch.go` expires entries after 10 minutes and implements a 10-second grace period for duplicate requests.
- **Middleware Chain:** The session middleware provides both hard-gate (`RequireSession`) and soft-load (`LoadSession`) middleware. Hard-gate routes redirect unauthenticated users to `/`, while soft-load routes allow unauthenticated access but attach session context if present.
1.**Configuration Loading:** Implement a robust configuration loader for `app/main.go` (e.g., using `spf13/viper` or environment variables).
2.**Structured Logging:** Move from the standard `log` package to Go 1.21's `log/slog` for better performance and structured logging.
3.**Refresh Tokens:** Implement OAuth2 refresh token logic to maintain long-lived sessions without requiring re-authentication.
4.**Additional FHIR Resources:** Add support for more resources like Encounters, Procedures, Immunizations, etc.
5.**Frontend Enhancement:** Evolve the current templates into a more dynamic UI with better interactivity (e.g., using HTMX, htmx+ forms, or a modern JS framework if appropriate).
6.**FHIR Type Safety:** Consider using a comprehensive FHIR library (e.g., `google/fhir/go`) for type-safe resource handling as the scope grows.
7.**Testing:** Add comprehensive handler and integration tests to complement the existing db and fhir package tests.