2022-10-20 19:59:46 +00:00
|
|
|
package main
|
2022-10-16 18:33:31 +00:00
|
|
|
|
|
|
|
import (
|
2024-06-23 12:37:10 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2024-06-10 20:31:29 +00:00
|
|
|
"fmt"
|
2023-08-05 21:53:17 +00:00
|
|
|
"isle/daemon"
|
2024-06-23 12:37:10 +00:00
|
|
|
"isle/daemon/jsonrpc2"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mctx"
|
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
2022-10-16 18:33:31 +00:00
|
|
|
)
|
|
|
|
|
2024-06-23 12:37:10 +00:00
|
|
|
const daemonHTTPRPCPath = "/rpc/v0.json"
|
|
|
|
|
|
|
|
func newHTTPServer(
|
|
|
|
ctx context.Context, logger *mlog.Logger, rpc *daemon.RPC,
|
|
|
|
) (
|
|
|
|
*http.Server, error,
|
|
|
|
) {
|
2024-07-06 12:26:06 +00:00
|
|
|
socketPath := daemon.HTTPSocketPath()
|
|
|
|
l, err := net.Listen("unix", socketPath)
|
2024-06-23 12:37:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
2024-07-06 12:26:06 +00:00
|
|
|
"failed to listen on socket %q: %w", socketPath, err,
|
2024-06-23 12:37:10 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = mctx.Annotate(ctx, "httpAddr", l.Addr().String())
|
|
|
|
logger.Info(ctx, "HTTP server socket created")
|
|
|
|
|
|
|
|
rpcHandler := jsonrpc2.Chain(
|
|
|
|
jsonrpc2.NewMLogMiddleware(logger.WithNamespace("rpc")),
|
|
|
|
jsonrpc2.ExposeServerSideErrorsMiddleware,
|
|
|
|
)(
|
|
|
|
jsonrpc2.NewDispatchHandler(&rpc),
|
|
|
|
)
|
|
|
|
|
|
|
|
httpMux := http.NewServeMux()
|
|
|
|
httpMux.Handle(daemonHTTPRPCPath, jsonrpc2.NewHTTPHandler(rpcHandler))
|
|
|
|
|
|
|
|
srv := &http.Server{Handler: httpMux}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := srv.Serve(l); !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
logger.Fatal(ctx, "HTTP server unexpectedly shut down", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return srv, nil
|
|
|
|
}
|