Commit 0427d1ef authored by verokarhu's avatar verokarhu

beginnings of an http gateway/api

parent c7af4a6f
......@@ -35,6 +35,7 @@ Tool commands:
Advanced Commands:
mount Mount an ipfs read-only mountpoint.
serve Serve an http gateway into ipfs.
Use "ipfs help <command>" for more information about a command.
`,
......@@ -49,6 +50,7 @@ Use "ipfs help <command>" for more information about a command.
cmdIpfsCommands,
cmdIpfsMount,
cmdIpfsInit,
cmdIpfsServe,
},
Flag: *flag.NewFlagSet("ipfs", flag.ExitOnError),
}
......
package main
import (
"github.com/gonuts/flag"
"github.com/jbenet/commander"
h "github.com/jbenet/go-ipfs/http"
)
var cmdIpfsServe = &commander.Command{
UsageLine: "serve",
Short: "Serve an HTTP API",
Long: `ipfs serve - Serve an http gateway into ipfs.`,
Run: serveCmd,
Flag: *flag.NewFlagSet("ipfs-serve", flag.ExitOnError),
}
func serveCmd(c *commander.Command, _ []string) error {
n, err := localNode()
if err != nil {
return err
}
return h.Serve("127.0.0.1", 80, n)
}
package http
import (
"fmt"
"github.com/gorilla/mux"
core "github.com/jbenet/go-ipfs/core"
"net/http"
"strconv"
)
type ipfsHandler struct {
node *core.IpfsNode
}
func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
nd, err := i.node.Resolver.ResolvePath(path)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "%s", nd.Data)
}
func Serve(host string, port uint16, node *core.IpfsNode) error {
r := mux.NewRouter()
r.PathPrefix("/").Handler(&ipfsHandler{node}).Methods("GET")
http.Handle("/", r)
address := host + ":" + strconv.FormatUint(uint64(port), 10)
fmt.Printf("Serving on %s\n", address)
return http.ListenAndServe(address, nil)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment