コード例 #1
0
ファイル: main.go プロジェクト: RyuaNerin/hath
func main() {
	flag.Parse()
	log.Print("scanning")
	start := time.Now()
	frontend := hath.NewFrontend(dir)
	files := make(chan hath.File)
	progress := make(chan hath.Progress)
	wg := new(sync.WaitGroup)
	wg.Add(1)
	go func() {
		for p := range progress {
			log.Println("scan progress", p)
		}
	}()
	go func() {
		defer close(files)
		defer wg.Done()
		if err := frontend.Scan(files, progress); err != nil {
			log.Println("error while scanning:", err)
		} else {
			log.Println("scanned")
		}
	}()
	w := new(walker)
	for f := range files {
		// fmt.Print(".")
		w.count++
		w.sum += f.Size
	}
	wg.Wait()
	log.Println("end")
	end := time.Now()
	duration := end.Sub(start)
	log.Println("count", w.count, "size", bytefmt.ByteSize(uint64(w.sum)), "duration", duration)
}
コード例 #2
0
ファイル: main.go プロジェクト: RyuaNerin/hath
func main() {
	flag.Parse()
	fmt.Println("Hentai@Home", version)

	if err := createDirIfNotExists(); err != nil {
		log.Fatal("hath: error while checking directory", dir, err)
	}

	frontend := hath.NewFrontend(dir)
	db, err := hath.NewDB(path.Join(dir, "hath.db"))
	if err != nil {
		log.Fatal(err)
	}
	filesInDB := db.Count()
	log.Println("hath:", "files in database:", filesInDB)

	credentials := hath.Credentials{ClientID: clientID, Key: clientKey}
	cfg := hath.ServerConfig{}
	if len(credentialsPath) != 0 {
		f, err := os.Open(credentialsPath)
		if err != nil {
			log.Fatal(err)
		}
		_, err = toml.DecodeReader(f, &credentials)
		if err != nil {
			log.Fatal(err)
		}
		log.Println("hath: credentials loaded from", credentialsPath)
	}
	cfg.Credentials = credentials
	cfg.Frontend = frontend
	cfg.DataBase = db
	if debug {
		cfg.DontCheckTimestamps = true
		cfg.DontCheckSHA1 = true
		cfg.Debug = true
	} else {
		gin.SetMode(gin.ReleaseMode)
	}

	log.Println("hath:", "starting")
	s := hath.NewServer(cfg)

	// profiling endpoint
	// if debug {
	go func() {
		log.Println(http.ListenAndServe("localhost:6060", nil))
	}()
	// }

	// populating database from disk
	if filesInDB == 0 || scan {
		log.Println("server:", "database is empty; trying to scan files in cache")
		if err := s.PopulateFromFrontend(); err != nil {
			log.Fatalln("server:", "failed to scan files and add them to db:", err)
		}
		log.Println("server:", "cache scanned")
	}

	closer.Bind(func() {
		s.Close()
	})

	// starting server
	if err := s.Start(); err != nil {
		log.Fatal(err)
	}
	log.Fatal(s.Listen())
	closer.Hold()
}