Ejemplo n.º 1
0
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()
}
Ejemplo n.º 2
0
func appRun() {
	defer closer.Close()
	closer.Bind(func() {
		log.Println("Bye!")
	})

	if err := portaudio.Initialize(); paError(err) {
		log.Fatalln("PortAudio init error:", paErrorText(err))
	}
	closer.Bind(func() {
		if err := portaudio.Terminate(); paError(err) {
			log.Println("PortAudio term error:", paErrorText(err))
		}
	})

	var input io.Reader
	if strings.HasPrefix(*uri, "http://") || strings.HasPrefix(*uri, "https://") {
		resp, err := http.Get(*uri)
		if err != nil {
			log.Fatalln(err)
		}
		closer.Bind(func() {
			resp.Body.Close()
		})
		input = resp.Body
	} else {
		f, err := os.Open(*uri)
		if err != nil {
			log.Fatalln(err)
		}
		closer.Bind(func() {
			f.Close()
		})
		input = f
	}

	dec, err := decoder.New(input, samplesPerChannel)
	if err != nil {
		log.Fatalln(err)
	}
	closer.Bind(dec.Close)

	info := dec.Info()
	log.Println(fileInfoTable(info))

	dec.SetErrorHandler(func(err error) {
		log.Println("[WARN]", err)
	})
	go func() {
		dec.Decode()
		dec.Close()
	}()

	var wg sync.WaitGroup
	var stream *portaudio.Stream
	callback := paCallback(&wg, int(info.Channels), dec.SamplesOut())
	if err := portaudio.OpenDefaultStream(&stream, 0, int32(info.Channels), sampleFormat, info.SampleRate,
		samplesPerChannel, callback, nil); paError(err) {
		log.Fatalln("PortAudio error:", paErrorText(err))
	}
	closer.Bind(func() {
		if err := portaudio.CloseStream(stream); paError(err) {
			log.Println("[WARN] PortAudio error:", paErrorText(err))
		}
	})

	if err := portaudio.StartStream(stream); paError(err) {
		log.Fatalln("PortAudio error:", paErrorText(err))
	}
	closer.Bind(func() {
		if err := portaudio.StopStream(stream); paError(err) {
			log.Fatalln("[WARN] PortAudio error:", paErrorText(err))
		}
	})

	log.Println("Playing...")
	wg.Wait()
}
Ejemplo n.º 3
0
//Run starts the application run loop
func (a *App) Run() {
	closer.Bind(a.cleanup)
	closer.Checked(a.internalRun, true)
	defer closer.Close()
}