ServeFile
This commit is contained in:
parent
06b167c669
commit
0bb6c111bb
|
@ -2,7 +2,9 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
@ -10,13 +12,15 @@ import (
|
||||||
"github.com/tendermint/tendermint/binary"
|
"github.com/tendermint/tendermint/binary"
|
||||||
. "github.com/tendermint/tendermint/common"
|
. "github.com/tendermint/tendermint/common"
|
||||||
pcm "github.com/tendermint/tendermint/process"
|
pcm "github.com/tendermint/tendermint/process"
|
||||||
rpc "github.com/tendermint/tendermint/rpc"
|
"github.com/tendermint/tendermint/rpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Routes = map[string]*rpc.RPCFunc{
|
var Routes = map[string]*rpc.RPCFunc{
|
||||||
"RunProcess": rpc.NewRPCFunc(RunProcess, []string{"wait", "label", "execPath", "args"}),
|
"RunProcess": rpc.NewRPCFunc(RunProcess, []string{"wait", "label", "execPath", "args"}),
|
||||||
"ListProcesses": rpc.NewRPCFunc(ListProcesses, []string{}),
|
"ListProcesses": rpc.NewRPCFunc(ListProcesses, []string{}),
|
||||||
"StopProcess": rpc.NewRPCFunc(StopProcess, []string{"label", "kill"}),
|
"StopProcess": rpc.NewRPCFunc(StopProcess, []string{"label", "kill"}),
|
||||||
|
// NOTE: also, two special non-JSONRPC routes called
|
||||||
|
// "download" and "upload".
|
||||||
}
|
}
|
||||||
|
|
||||||
type Validator struct {
|
type Validator struct {
|
||||||
|
@ -52,8 +56,11 @@ func main() {
|
||||||
fmt.Printf("Validators: %v\n", options.Validators)
|
fmt.Printf("Validators: %v\n", options.Validators)
|
||||||
|
|
||||||
// start rpc server.
|
// start rpc server.
|
||||||
fmt.Println("Listening HTTP-JSONRPC on", options.ListenAddress)
|
mux := http.NewServeMux()
|
||||||
rpc.StartHTTPServer(options.ListenAddress, Routes, nil)
|
mux.HandleFunc("/download", ServeFile)
|
||||||
|
// TODO: mux.HandleFunc("/upload", UploadFile)
|
||||||
|
rpc.RegisterRPCFuncs(mux, Routes)
|
||||||
|
rpc.StartHTTPServer(options.ListenAddress, mux)
|
||||||
|
|
||||||
TrapSignal(func() {
|
TrapSignal(func() {
|
||||||
fmt.Println("Debora shutting down")
|
fmt.Println("Debora shutting down")
|
||||||
|
@ -66,7 +73,7 @@ func main() {
|
||||||
type ResponseRunProcess struct {
|
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()
|
debora.mtx.Lock()
|
||||||
|
|
||||||
// First, see if there already is a process labeled 'label'
|
// 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.
|
// 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.processes[label] = proc
|
||||||
debora.mtx.Unlock()
|
debora.mtx.Unlock()
|
||||||
|
|
||||||
|
@ -125,3 +132,23 @@ func StopProcess(label string, kill bool) (*ResponseStopProcess, error) {
|
||||||
err := pcm.Stop(proc, kill)
|
err := pcm.Stop(proc, kill)
|
||||||
return &ResponseStopProcess{}, err
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package node
|
package node
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
bc "github.com/tendermint/tendermint/blockchain"
|
bc "github.com/tendermint/tendermint/blockchain"
|
||||||
|
@ -146,7 +147,12 @@ func (n *Node) StartRPC() {
|
||||||
core.SetConsensusState(n.consensusState)
|
core.SetConsensusState(n.consensusState)
|
||||||
core.SetMempoolReactor(n.mempoolReactor)
|
core.SetMempoolReactor(n.mempoolReactor)
|
||||||
core.SetSwitch(n.sw)
|
core.SetSwitch(n.sw)
|
||||||
rpc.StartHTTPServer(config.App().GetString("RPC.HTTP.ListenAddr"), core.Routes, n.evsw)
|
|
||||||
|
listenAddr := config.App().GetString("RPC.HTTP.ListenAddr")
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
rpc.RegisterEventsHandler(mux, n.evsw)
|
||||||
|
rpc.RegisterRPCFuncs(mux, core.Routes)
|
||||||
|
rpc.StartHTTPServer(listenAddr, mux)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) Switch() *p2p.Switch {
|
func (n *Node) Switch() *p2p.Switch {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package process
|
package process
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
@ -8,23 +9,24 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeFile(prefix string) *os.File {
|
func makeFile(prefix string) (string, *os.File) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
filename := fmt.Sprintf("%v_%v.out", prefix, now.Format("2006_01_02_15_04_05_MST"))
|
path := fmt.Sprintf("%v_%v.out", prefix, now.Format("2006_01_02_15_04_05_MST"))
|
||||||
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
return f
|
return path, file
|
||||||
}
|
}
|
||||||
|
|
||||||
type Process struct {
|
type Process struct {
|
||||||
Label string
|
Label string
|
||||||
ExecPath string
|
ExecPath string
|
||||||
StartTime time.Time
|
StartTime time.Time
|
||||||
Cmd *exec.Cmd `json:"-"`
|
OutputPath string
|
||||||
Output *os.File `json:"-"`
|
Cmd *exec.Cmd `json:"-"`
|
||||||
ExitState *os.ProcessState `json:"-"`
|
ExitState *os.ProcessState `json:"-"`
|
||||||
|
OutputFile *os.File `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -34,19 +36,22 @@ const (
|
||||||
|
|
||||||
// execPath: command name
|
// execPath: command name
|
||||||
// args: args to command. (should not include name)
|
// args: args to command. (should not include name)
|
||||||
func Create(mode int, label string, execPath string, args ...string) *Process {
|
func Create(mode int, label string, execPath string, args []string, input string) *Process {
|
||||||
out := makeFile(label)
|
outPath, outFile := makeFile(label)
|
||||||
cmd := exec.Command(execPath, args...)
|
cmd := exec.Command(execPath, args...)
|
||||||
switch mode {
|
switch mode {
|
||||||
case ProcessModeStd:
|
case ProcessModeStd:
|
||||||
cmd.Stdout = io.MultiWriter(os.Stdout, out)
|
cmd.Stdout = io.MultiWriter(os.Stdout, outFile)
|
||||||
cmd.Stderr = io.MultiWriter(os.Stderr, out)
|
cmd.Stderr = io.MultiWriter(os.Stderr, outFile)
|
||||||
cmd.Stdin = nil
|
cmd.Stdin = nil
|
||||||
case ProcessModeDaemon:
|
case ProcessModeDaemon:
|
||||||
cmd.Stdout = out
|
cmd.Stdout = outFile
|
||||||
cmd.Stderr = out
|
cmd.Stderr = outFile
|
||||||
cmd.Stdin = nil
|
cmd.Stdin = nil
|
||||||
}
|
}
|
||||||
|
if input != "" {
|
||||||
|
cmd.Stdin = bytes.NewReader([]byte(input))
|
||||||
|
}
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
fmt.Printf("Failed to run command. %v\n", err)
|
fmt.Printf("Failed to run command. %v\n", err)
|
||||||
return nil
|
return nil
|
||||||
|
@ -54,11 +59,13 @@ func Create(mode int, label string, execPath string, args ...string) *Process {
|
||||||
fmt.Printf("Success!")
|
fmt.Printf("Success!")
|
||||||
}
|
}
|
||||||
return &Process{
|
return &Process{
|
||||||
Label: label,
|
Label: label,
|
||||||
ExecPath: execPath,
|
ExecPath: execPath,
|
||||||
StartTime: time.Now(),
|
StartTime: time.Now(),
|
||||||
Cmd: cmd,
|
OutputPath: outPath,
|
||||||
Output: out,
|
Cmd: cmd,
|
||||||
|
ExitState: nil,
|
||||||
|
OutputFile: outFile,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,16 +10,10 @@ import (
|
||||||
|
|
||||||
"github.com/tendermint/tendermint/binary"
|
"github.com/tendermint/tendermint/binary"
|
||||||
. "github.com/tendermint/tendermint/common"
|
. "github.com/tendermint/tendermint/common"
|
||||||
"github.com/tendermint/tendermint/events"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func StartHTTPServer(listenAddr string, funcMap map[string]*RPCFunc, evsw *events.EventSwitch) {
|
func StartHTTPServer(listenAddr string, mux *http.ServeMux) {
|
||||||
log.Info(Fmt("Starting RPC HTTP server on %s", listenAddr))
|
log.Info(Fmt("Starting RPC HTTP server on %s", listenAddr))
|
||||||
mux := http.NewServeMux()
|
|
||||||
RegisterRPCFuncs(mux, funcMap)
|
|
||||||
if evsw != nil {
|
|
||||||
RegisterEventsHandler(mux, evsw)
|
|
||||||
}
|
|
||||||
go func() {
|
go func() {
|
||||||
res := http.ListenAndServe(
|
res := http.ListenAndServe(
|
||||||
listenAddr,
|
listenAddr,
|
||||||
|
|
Loading…
Reference in New Issue