Example #1
0
func main() {
	serverconfpath := flag.String("server-config", "", "server config path")
	readeefconfpath := flag.String("readeef-config", "", "readeef config path")
	host := flag.String("host", "", "server host")
	port := flag.Int("port", 0, "server port")

	flag.Parse()

	cfg, err := readeef.ReadConfig(*readeefconfpath)
	if err != nil {
		exitWithError(fmt.Sprintf("Error reading config from path '%s': %v", *readeefconfpath, err))
	}

	server := webfw.NewServer(*serverconfpath)
	if *host != "" {
		server.SetHost(*host)
	}

	if *port > 0 {
		server.SetPort(*port)
	}

	dispatcher := server.Dispatcher("/api/")

	logger := log.New(os.Stderr, "", 0)
	readeef.InitDebug(logger, cfg)

	if err := api.RegisterControllers(cfg, dispatcher, logger); err != nil {
		exitWithError(err.Error())
	}

	dispatcher = server.Dispatcher("/")
	web.RegisterControllers(dispatcher, "/api/")

	if err := server.ListenAndServe(); err != nil {
		exitWithError(fmt.Sprintf("Error starting server: %s\n", err.Error()))
	}
}
Example #2
0
func main() {
	serverconfpath := flag.String("server-config", "", "server config path")
	readeefconfpath := flag.String("readeef-config", "", "readeef config path")
	address := flag.String("address", "", "local server network address")
	port := flag.Int("port", 0, "server port")

	flag.Parse()

	cfg, err := readeef.ReadConfig(*readeefconfpath)
	if err != nil {
		exitWithError(fmt.Sprintf("Error reading config from path '%s': %v", *readeefconfpath, err))
	}

	logger := readeef.NewLogger(cfg)
	defer func() {
		if rec := recover(); rec != nil {
			stack := debug.Stack()
			logger.Fatalf("Fatal error: %v\n%s\n", rec, stack)
		}
	}()

	server := webfw.NewServer(*serverconfpath)
	if *address != "" {
		server.Address = *address
	}

	if *port > 0 {
		server.Port = *port
	}

	var accessWriter io.Writer
	if cfg.Logger.AccessFile == "-" {
		accessWriter = os.Stdout
	} else {
		accessWriter = &lumberjack.Logger{
			Filename:   cfg.Logger.AccessFile,
			MaxSize:    20,
			MaxBackups: 5,
			MaxAge:     28,
		}
	}

	accessLogger := webfw.NewStandardLogger(accessWriter, "", 0)

	dispatcher := server.Dispatcher("/api/")
	dispatcher.Logger = logger
	dispatcher.RegisterMiddleware(middleware.Logger{AccessLogger: accessLogger})

	if err := api.RegisterControllers(cfg, dispatcher, logger); err != nil {
		exitWithError(err.Error())
	}

	dispatcher = server.Dispatcher("/")
	dispatcher.Logger = logger
	dispatcher.RegisterMiddleware(middleware.Logger{AccessLogger: accessLogger})

	web.RegisterControllers(cfg, dispatcher, "/api/")

	if err := server.ListenAndServe(); err != nil {
		exitWithError(fmt.Sprintf("Error starting server: %s\n", err.Error()))
	}
}