-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
97 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package file | ||
|
||
import ( | ||
"github.com/multiverse-vcs/go-multiverse/pkg/remote" | ||
) | ||
|
||
// Service wraps a remote and provides RPC. | ||
type Service struct { | ||
*remote.Server | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,90 @@ | ||
package rpc | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"net" | ||
"net/http" | ||
"net/rpc" | ||
"net/rpc/jsonrpc" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/multiverse-vcs/go-multiverse/pkg/remote" | ||
"github.com/multiverse-vcs/go-multiverse/pkg/rpc/author" | ||
"github.com/multiverse-vcs/go-multiverse/pkg/rpc/file" | ||
"github.com/multiverse-vcs/go-multiverse/pkg/rpc/repo" | ||
) | ||
|
||
// SockFile is the name of the unix socket file. | ||
const SockFile = "rpc.sock" | ||
|
||
// DialErrMsg is an error message for failed RPC connections. | ||
var DialErrMsg = ` | ||
Could not connect to local RPC server. | ||
Make sure the Multiverse daemon is up. | ||
See 'multi help daemon' for more info. | ||
` | ||
|
||
// HttpConn wraps an HTTP request. | ||
type HttpConn struct { | ||
r io.Reader | ||
w io.Writer | ||
} | ||
|
||
// Read reads bytes from the reader. | ||
func (c *HttpConn) Read(p []byte) (n int, err error) { | ||
return c.r.Read(p) | ||
} | ||
|
||
// Write writes bytes to the writer. | ||
func (c *HttpConn) Write(d []byte) (n int, err error) { | ||
return c.w.Write(d) | ||
} | ||
|
||
// Close does nothing. | ||
func (c *HttpConn) Close() error { | ||
return nil | ||
} | ||
|
||
// NewClient returns a new RPC client. | ||
func NewClient() (*rpc.Client, error) { | ||
home, err := os.UserHomeDir() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
config := remote.NewConfig(filepath.Join(home, remote.DotDir)) | ||
if err := config.Read(); err != nil { | ||
return nil, err | ||
} | ||
|
||
socket := filepath.Join(home, remote.DotDir, SockFile) | ||
return rpc.DialHTTP("unix", socket) | ||
return rpc.DialHTTP("tcp", config.HttpAddress) | ||
} | ||
|
||
// ListenAndServe starts the RPC listener. | ||
func ListenAndServe(server *remote.Server) error { | ||
socket := filepath.Join(server.Root, SockFile) | ||
if err := os.RemoveAll(socket); err != nil { | ||
return err | ||
} | ||
|
||
rpc.RegisterName("Author", &author.Service{server}) | ||
rpc.RegisterName("File", &file.Service{server}) | ||
rpc.RegisterName("Repo", &repo.Service{server}) | ||
|
||
rpc.HandleHTTP() | ||
http.HandleFunc("/_jsonRPC_", ServeHTTP) | ||
|
||
listener, err := net.Listen("unix", socket) | ||
listener, err := net.Listen("tcp", server.Config.HttpAddress) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer listener.Close() | ||
|
||
return http.Serve(listener, nil) | ||
} | ||
|
||
// ServeHTTP serves json rpc connections over http. | ||
func ServeHTTP(w http.ResponseWriter, req *http.Request) { | ||
w.Header().Set("Access-Control-Allow-Origin", "*") | ||
w.Header().Set("Access-Control-Allow-Methods", "*") | ||
w.Header().Set("Access-Control-Allow-Headers", "*") | ||
w.WriteHeader(http.StatusOK) | ||
|
||
if req.Method == http.MethodPost { | ||
jsonrpc.ServeConn(&HttpConn{req.Body, w}) | ||
} | ||
} |