Example #1
0
File: main.go Project: sbinet/romba
func main() {
	config := new(Config)

	err := gcfg.ReadFileInto(config, "romba.ini")
	if err != nil {
		fmt.Fprintf(os.Stderr, "reading romba ini failed: %v\n", err)
		os.Exit(1)
	}

	for i := 0; i < len(config.Depot.MaxSize); i++ {
		config.Depot.MaxSize[i] *= int64(archive.GB)
	}

	runtime.GOMAXPROCS(config.General.Workers)

	flag.Set("log_dir", config.General.LogDir)
	flag.Set("alsologtostderr", "true")

	romDB, err := db.New(config.Index.Db)
	if err != nil {
		fmt.Fprintf(os.Stderr, "opening db failed: %v\n", err)
		os.Exit(1)
	}

	depot, err := archive.NewDepot(config.Depot.Root, config.Depot.MaxSize, romDB)
	if err != nil {
		fmt.Fprintf(os.Stderr, "creating depot failed: %v\n", err)
		os.Exit(1)
	}

	go signalCatcher(romDB)

	rs := service.NewRombaService(romDB, depot, config.Index.Dats, config.General.Workers, config.General.LogDir)

	s := rpc.NewServer()
	s.RegisterCodec(json2.NewCustomCodec(&rpc.CompressionSelector{}), "application/json")
	s.RegisterService(rs, "")
	http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("./web"))))
	http.Handle("/jsonrpc/", s)
	http.Handle("/progress", websocket.Handler(rs.SendProgress))

	fmt.Printf("starting romba server at localhost:%d/romba.html\n", config.Server.Port)

	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", config.Server.Port), nil))
}
Example #2
0
File: main.go Project: sbinet/romba
func main() {
	wg := new(sync.WaitGroup)
	wg.Add(1)

	go func() {
		log.Println(http.ListenAndServe("localhost:6060", nil))
		wg.Done()
	}()

	err := gcfg.ReadFileInto(config, "romba.ini")
	if err != nil {
		fmt.Fprintf(os.Stderr, "reading romba ini failed: %v\n", err)
		os.Exit(1)
	}

	for i := 0; i < len(config.Depot.MaxSize); i++ {
		config.Depot.MaxSize[i] *= int64(archive.GB)
	}

	romDB, err = db.New(config.Index.Db)
	if err != nil {
		fmt.Fprintf(os.Stderr, "opening db failed: %v\n", err)
		os.Exit(1)
	}

	err = cmd.Flag.Parse(os.Args[1:])
	if err != nil {
		fmt.Fprintf(os.Stderr, "parsing cmd line flags failed: %v\n", err)
		os.Exit(1)
	}

	args := cmd.Flag.Args()
	err = cmd.Run(args)
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		os.Exit(1)
	}
	//romDB.Close()

	wg.Wait()
}
Example #3
0
func main() {
	cfg := new(config.Config)

	iniPath, err := findINI()
	if err != nil {
		fmt.Fprintf(os.Stderr, "finding romba ini failed: %v\n", err)
		os.Exit(1)
	}

	err = gcfg.ReadFileInto(cfg, iniPath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "reading romba ini from %s failed: %v\n", iniPath, err)
		os.Exit(1)
	}

	for i := 0; i < len(cfg.Depot.MaxSize); i++ {
		cfg.Depot.MaxSize[i] *= int64(archive.GB)
	}

	cfg.General.LogDir, err = filepath.Abs(cfg.General.LogDir)
	if err != nil {
		fmt.Fprintf(os.Stderr, "reading romba ini failed: %v\n", err)
		os.Exit(1)
	}
	cfg.General.TmpDir, err = filepath.Abs(cfg.General.TmpDir)
	if err != nil {
		fmt.Fprintf(os.Stderr, "reading romba ini failed: %v\n", err)
		os.Exit(1)
	}
	for i, pv := range cfg.Depot.Root {
		cfg.Depot.Root[i], err = filepath.Abs(pv)
		if err != nil {
			fmt.Fprintf(os.Stderr, "reading romba ini failed: %v\n", err)
			os.Exit(1)
		}
	}
	cfg.Index.Dats, err = filepath.Abs(cfg.Index.Dats)
	if err != nil {
		fmt.Fprintf(os.Stderr, "reading romba ini failed: %v\n", err)
		os.Exit(1)
	}
	cfg.Index.Db, err = filepath.Abs(cfg.Index.Db)
	if err != nil {
		fmt.Fprintf(os.Stderr, "reading romba ini failed: %v\n", err)
		os.Exit(1)
	}
	cfg.General.WebDir, err = filepath.Abs(cfg.General.WebDir)
	if err != nil {
		fmt.Fprintf(os.Stderr, "reading romba ini failed: %v\n", err)
		os.Exit(1)
	}
	cfg.General.BadDir, err = filepath.Abs(cfg.General.BadDir)
	if err != nil {
		fmt.Fprintf(os.Stderr, "reading romba ini failed: %v\n", err)
		os.Exit(1)
	}

	config.GlobalConfig = cfg

	runtime.GOMAXPROCS(cfg.General.Cores)

	flag.Set("log_dir", cfg.General.LogDir)
	flag.Set("alsologtostderr", "true")
	flag.Set("v", strconv.Itoa(cfg.General.Verbosity))

	flag.Parse()

	romDB, err := db.New(cfg.Index.Db)
	if err != nil {
		fmt.Fprintf(os.Stderr, "opening db failed: %v\n", err)
		os.Exit(1)
	}

	depot, err := archive.NewDepot(cfg.Depot.Root, cfg.Depot.MaxSize, romDB)
	if err != nil {
		fmt.Fprintf(os.Stderr, "creating depot failed: %v\n", err)
		os.Exit(1)
	}

	rs := service.NewRombaService(romDB, depot, cfg)

	go signalCatcher(rs)

	s := rpc.NewServer()
	s.RegisterCodec(json2.NewCustomCodec(&rpc.CompressionSelector{}), "application/json")
	s.RegisterService(rs, "")
	http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir(cfg.General.WebDir))))
	http.Handle("/jsonrpc/", s)
	http.Handle("/progress", websocket.Handler(rs.SendProgress))

	fmt.Printf("starting romba server version %s at localhost:%d/romba.html\n", service.Version, cfg.Server.Port)

	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", cfg.Server.Port), nil))
}
Example #4
0
func TestDB(t *testing.T) {
	dbDir, err := ioutil.TempDir("", "rombadb")
	if err != nil {
		t.Fatalf("cannot create temp dir for test db: %v", err)
	}

	t.Logf("creating test db in %s\n", dbDir)

	krdb, err := db.New(dbDir)
	if err != nil {
		t.Fatalf("failed to open db: %v", err)
	}

	dat, sha1Bytes, err := parser.ParseDat(strings.NewReader(datText), "testing/dat")
	if err != nil {
		t.Fatalf("failed to parse test dat: %v", err)
	}

	err = krdb.IndexDat(dat, sha1Bytes)
	if err != nil {
		t.Fatalf("failed to index test dat: %v", err)
	}

	datFromDb, err := krdb.GetDat(sha1Bytes)
	if err != nil {
		t.Fatalf("failed to retrieve test dat: %v", err)
	}

	if !datFromDb.Equals(dat) {
		fmt.Printf("datFromDb=%s\n", string(types.PrintDat(datFromDb)))
		fmt.Printf("dat=%s\n", string(types.PrintDat(dat)))
		t.Fatalf("dat differs from db dat")
	}

	romSha1Bytes, err := hex.DecodeString("80353cb168dc5d7cc1dce57971f4ea2640a50ac4")
	if err != nil {
		t.Fatalf("failed to hex decode: %v", err)
	}

	rom := new(types.Rom)
	rom.Sha1 = romSha1Bytes

	dats, err := krdb.DatsForRom(rom)
	if err != nil {
		t.Fatalf("failed to retrieve dats for rom: %v", err)
	}

	if len(dats) != 1 {
		t.Fatalf("couldn't find dats for rom")
	}

	datFromDb = dats[0]

	if !datFromDb.Equals(dat) {
		fmt.Printf("datFromDb=%s\n", string(types.PrintDat(datFromDb)))
		fmt.Printf("dat=%s\n", string(types.PrintDat(dat)))
		t.Fatalf("dat differs from db dat")
	}

	err = krdb.Close()
	if err != nil {
		t.Fatalf("failed to close db: %v", err)
	}

	err = os.RemoveAll(dbDir)
	if err != nil {
		t.Fatalf("failed to remove test db dir %s: %v", dbDir, err)
	}
}