mirror of
https://github.com/tendermint/tendermint.git
synced 2026-08-28 11:56:15 +00:00
ServeFile
This commit is contained in:
+32
-5
@@ -2,7 +2,9 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
@@ -10,13 +12,15 @@ import (
|
||||
"github.com/tendermint/tendermint/binary"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
pcm "github.com/tendermint/tendermint/process"
|
||||
rpc "github.com/tendermint/tendermint/rpc"
|
||||
"github.com/tendermint/tendermint/rpc"
|
||||
)
|
||||
|
||||
var Routes = map[string]*rpc.RPCFunc{
|
||||
"RunProcess": rpc.NewRPCFunc(RunProcess, []string{"wait", "label", "execPath", "args"}),
|
||||
"ListProcesses": rpc.NewRPCFunc(ListProcesses, []string{}),
|
||||
"StopProcess": rpc.NewRPCFunc(StopProcess, []string{"label", "kill"}),
|
||||
// NOTE: also, two special non-JSONRPC routes called
|
||||
// "download" and "upload".
|
||||
}
|
||||
|
||||
type Validator struct {
|
||||
@@ -52,8 +56,11 @@ func main() {
|
||||
fmt.Printf("Validators: %v\n", options.Validators)
|
||||
|
||||
// start rpc server.
|
||||
fmt.Println("Listening HTTP-JSONRPC on", options.ListenAddress)
|
||||
rpc.StartHTTPServer(options.ListenAddress, Routes, nil)
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/download", ServeFile)
|
||||
// TODO: mux.HandleFunc("/upload", UploadFile)
|
||||
rpc.RegisterRPCFuncs(mux, Routes)
|
||||
rpc.StartHTTPServer(options.ListenAddress, mux)
|
||||
|
||||
TrapSignal(func() {
|
||||
fmt.Println("Debora shutting down")
|
||||
@@ -66,7 +73,7 @@ func main() {
|
||||
type ResponseRunProcess struct {
|
||||
}
|
||||
|
||||
func RunProcess(wait bool, label string, execPath string, args []string) (*ResponseRunProcess, error) {
|
||||
func RunProcess(wait bool, label string, execPath string, args []string, input string) (*ResponseRunProcess, error) {
|
||||
debora.mtx.Lock()
|
||||
|
||||
// First, see if there already is a process labeled 'label'
|
||||
@@ -77,7 +84,7 @@ func RunProcess(wait bool, label string, execPath string, args []string) (*Respo
|
||||
}
|
||||
|
||||
// Otherwise, create one.
|
||||
proc := pcm.Create(pcm.ProcessModeDaemon, label, execPath, args...)
|
||||
proc := pcm.Create(pcm.ProcessModeDaemon, label, execPath, args, input)
|
||||
debora.processes[label] = proc
|
||||
debora.mtx.Unlock()
|
||||
|
||||
@@ -125,3 +132,23 @@ func StopProcess(label string, kill bool) (*ResponseStopProcess, error) {
|
||||
err := pcm.Stop(proc, kill)
|
||||
return &ResponseStopProcess{}, err
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
func ServeFile(w http.ResponseWriter, req *http.Request) {
|
||||
path := req.FormValue("path")
|
||||
if path == "" {
|
||||
http.Error(w, "Must specify path", 400)
|
||||
return
|
||||
}
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
http.Error(w, Fmt("Error opening file: %v. %v", path, err), 400)
|
||||
return
|
||||
}
|
||||
_, err = io.Copy(w, file)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, Fmt("Error serving file: %v. %v", path, err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user