BOC v1.0: RS256 auth, Ledger integration, Prometheus metrics, CI/CD, backup
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
FROM alpine:latest
|
||||
RUN apk add --no-cache ca-certificates
|
||||
COPY event-stream /usr/local/bin/
|
||||
EXPOSE 9097
|
||||
CMD ["event-stream"]
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,10 @@
|
||||
module event-stream
|
||||
|
||||
go 1.25.10
|
||||
|
||||
require (
|
||||
github.com/go-chi/chi/v5 v5.3.1 // indirect
|
||||
github.com/klauspost/compress v1.15.9 // indirect
|
||||
github.com/pierrec/lz4/v4 v4.1.15 // indirect
|
||||
github.com/segmentio/kafka-go v0.4.51 // indirect
|
||||
)
|
||||
@@ -0,0 +1,8 @@
|
||||
github.com/go-chi/chi/v5 v5.3.1 h1:3j4HZLGZQ3JpMCrPJF/Jl3mYJfWLKBfNJ6quurUGCf8=
|
||||
github.com/go-chi/chi/v5 v5.3.1/go.mod h1:R+tYY2hNuVUUjxoPtqUdgBqevM9s9njzkTLutVsOCto=
|
||||
github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
|
||||
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
|
||||
github.com/pierrec/lz4/v4 v4.1.15 h1:MO0/ucJhngq7299dKLwIMtgTfbkoSPF6AoMYDd8Q4q0=
|
||||
github.com/pierrec/lz4/v4 v4.1.15/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
||||
github.com/segmentio/kafka-go v0.4.51 h1:JgDPPG75tC1rWIS2Me6MwcvXJ6f49UQ4HjAOef71Hno=
|
||||
github.com/segmentio/kafka-go v0.4.51/go.mod h1:Y1gn60kzLEEaW28YshXyk2+VCUKbJ3Qr6DrnT3i4+9E=
|
||||
@@ -0,0 +1,156 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/segmentio/kafka-go"
|
||||
)
|
||||
|
||||
type Event struct {
|
||||
EventID string `json:"event_id"`
|
||||
EventType string `json:"event_type"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
UserID string `json:"user_id,omitempty"`
|
||||
UserEmail string `json:"user_email,omitempty"`
|
||||
CompanyID string `json:"company_id,omitempty"`
|
||||
JobID string `json:"job_id,omitempty"`
|
||||
Amount float64 `json:"amount,omitempty"`
|
||||
Currency string `json:"currency,omitempty"`
|
||||
Status string `json:"status"`
|
||||
Country string `json:"country,omitempty"`
|
||||
IPAddress string `json:"ip_address,omitempty"`
|
||||
UserAgent string `json:"user_agent,omitempty"`
|
||||
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
||||
RiskScore float64 `json:"risk_score,omitempty"`
|
||||
AnomalyDetected bool `json:"anomaly_detected,omitempty"`
|
||||
}
|
||||
|
||||
type EventStore struct {
|
||||
kafkaWriter *kafka.Writer
|
||||
}
|
||||
|
||||
func NewEventStore(brokers []string) *EventStore {
|
||||
// Use explicit dialer to avoid DNS issues
|
||||
dialer := &kafka.Dialer{
|
||||
Timeout: 10 * time.Second,
|
||||
DualStack: true,
|
||||
}
|
||||
|
||||
return &EventStore{
|
||||
kafkaWriter: &kafka.Writer{
|
||||
Addr: kafka.TCP(brokers...),
|
||||
Topic: "quixzoom.events",
|
||||
Balancer: &kafka.LeastBytes{},
|
||||
Dialer: dialer,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *EventStore) PublishEvent(ctx context.Context, event Event) error {
|
||||
event.Timestamp = time.Now().UTC()
|
||||
if event.EventID == "" {
|
||||
event.EventID = fmt.Sprintf("evt_%d", time.Now().UnixNano())
|
||||
}
|
||||
|
||||
data, err := json.Marshal(event)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.kafkaWriter.WriteMessages(ctx, kafka.Message{
|
||||
Key: []byte(event.EventType),
|
||||
Value: data,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *EventStore) Close() error {
|
||||
return s.kafkaWriter.Close()
|
||||
}
|
||||
|
||||
func main() {
|
||||
brokers := os.Getenv("KAFKA_BROKERS")
|
||||
if brokers == "" {
|
||||
brokers = "172.24.0.5:29092"
|
||||
}
|
||||
|
||||
store := NewEventStore([]string{brokers})
|
||||
defer store.Close()
|
||||
|
||||
r := chi.NewRouter()
|
||||
r.Use(middleware.Logger)
|
||||
r.Use(middleware.Recoverer)
|
||||
r.Use(middleware.RequestID)
|
||||
|
||||
// Health check
|
||||
r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
|
||||
})
|
||||
|
||||
// Ingest event
|
||||
r.Post("/api/v1/events", func(w http.ResponseWriter, r *http.Request) {
|
||||
var event Event
|
||||
if err := json.NewDecoder(r.Body).Decode(&event); err != nil {
|
||||
http.Error(w, `{"error":"invalid json"}`, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if event.EventType == "" {
|
||||
http.Error(w, `{"error":"event_type required"}`, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Anomaly detection (simple rule-based)
|
||||
if event.Amount > 100000 {
|
||||
event.RiskScore = 0.8
|
||||
event.AnomalyDetected = true
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := store.PublishEvent(ctx, event); err != nil {
|
||||
log.Printf("Error publishing event: %v", err)
|
||||
http.Error(w, `{"error":"failed to publish"}`, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
"status": "published",
|
||||
"event_id": event.EventID,
|
||||
})
|
||||
})
|
||||
|
||||
// Search events (placeholder - would query Elasticsearch)
|
||||
r.Get("/api/v1/events/search", func(w http.ResponseWriter, r *http.Request) {
|
||||
query := r.URL.Query().Get("q")
|
||||
eventType := r.URL.Query().Get("type")
|
||||
userID := r.URL.Query().Get("user_id")
|
||||
|
||||
// TODO: Query Elasticsearch
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"query": query,
|
||||
"type": eventType,
|
||||
"user_id": userID,
|
||||
"results": []Event{},
|
||||
"total": 0,
|
||||
"note": "Elasticsearch integration pending",
|
||||
})
|
||||
})
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "9097"
|
||||
}
|
||||
|
||||
log.Printf("Event streaming server starting on :%s", port)
|
||||
log.Fatal(http.ListenAndServe(":"+port, r))
|
||||
}
|
||||
Reference in New Issue
Block a user