Beispiel #1
0
func (s *Server) Download(name string, w http.ResponseWriter, r *http.Request) {
	_, ok := s.stor.Get(name)
	if ok {
		// File exists
		s.Err(409, r, w)
		return
	}
	url := s.downloadUrl(r)
	tf := temp.NewFile(s.conf.TmpDir)
	aelog.Debugf("Start download from %s\n", url)
	err := tf.LoadFromUrl(url)
	defer tf.Close()
	if err != nil {
		aelog.Infof("Can't download : %s, err: %v\n", url, err)
		s.Err(500, r, w)
		return
	}
	// again check for exists
	_, ok = s.stor.Get(name)
	if ok {
		s.Err(409, r, w)
		return
	}
	s.save(name, tf.Size, tf.File, r, w)
}
Beispiel #2
0
func main() {
	defer func() {
		/* if r := recover(); r != nil {
		   	fmt.Println("Error!")
		   	fmt.Println(r)
		   }
		*/
	}()

	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			panic(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	if *httpprofile {
		go func() {
			log.Println(ghttp.ListenAndServe(":6060", nil))
		}()
	}

	var err error

	if *isPrintVersion {
		printVersion()
		return
	}

	if *isPrintHelp {
		printHelp()
		return
	}

	// If configFile not specified - show help
	if *configFile == "" {
		printHelp()
		return
	}

	// Init config
	c := &config.Config{}
	c.ReadFile(*configFile)

	// Init logger
	aelog.DefaultLogger, err = aelog.New(c.LogFile, c.LogLevel)
	if err != nil {
		panic(err)
	}

	// Init storage
	stor := &storage.Storage{}
	stor.Init(c)
	err = stor.Open()
	if err != nil {
		panic(err)
	}
	defer stor.Close()

	// init access log is needed
	var al *aelog.AntLog
	if c.LogAccess != "" {
		al, err = aelog.New(c.LogAccess, aelog.LOG_PRINT)
		if err != nil {
			panic(err)
		}
	}

	// Run server
	http.RunServer(stor, al)

	// Run rpc server
	rpcserver.StartRpcServer(stor)

	aelog.Infof("Run working (use %d cpus)", c.CpuNum)

	interrupt := make(chan os.Signal, 1)
	signal.Notify(interrupt, syscall.SIGKILL, os.Interrupt, syscall.SIGTERM)
	sig := <-interrupt
	aelog.Infof("Catched signal %v. Stop server", sig)
	stor.Dump()
	aelog.Infoln("")
}