changes in sockets and terminal development

This commit is contained in:
2025-04-21 14:59:25 +05:30
parent 305650925e
commit 86dcfa2a4a
11 changed files with 1357 additions and 9 deletions

View File

@@ -0,0 +1,209 @@
package handlers
import (
"encoding/json"
"log"
"net/http"
"sync"
"time"
"github.com/arnab-afk/monaco/internal/executor"
"github.com/arnab-afk/monaco/internal/models"
)
// Handler manages HTTP requests for code submissions
type Handler struct {
executionService *executor.ExecutionService
mu sync.Mutex
submissions map[string]*models.CodeSubmission
}
// NewHandler creates a new handler instance
func NewHandler() *Handler {
return &Handler{
executionService: executor.NewExecutionService(),
submissions: make(map[string]*models.CodeSubmission),
}
}
// SubmitHandler handles code submission requests
func (h *Handler) SubmitHandler(w http.ResponseWriter, r *http.Request) {
// Only allow POST method
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Parse the request body
var submission models.CodeSubmission
if err := json.NewDecoder(r.Body).Decode(&submission); err != nil {
http.Error(w, "Invalid request body: "+err.Error(), http.StatusBadRequest)
return
}
// Validate the submission
if submission.Code == "" {
http.Error(w, "Code is required", http.StatusBadRequest)
return
}
if submission.Language == "" {
http.Error(w, "Language is required", http.StatusBadRequest)
return
}
// Generate a unique ID for the submission
h.mu.Lock()
submission.ID = executor.GenerateUUID()
submission.Status = "pending"
h.submissions[submission.ID] = &submission
h.mu.Unlock()
// Execute the code in a goroutine
go h.executionService.ExecuteCode(&submission)
// Return the submission ID
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(map[string]string{"id": submission.ID})
}
// StatusHandler handles status check requests
func (h *Handler) StatusHandler(w http.ResponseWriter, r *http.Request) {
// Only allow GET method
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Get the submission ID from the query parameters
id := r.URL.Query().Get("id")
if id == "" {
http.Error(w, "ID is required", http.StatusBadRequest)
return
}
// Get the submission from the map
h.mu.Lock()
submission, exists := h.submissions[id]
h.mu.Unlock()
if !exists {
http.Error(w, "Submission not found", http.StatusNotFound)
return
}
// Return the submission status
response := map[string]interface{}{
"id": submission.ID,
"status": submission.Status,
}
// Add time information based on status
if !submission.QueuedAt.IsZero() {
response["queuedAt"] = submission.QueuedAt.Format(time.RFC3339)
}
if !submission.StartedAt.IsZero() {
response["startedAt"] = submission.StartedAt.Format(time.RFC3339)
}
if !submission.CompletedAt.IsZero() {
response["completedAt"] = submission.CompletedAt.Format(time.RFC3339)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
// ResultHandler handles result requests
func (h *Handler) ResultHandler(w http.ResponseWriter, r *http.Request) {
// Only allow GET method
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Get the submission ID from the query parameters
id := r.URL.Query().Get("id")
if id == "" {
http.Error(w, "ID is required", http.StatusBadRequest)
return
}
// Get the submission from the map
h.mu.Lock()
submission, exists := h.submissions[id]
h.mu.Unlock()
if !exists {
http.Error(w, "Submission not found", http.StatusNotFound)
return
}
// Return the submission result
response := map[string]interface{}{
"id": submission.ID,
"status": submission.Status,
"language": submission.Language,
"output": submission.Output,
}
// Add error information if available
if submission.Error != "" {
response["error"] = submission.Error
}
// Add time information
if !submission.QueuedAt.IsZero() {
response["queuedAt"] = submission.QueuedAt.Format(time.RFC3339)
}
if !submission.StartedAt.IsZero() {
response["startedAt"] = submission.StartedAt.Format(time.RFC3339)
}
if !submission.CompletedAt.IsZero() {
response["completedAt"] = submission.CompletedAt.Format(time.RFC3339)
if !submission.StartedAt.IsZero() {
response["executionTime"] = submission.CompletedAt.Sub(submission.StartedAt).Milliseconds()
}
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
// QueueStatsHandler provides information about the job queue
func (h *Handler) QueueStatsHandler(w http.ResponseWriter, r *http.Request) {
// Only allow GET method
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Get the queue statistics
stats := h.executionService.GetQueueStats()
// Return the queue statistics
response := map[string]interface{}{
"queue_stats": stats,
"submissions": len(h.submissions),
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
// HealthCheckHandler handles health check requests
func (h *Handler) HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
// Only allow GET method
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Return a simple health check response
response := map[string]interface{}{
"status": "ok",
"timestamp": time.Now().Format(time.RFC3339),
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}

View File

@@ -0,0 +1,70 @@
package handlers
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSubmitHandler(t *testing.T) {
h := NewHandler()
// Create a test request
reqBody := map[string]string{
"language": "python",
"code": "print('Hello, World!')",
"input": "",
}
reqJSON, _ := json.Marshal(reqBody)
req, err := http.NewRequest("POST", "/submit", bytes.NewBuffer(reqJSON))
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
// Create a response recorder
rr := httptest.NewRecorder()
// Call the handler
h.SubmitHandler(rr, req)
// Check the status code
assert.Equal(t, http.StatusAccepted, rr.Code)
// Check the response body
var response map[string]string
err = json.Unmarshal(rr.Body.Bytes(), &response)
assert.NoError(t, err)
assert.Contains(t, response, "id")
assert.NotEmpty(t, response["id"])
}
func TestHealthCheckHandler(t *testing.T) {
h := NewHandler()
// Create a test request
req, err := http.NewRequest("GET", "/health", nil)
if err != nil {
t.Fatal(err)
}
// Create a response recorder
rr := httptest.NewRecorder()
// Call the handler
h.HealthCheckHandler(rr, req)
// Check the status code
assert.Equal(t, http.StatusOK, rr.Code)
// Check the response body
var response map[string]interface{}
err = json.Unmarshal(rr.Body.Bytes(), &response)
assert.NoError(t, err)
assert.Equal(t, "ok", response["status"])
assert.Contains(t, response, "timestamp")
}

View File

@@ -0,0 +1,49 @@
package handlers
import (
"log"
"net/http"
"time"
)
// LoggingMiddleware logs HTTP requests
func LoggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
log.Printf("[HTTP] %s %s %s", r.Method, r.URL.Path, r.RemoteAddr)
next.ServeHTTP(w, r)
log.Printf("[HTTP] %s %s completed in %v", r.Method, r.URL.Path, time.Since(startTime))
})
}
// CORSMiddleware adds CORS headers to responses
func CORSMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Set CORS headers
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
// Handle preflight requests
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}
// Call the next handler
next.ServeHTTP(w, r)
})
}
// RecoveryMiddleware recovers from panics
func RecoveryMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
log.Printf("[PANIC] %v", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}()
next.ServeHTTP(w, r)
})
}