storj/examples/eestream/serve-pieces/main.go

53 lines
1013 B
Go
Raw Normal View History

// 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)
}
}
2018-04-25 15:55:26 +01:00
// Main is the exported CLI executable function
func Main() error {
pieces, err := ioutil.ReadDir(flag.Arg(0))
if err != nil {
return err
}
2018-09-28 19:08:44 +01:00
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())
2018-09-28 19:08:44 +01:00
go fmt.Println(
http.ListenAndServe(pieceAddr, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, piecePath)
})))
}
select {} // sleep forever
}