示例#1
0
func main() {
	flag.Parse()

	// Open a new logger with an appropriate log level.
	if *fQuiet {
		LogLevel = -1 // Disable ALL output
		//	} else if *fVerbose {
		//		LogLevel = log.INFO
	} else if *fDebug {
		LogLevel = log.DEBUG
	}
	l, _ = log.NewLevel(LogLevel, true, os.Stdout, "", log.Ltime)

	// If any of the 'show' flags are set, print the relevant variable
	// and exit.
	switch {
	case *fShowVersion:
		l.Println(Version)
		return
	case *fShowFVersion:
		l.Println(Version)
		return
	case *fShowBind:
		l.Println(Bind)
		return
	case *fShowPort:
		l.Println(Port)
		return
	case *fShowRes:
		l.Println(Resources)
		return
	}

	l.Infof("Starting Grove version %s\n", Version)

	var repodir string
	if flag.NArg() > 0 {
		repodir = path.Clean(flag.Arg(0))

		if !path.IsAbs(repodir) {
			wd, err := os.Getwd()
			if err != nil {
				l.Fatalf("Error getting working directory: %s\n", err)
			}
			repodir = path.Join(wd, repodir)
		}
	} else {
		wd, err := os.Getwd()
		if err != nil {
			l.Fatalf("Error getting working directory: %s\n", err)
		}
		repodir = wd
	}

	Serve(repodir)
}
func main() {
	flag.Parse()

	// Load the configuration.
	var err error
	Conf, err = ReadConfig(*fConf)
	if err != nil {
		fmt.Printf("Could not read conf: %s", err)
		os.Exit(1)
	}
	// Touch up any fields if they're in common but unusable forms.
	if len(Conf.Prefix) == 0 {
		Conf.Prefix = "/"
	}

	// Initialize the log with an appropriate log level. Do this
	// inside a separate scope so that variables can be garbage
	// collected.
	{
		var level log.LogLevel
		if *fDebug {
			level = log.DEBUG
		} else {
			level = DefaultLogLevel
		}
		l, err = log.NewLevel(level, true, os.Stdout, "",
			log.Ldate|log.Ltime)
		if err != nil {
			fmt.Printf("Could start logger: %s", err)
			os.Exit(1)
		}
	}

	l.Infof("Starting NodeAtlas %s\n", Version)

	if *fTestDB {
		// Open up a temporary database using sqlite3.
		tempDB := path.Join(os.TempDir(), "nodeatlas-test.db")

		db, err := sql.Open("sqlite3", tempDB)
		if err != nil {
			l.Fatalf("Could not connect to temporary database: %s", err)
		}

		// Perform all of the tests in sequence.
		TestDatabase(DB{db, false})
		err = db.Close()
		if err != nil {
			l.Emergf("Could not close temporary database: %s", err)
		}

		// Finally, remove the database file and exit.
		err = os.Remove(tempDB)
		if err != nil {
			l.Emergf("Could not remove temporary database %q: %s",
				tempDB, err)
		}
		return
	}

	// Connect to the database with configured parameters.
	db, err := sql.Open(Conf.Database.DriverName,
		Conf.Database.Resource)
	if err != nil {
		l.Fatalf("Could not connect to database: %s", err)
	}
	// Wrap the *sql.DB type.
	Db = DB{
		DB:       db,
		ReadOnly: (*fReadOnly || Conf.Database.ReadOnly),
	}
	l.Debug("Connected to database\n")
	if Db.ReadOnly {
		l.Debug("Database is read only\n")
	}

	// Initialize the database with all of its tables.
	err = Db.InitializeTables()
	if err != nil {
		l.Fatalf("Could not initialize database: %s", err)
	}
	l.Debug("Initialized database\n")
	l.Infof("Nodes: %d (%d local)\n", Db.LenNodes(true), Db.LenNodes(false))

	// Start the HTTP server.
	err = StartServer(Conf.Addr, Conf.Prefix)
	if err != nil {
		l.Fatalf("Server crashed: %s", err)
	}
}