Пример #1
0
func (self *Server) Initialize() error {
	if self.LogLevel != `` {
		util.ParseLogLevel(self.LogLevel)
	}

	if data, err := ioutil.ReadFile(self.ConfigPath); err == nil {
		if config, err := LoadConfig(data); err == nil {
			self.Config = config

			if v := self.Config.Options.DefaultEngine; v != `` {
				DefaultTemplateEngine = v
			}

			for name, binding := range self.Config.Bindings {
				if err := binding.Initialize(name); err != nil {
					return fmt.Errorf("Failed to initilize binding '%s': %v", name, err)
				}
			}

			if err := self.InitializeMounts(config.Mounts); err != nil {
				return fmt.Errorf("Failed to initialize mounts: %v", err)
			}
		} else {
			return fmt.Errorf("Cannot load configuration at %s: %v", self.ConfigPath, err)
		}
	}

	self.MountProxy.Fallback = http.Dir(self.StaticPath)

	return nil
}
Пример #2
0
func (self *Server) Initialize() error {
	if self.LogLevel != `` {
		util.ParseLogLevel(self.LogLevel)
	}

	if data, err := ioutil.ReadFile(self.ConfigPath); err == nil {
		if config, err := LoadConfig(data); err == nil {
			if err := self.PopulateBindings(config.Bindings); err != nil {
				return fmt.Errorf("Cannot populate bindings: %v", err)
			}

		} else {
			return fmt.Errorf("Cannot load bindings.yml: %v", err)
		}
	}

	if err := self.LoadTemplates(); err != nil {
		return err
	}

	return nil
}
Пример #3
0
func main() {
	app := cli.NewApp()
	app.Name = util.ApplicationName
	app.Usage = util.ApplicationSummary
	app.Version = util.ApplicationVersion
	app.EnableBashCompletion = false

	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name:   `log-level, L`,
			Usage:  `Level of log output verbosity`,
			Value:  `info`,
			EnvVar: `LOGLEVEL`,
		},
		cli.StringFlag{
			Name:   `config-file, c`,
			Usage:  `Path to the configuration file to use`,
			Value:  diecast.DEFAULT_CONFIG_PATH,
			EnvVar: `CONFIG_FILE`,
		},
		cli.StringFlag{
			Name:   `address, a`,
			Usage:  `Address the HTTP server should listen on`,
			Value:  diecast.DEFAULT_SERVE_ADDRESS,
			EnvVar: `HTTP_ADDR`,
		},
		cli.IntFlag{
			Name:   `port, p`,
			Usage:  `TCP port the HTTP server should listen on`,
			Value:  diecast.DEFAULT_SERVE_PORT,
			EnvVar: `HTTP_PORT`,
		},
		cli.StringFlag{
			Name:   `templates-dir, T`,
			Usage:  `Root path where templates are stored`,
			Value:  engines.DEFAULT_TEMPLATE_PATH,
			EnvVar: `TEMPLATES_DIR`,
		},
		cli.StringFlag{
			Name:   `static-dir, S`,
			Usage:  `Path where static assets are located`,
			Value:  diecast.DEFAULT_STATIC_PATH,
			EnvVar: `STATIC_PATH`,
		},
		cli.StringFlag{
			Name:   `route-prefix`,
			Usage:  `The path prepended to all HTTP requests`,
			Value:  diecast.DEFAULT_ROUTE_PREFIX,
			EnvVar: `ROUTE_PREFIX`,
		},
	}

	app.Before = func(c *cli.Context) error {
		util.ParseLogLevel(c.String(`log-level`))

		log.Infof("%s v%s started at %s", util.ApplicationName, util.ApplicationVersion, util.StartedAt)

		return nil
	}

	app.Action = func(c *cli.Context) {
		server := diecast.NewServer()

		server.Address = c.String(`address`)
		server.Port = c.Int(`port`)
		server.ConfigPath = c.String(`config-file`)
		server.StaticPath = c.String(`static-dir`)
		server.TemplatePath = c.String(`templates-dir`)
		server.RoutePrefix = c.String(`route-prefix`)

		if err := server.Initialize(); err == nil {
			log.Infof("Starting HTTP server at http://%s:%d", server.Address, server.Port)
			if err := server.Serve(); err != nil {
				log.Fatalf("Cannot start server: %v", err)
			}
		} else {
			log.Fatalf("Failed to start HTTP server: %v", err)
		}
	}

	app.Run(os.Args)
}