1
0
mirror of https://github.com/axllent/mailpit.git synced 2025-08-15 20:13:16 +02:00
Files
mailpit/server/apiv1/chaos.go
Ralph Slooten e29883fa1c Chore: Refactor API Swagger definitions and remove unused structs
- Removed deprecated `thumbnailParams` struct from `thumbnails.go`.
- Updated `server_test.go` to use an anonymous struct for response unmarshalling.
- Enhanced `swagger.json` with detailed definitions for SendRequest and SendMessageResponse.
- Introduced new `swaggerParams.go` to define Swagger parameters for various API endpoints.
- Created `swaggerResponses.go` to define Swagger responses for API endpoints.
- Cleaned up unused JSON error message definitions and consolidated error responses.
- Improved documentation for Chaos triggers and web UI configuration responses.
2025-06-22 15:25:22 +12:00

93 lines
2.0 KiB
Go

package apiv1
import (
"encoding/json"
"net/http"
"github.com/axllent/mailpit/internal/smtpd/chaos"
)
// GetChaos returns the current Chaos triggers
func GetChaos(w http.ResponseWriter, _ *http.Request) {
// swagger:route GET /api/v1/chaos testing getChaos
//
// # Get Chaos triggers
//
// Returns the current Chaos triggers configuration.
// This API route will return an error if Chaos is not enabled at runtime.
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: ChaosResponse
// 400: ErrorResponse
if !chaos.Enabled {
httpError(w, "Chaos is not enabled")
return
}
conf := chaos.Config
w.Header().Add("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(conf); err != nil {
httpError(w, err.Error())
}
}
// SetChaos sets the Chaos configuration.
func SetChaos(w http.ResponseWriter, r *http.Request) {
// swagger:route PUT /api/v1/chaos testing setChaosParams
//
// # Set Chaos triggers
//
// Set the Chaos triggers configuration and return the updated values.
// This API route will return an error if Chaos is not enabled at runtime.
//
// If any triggers are omitted from the request, then those are reset to their
// default values with a 0% probability (ie: disabled).
// Setting a blank `{}` will reset all triggers to their default values.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http, https
//
// Responses:
// 200: ChaosResponse
// 400: ErrorResponse
if !chaos.Enabled {
httpError(w, "Chaos is not enabled")
return
}
data := chaos.Triggers{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&data)
if err != nil {
httpError(w, err.Error())
return
}
if err := chaos.SetFromStruct(data); err != nil {
httpError(w, err.Error())
return
}
conf := chaos.Config
w.Header().Add("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(conf); err != nil {
httpError(w, err.Error())
}
}