add flags and output to tusd cli

This commit is contained in:
Acconut 2015-02-17 20:07:49 +01:00
parent 2b66ccfd56
commit 09056611d6
1 changed files with 32 additions and 7 deletions

View File

@ -1,34 +1,59 @@
package main
import (
"flag"
"github.com/tus/tusd"
"github.com/tus/tusd/filestore"
"log"
"net/http"
"os"
)
var httpHost string
var httpPort string
var maxSize int64
var dir string
var stdout = log.New(os.Stdout, "[tusd] ", 0)
var stderr = log.New(os.Stderr, "[tusd] ", 0)
func init() {
flag.StringVar(&httpHost, "host", "0.0.0.0", "Host to bind HTTP server to")
flag.StringVar(&httpPort, "port", "1080", "Port to bind HTTP server to")
flag.Int64Var(&maxSize, "max-size", 0, "Maximum size of uploads in bytes")
flag.StringVar(&dir, "dir", "./data", "Directory to store uploads in")
flag.Parse()
}
func main() {
if err := os.MkdirAll("./data/", os.FileMode(0775)); err != nil {
panic(err)
stdout.Printf("Using '%s' as directory storage.\n", dir)
if err := os.MkdirAll(dir, os.FileMode(0775)); err != nil {
stderr.Fatalf("Unable to ensure directory exists: %s", err)
}
store := filestore.FileStore{
Path: "./data/",
Path: dir,
}
stdout.Printf("Using %.2fMB as maximum size.\n", float64(maxSize)/1024/1024)
handler, err := tusd.NewHandler(tusd.Config{
MaxSize: 1024 * 1024 * 1024,
MaxSize: maxSize,
BasePath: "files/",
DataStore: store,
})
if err != nil {
panic(err)
stderr.Fatalf("Unable to create handler: %s", err)
}
address := httpHost + ":" + httpPort
stdout.Printf("Using %s as address to listen.\n", address)
http.Handle("/files/", http.StripPrefix("/files/", handler))
err = http.ListenAndServe(":1080", nil)
err = http.ListenAndServe(address, nil)
if err != nil {
panic(err)
stderr.Fatalf("Unable to listen: %s", err)
}
}