github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/requester/publicapi/server.go (about)

     1  package publicapi
     2  
     3  import (
     4  	"net/http"
     5  
     6  	sync "github.com/bacalhau-project/golang-mutex-tracer"
     7  	"github.com/filecoin-project/bacalhau/pkg/jobstore"
     8  	"github.com/filecoin-project/bacalhau/pkg/model"
     9  	"github.com/filecoin-project/bacalhau/pkg/publicapi"
    10  	"github.com/filecoin-project/bacalhau/pkg/requester"
    11  	"github.com/filecoin-project/bacalhau/pkg/storage"
    12  	"github.com/gorilla/websocket"
    13  )
    14  
    15  const APIPrefix = "requester/"
    16  
    17  type RequesterAPIServerParams struct {
    18  	APIServer          *publicapi.APIServer
    19  	Requester          requester.Endpoint
    20  	DebugInfoProviders []model.DebugInfoProvider
    21  	JobStore           jobstore.Store
    22  	StorageProviders   storage.StorageProvider
    23  }
    24  
    25  type RequesterAPIServer struct {
    26  	apiServer          *publicapi.APIServer
    27  	requester          requester.Endpoint
    28  	debugInfoProviders []model.DebugInfoProvider
    29  	jobStore           jobstore.Store
    30  	storageProviders   storage.StorageProvider
    31  	// jobId or "" (for all events) -> connections for that subscription
    32  	websockets      map[string][]*websocket.Conn
    33  	websocketsMutex sync.RWMutex
    34  }
    35  
    36  func NewRequesterAPIServer(params RequesterAPIServerParams) *RequesterAPIServer {
    37  	return &RequesterAPIServer{
    38  		apiServer:          params.APIServer,
    39  		requester:          params.Requester,
    40  		debugInfoProviders: params.DebugInfoProviders,
    41  		jobStore:           params.JobStore,
    42  		storageProviders:   params.StorageProviders,
    43  		websockets:         make(map[string][]*websocket.Conn),
    44  	}
    45  }
    46  
    47  func (s *RequesterAPIServer) RegisterAllHandlers() error {
    48  	handlerConfigs := []publicapi.HandlerConfig{
    49  		{URI: "/" + APIPrefix + "list", Handler: http.HandlerFunc(s.list)},
    50  		{URI: "/" + APIPrefix + "states", Handler: http.HandlerFunc(s.states)},
    51  		{URI: "/" + APIPrefix + "results", Handler: http.HandlerFunc(s.results)},
    52  		{URI: "/" + APIPrefix + "events", Handler: http.HandlerFunc(s.events)},
    53  		{URI: "/" + APIPrefix + "submit", Handler: http.HandlerFunc(s.submit)},
    54  		{URI: "/" + APIPrefix + "cancel", Handler: http.HandlerFunc(s.cancel)},
    55  		{URI: "/" + APIPrefix + "websocket/events", Handler: http.HandlerFunc(s.websocketJobEvents), Raw: true},
    56  		{URI: "/" + APIPrefix + "debug", Handler: http.HandlerFunc(s.debug)},
    57  	}
    58  	return s.apiServer.RegisterHandlers(handlerConfigs...)
    59  }