package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
const webhookSignatureKey = "YOUR_WEBHOOK_SECRET_FROM_ADMIN_CONSOLE"
// LabReport represents the webhook payload structure
type LabReport struct {
ApplicationUserID string `json:"application_user_id"`
RecordID string `json:"record_id"`
Status string `json:"status"`
ParsingError string `json:"parsing_error,omitempty"`
CollectionDate string `json:"collection_date,omitempty"`
UploadedAt time.Time `json:"uploaded_at"`
ModifiedAt time.Time `json:"modified_at"`
// Add other fields as needed
}
func main() {
http.HandleFunc("/lab-reports-webhook", func(w http.ResponseWriter, r *http.Request) {
// Verify HMAC signature
signature := r.Header.Get("x-body-signature")
if signature == "" {
http.Error(w, "Missing signature", http.StatusBadRequest)
return
}
// Read request body
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Failed to read body", http.StatusInternalServerError)
return
}
// Calculate and verify HMAC
hm := hmac.New(sha256.New, []byte(webhookSignatureKey))
hm.Write(body)
expectedSignature := hex.EncodeToString(hm.Sum(nil))
if signature != expectedSignature {
http.Error(w, "Invalid signature", http.StatusUnauthorized)
return
}
// Parse lab report
var record LabReport
if err := json.Unmarshal(body, &record); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
// Process the lab report
fmt.Printf("Received lab report analysis: %s for user %s\n", record.Status, record.ApplicationUserID)
if record.Status == "completed" {
fmt.Printf("Lab report completed for record %s\n", record.RecordID)
// Update your application with the results
} else if record.Status == "failed" {
fmt.Printf("Analysis failed: %s\n", record.ParsingError)
// Handle failure case
}
// Respond with success
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})
fmt.Println("Starting lab reports webhook server on port 8000")
http.ListenAndServe(":8000", nil)
}