mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 19:24:14 +08:00
add port flag to serve
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"github.com/gonuts/flag"
|
"github.com/gonuts/flag"
|
||||||
"github.com/jbenet/commander"
|
"github.com/jbenet/commander"
|
||||||
h "github.com/jbenet/go-ipfs/http"
|
h "github.com/jbenet/go-ipfs/http"
|
||||||
@ -14,11 +15,20 @@ var cmdIpfsServe = &commander.Command{
|
|||||||
Flag: *flag.NewFlagSet("ipfs-serve", flag.ExitOnError),
|
Flag: *flag.NewFlagSet("ipfs-serve", flag.ExitOnError),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
cmdIpfsServe.Flag.Uint("port", 80, "Port number")
|
||||||
|
}
|
||||||
|
|
||||||
func serveCmd(c *commander.Command, _ []string) error {
|
func serveCmd(c *commander.Command, _ []string) error {
|
||||||
|
port := c.Flag.Lookup("port").Value.Get().(uint)
|
||||||
|
if port < 1 || port > 65535 {
|
||||||
|
return errors.New("invalid port number")
|
||||||
|
}
|
||||||
|
|
||||||
n, err := localNode()
|
n, err := localNode()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return h.Serve("127.0.0.1", 80, n)
|
return h.Serve("127.0.0.1", port, n)
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
fmt.Fprintf(w, "%s", nd.Data)
|
fmt.Fprintf(w, "%s", nd.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Serve(host string, port uint16, node *core.IpfsNode) error {
|
func Serve(host string, port uint, node *core.IpfsNode) error {
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
r.PathPrefix("/").Handler(&ipfsHandler{node}).Methods("GET")
|
r.PathPrefix("/").Handler(&ipfsHandler{node}).Methods("GET")
|
||||||
http.Handle("/", r)
|
http.Handle("/", r)
|
||||||
|
Reference in New Issue
Block a user