server/listen/listen.go

79 lines
1.8 KiB
Go

package listen
import (
"fmt"
"net/http"
"git.ohea.xyz/cursorius/server/admin_api"
"git.ohea.xyz/cursorius/server/config"
"git.ohea.xyz/cursorius/server/database"
"git.ohea.xyz/cursorius/server/pipeline_api"
"git.ohea.xyz/cursorius/server/runnermanager"
"git.ohea.xyz/cursorius/server/webhook"
"github.com/google/uuid"
"github.com/op/go-logging"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"nhooyr.io/websocket"
)
var log = logging.MustGetLogger("cursorius-server")
func setupHTTPServer(
mux *http.ServeMux,
conf config.PipelineConf,
db database.Database,
runnerManagerChans runnermanager.RunnerManagerChans,
pollChan chan uuid.UUID,
) error {
webhook.CreateWebhookHandler(db, conf, mux)
pipeline_api.CreateHandler(runnerManagerChans.Allocation, runnerManagerChans.Release, mux)
err := admin_api.CreateHandler(db, pollChan, mux)
if err != nil {
return fmt.Errorf("Could not create admin api handler: %w", err)
}
mux.HandleFunc("/runner", func(w http.ResponseWriter, r *http.Request) {
conn, err := websocket.Accept(w, r, nil)
if err != nil {
log.Errorf("Could not upgrade runner connection to websocket: %v", err)
return
}
go runnermanager.RegisterRunner(conn, runnerManagerChans.Registration)
})
return nil
}
func Listen(
mux *http.ServeMux,
address string,
port int,
conf config.PipelineConf,
db database.Database,
runnerManagerChans runnermanager.RunnerManagerChans,
pollChan chan uuid.UUID,
) error {
err := setupHTTPServer(
mux,
conf,
db,
runnerManagerChans,
pollChan,
)
if err != nil {
return fmt.Errorf("Could not setup http endpoints: %w", err)
}
connect_string := fmt.Sprintf("%v:%v", address, port)
log.Noticef("Launching HTTP server on %v\n", connect_string)
return http.ListenAndServe(
connect_string,
h2c.NewHandler(mux, &http2.Server{}),
)
}