feat: implement publish endpoint and related database functionality
This commit is contained in:
parent
b09275fa67
commit
582d93a103
2 changed files with 417 additions and 30 deletions
64
internal/api/handlers/v0/publish.go
Normal file
64
internal/api/handlers/v0/publish.go
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
// Package v0 contains API handlers for version 0 of the API
|
||||
package v0
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/modelcontextprotocol/registry/internal/model"
|
||||
"github.com/modelcontextprotocol/registry/internal/service"
|
||||
)
|
||||
|
||||
// PublishHandler handles requests to publish new server details to the registry
|
||||
func PublishHandler(registry service.RegistryService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// Only allow POST method
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Read the request body
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, "Error reading request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
// Parse request body into ServerDetail struct
|
||||
var serverDetail model.ServerDetail
|
||||
err = json.Unmarshal(body, &serverDetail)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid request payload", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate required fields
|
||||
if serverDetail.Name == "" {
|
||||
http.Error(w, "Name is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// version is required
|
||||
if serverDetail.VersionDetail.Version == "" {
|
||||
http.Error(w, "Version is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Call the publish method on the registry service
|
||||
err = registry.Publish(&serverDetail)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to publish server details: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
"message": "Server publication successful",
|
||||
"id": serverDetail.ID,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -16,6 +16,7 @@ func RegisterV0Routes(mux *http.ServeMux, cfg *config.Config, registry service.R
|
|||
mux.HandleFunc("/v0/servers", v0.ServersHandler(registry))
|
||||
mux.HandleFunc("/v0/servers/{id}", v0.ServersDetailHandler(registry))
|
||||
mux.HandleFunc("/v0/ping", v0.PingHandler(cfg))
|
||||
mux.HandleFunc("/v0/publish", v0.PublishHandler(registry))
|
||||
|
||||
// Register Swagger UI routes
|
||||
mux.HandleFunc("/v0/swagger/", v0.SwaggerHandler())
|
||||
|
|
|
|||
Loading…
Reference in a new issue