storj/examples/eestream/serve-pieces/main.go
Kaloyan Raev f96cc9c955 HTTP Ranger (#11)
* HTTPRanger

* Migrate eestream library to use ReadCloser instead of Reader

* Use MultiReadCloser instead of MultiReader

* Adapt example cli cmds to latest serverbox changes

* Close LazyReadCloser only if only generated

* Close ReadClosers sequentially

* Close response body on unexpected status code

* Avoid double close of the pipe reader

* Better formatting for imports

* Exit instead of panicking
2018-04-17 07:39:14 -06:00

50 lines
944 B
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
)
func main() {
flag.Parse()
if flag.Arg(0) == "" {
fmt.Printf("usage: %s <targetdir>\n", os.Args[0])
os.Exit(1)
}
err := Main()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func Main() error {
pieces, err := ioutil.ReadDir(flag.Arg(0))
if err != nil {
return err
}
for _, piece := range pieces {
pieceNum, err := strconv.Atoi(strings.TrimSuffix(piece.Name(), ".piece"))
if err != nil {
return err
}
pieceAddr := "localhost:" + strconv.Itoa(10000+pieceNum)
piecePath := filepath.Join(flag.Arg(0), piece.Name())
go http.ListenAndServe(pieceAddr, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, piecePath)
}))
}
select {} // sleep forever
}