github.com/blong14/gache@v0.0.0-20240124023949-89416fd8bbfa/internal/io/rpc/srv.go (about)

     1  package rpc
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  	"net/rpc"
     7  )
     8  
     9  type Handler any
    10  
    11  func Start(srv *http.Server, services []Handler) {
    12  	for _, service := range services {
    13  		if err := rpc.Register(service); err != nil {
    14  			log.Fatal(err)
    15  		}
    16  	}
    17  	rpc.HandleHTTP()
    18  	if err := srv.ListenAndServe(); err != http.ErrServerClosed {
    19  		log.Println(err)
    20  	}
    21  }
    22  
    23  func Client(addr string) (*rpc.Client, error) {
    24  	client, err := rpc.DialHTTP("tcp", addr)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	return client, nil
    29  }