Example #1
0
func main() {

	confName := flag.String("f", default_conf, "the config file")
	flag.Parse()

	var cfg Message
	data, err := ioutil.ReadFile(*confName)
	if err != nil {
		log.Fatal(err)
	}
	err = json.Unmarshal(data, &cfg)
	if err != nil {
		log.Fatal(err)
	}

	os.Mkdir(cfg.Root, 0755)
	stg := store.NewStore(cfg.Root)

	acc := make(map[string]string, len(cfg.Acc))
	for _, v := range cfg.Acc {
		acc[v.Uname] = v.Digest
	}
	service := vio.New(stg, cfg.MyHost, cfg.UrlExpireTime, acc)

	log.Println("vio is running at", cfg.BindHost)
	err = service.Run(cfg.BindHost)
	log.Fatal(err)
}
Example #2
0
func Test(t *testing.T) {
	os.Mkdir(root, 0755)
	stg := store.NewStore(root)
	service := &unsafeService{New(stg, "http://localhost:12333", 3600, nil, NewThumbStorage(stg))}
	go service.Run(":12333")
	time.Sleep(1e9)

	defer os.RemoveAll(root)

	src, err := os.Open(testfile)
	if err != nil {
		t.Fatal(err)
	}
	defer src.Close()
	fi, err := os.Stat(testfile)
	if err != nil {
		t.Fatal(err)
	}
	fsize := fi.Size()

	resp, err := http.Get("http://localhost:12333/put-auth")
	if err != nil {
		t.Fatal(err)
	}
	defer resp.Body.Close()
	buf := new(bytes.Buffer)
	io.CopyN(buf, resp.Body, resp.ContentLength)
	putUrl := buf.String()

	req, err := http.NewRequest("POST", putUrl, src)
	if err != nil {
		t.Fatal(err)
	}
	req.ContentLength = fsize
	resp, err = http.DefaultClient.Do(req)
	if err != nil {
		t.Fatal(err)
	}
	defer resp.Body.Close()

	name := new(bytes.Buffer)
	io.Copy(name, resp.Body)
	fmt.Println(name.String(), "\n")

	resp, err = http.Get("http://localhost:12333/get/" + name.String())
	if err != nil {
		t.Fatal(err)
	}
	defer resp.Body.Close()

	b := new(bytes.Buffer)
	io.CopyN(b, resp.Body, resp.ContentLength)
	getUrl := b.String()

	resp, err = http.Get(getUrl)
	if err != nil {
		t.Fatal(err)
	}
	if resp.ContentLength != fsize {
		t.Error(fsize, resp.ContentLength)
	}
}