Exemplo n.º 1
0
func (app *Application) Init() error {
	httpAddr, err := net.ResolveTCPAddr("tcp", app.Options.HTTPAddress)
	if err != nil {
		return err
	}
	app.httpAddr = httpAddr

	httpListener, err := net.Listen("tcp", app.httpAddr.String())
	if err != nil {
		return err
	}
	app.httpListener = httpListener

	apiAddr, err := net.ResolveTCPAddr("tcp", app.Options.APIAddress)
	if err != nil {
		return err
	}
	app.apiAddr = apiAddr

	apiListener, err := net.Listen("tcp", app.apiAddr.String())
	if err != nil {
		return err
	}
	app.apiListener = apiListener

	vhosts := NewVirtualHost(app.Options.Root, app.Logger)
	if err := vhosts.Load(); err != nil {
		return err
	}
	app.vhosts = vhosts

	forwarder, err := forward.New(forward.Logger(app.Logger))
	if err != nil {
		return err
	}
	app.forwarder = forwarder

	reverser, err := reverse.New(app.forwarder, reverse.Route(app.vhosts), reverse.Logger(app.Logger))
	if err != nil {
		return err
	}
	app.reverser = reverser

	app.api = NewAPIController(app.vhosts)
	appid_dft, _ := util.AlphaStringRange(24, 32)
	secret_dft, _ := util.AlphaStringRange(32, 36)

	app.Configs.SetSection("forward")
	appid := app.Configs.StringDefault("appid", appid_dft)
	secret := app.Configs.StringDefault("secret", secret_dft)
	acl := app.Configs.BoolDefault("acl.enable", true)
	signature := app.api.sign(secret, appid)
	app.Info("application signature (%s)", signature)
	app.api.acl(acl)

	if err := app.Configs.Save(""); err != nil {
		return err
	}
	return nil
}
Exemplo n.º 2
0
func (ctx *context) load() {
	ctx.Lock()
	defer ctx.Unlock()

	// clear handlers
	for _, suffix := range ctx.markdowns {
		delete(handlers, suffix)
	}
	for _, suffix := range ctx.templates {
		delete(handlers, suffix)
	}
	delete(handlers, "page")
	delete(handlers, "system")

	// load
	conf := ctx.app.Configs
	conf.SetSection("h2object")

	ctx.site_name = conf.StringDefault("site.name", "")
	ctx.site_description = conf.StringDefault("site.description", "")
	ctx.site_author = conf.StringDefault("site.author", "h2object")
	ctx.site_contact = conf.StringDefault("site.author", "*****@*****.**")

	appid_dft, _ := util.AlphaStringRange(24, 32)
	secret_dft, _ := util.AlphaStringRange(32, 36)
	appid := conf.StringDefault("appid", appid_dft)
	secret := conf.StringDefault("secret", secret_dft)

	ctx.host = conf.StringDefault("host", ctx.app.Options.HTTPAddress)
	ctx.index = conf.StringDefault("index", "")
	ctx.signature = util.SignString(secret, appid)
	ctx.markdowns = conf.MultiStringDefault("markdown.suffix", ",", []string{"md", "markdown"})
	ctx.templates = conf.MultiStringDefault("template.suffix", ",", []string{"html", "htm", "tpl"})
	ctx.cache_duration = conf.DurationDefault("markdown.cache", 10*time.Minute)
	ctx.devmode = conf.BoolDefault("develope.mode", false)

	ctx.storage = uint64(util.FolderSize(ctx.app.Options.Root, nil))
	ctx.storage_max = ctx.app.Options.StorageMax
	if ctx.storage_max > 0 {
		ctx.Info("application storage current size (%s), max size (%s)",
			humanize.IBytes(ctx.storage), humanize.IBytes(ctx.storage_max))
	}

	// reset handlers
	for _, suffix := range ctx.markdowns {
		handlers[suffix] = do_markdown
	}
	for _, suffix := range ctx.templates {
		handlers[suffix] = do_template
	}
	if ctx.devmode {
		handlers["page"] = do_page
		handlers["click"] = do_click
		handlers["system"] = do_system
	}
}
Exemplo n.º 3
0
func DefaultCONFIG() *CONFIG {
	default_config = NewCONFIG()
	default_config.config.AddOption("theme", "provider", "h2object")
	default_config.config.AddOption("theme", "name", "demo")
	default_config.config.AddOption("theme", "public", "false")
	default_config.config.AddOption("theme", "catagory", "1")
	default_config.config.AddOption("theme", "version", "0.0.1")
	default_config.config.AddOption("theme", "description", "h2object demo site")

	default_config.config.AddOption("deploy", "host", "h2object.io")
	default_config.config.AddOption("deploy", "port", "80")
	default_config.config.AddOption("deploy", "appid", "")
	default_config.config.AddOption("deploy", "secret", "")

	default_config.config.AddOption("h2object", "site.name", "")
	default_config.config.AddOption("h2object", "site.author", "h2object")
	default_config.config.AddOption("h2object", "site.contact", "*****@*****.**")
	default_config.config.AddOption("h2object", "site.description", "")

	appid, _ := util.AlphaStringRange(24, 32)
	secret, _ := util.AlphaStringRange(32, 36)

	default_config.config.AddOption("h2object", "appid", appid)
	default_config.config.AddOption("h2object", "secret", secret)
	default_config.config.AddOption("h2object", "host", "")
	default_config.config.AddOption("h2object", "index", "")

	default_config.config.AddOption("h2object", "markdown.cache", "10m")
	default_config.config.AddOption("h2object", "markdown.suffix", "md,markdown")
	default_config.config.AddOption("h2object", "template.suffix", "html,htm,tpl")
	default_config.config.AddOption("h2object", "develope.mode", "false")

	default_config.config.AddOption("third", "qiniu.enable", "false")
	default_config.config.AddOption("third", "qiniu.appid", "")
	default_config.config.AddOption("third", "qiniu.secret", "")
	default_config.config.AddOption("third", "qiniu.domain", "")
	default_config.config.AddOption("third", "qiniu.bucket", "")

	default_config.config.AddOption("logs", "file.enable", "false")
	default_config.config.AddOption("logs", "file.name", "h2o.log")
	default_config.config.AddOption("logs", "file.level", "info")
	default_config.config.AddOption("logs", "file.rotate_max_size", "")
	default_config.config.AddOption("logs", "file.rotate_max_line", "")
	default_config.config.AddOption("logs", "file.rotate_daily", "true")

	return default_config
}
Exemplo n.º 4
0
func DefaultCONFIG() *CONFIG {
	default_config = NewCONFIG()

	appid, _ := util.AlphaStringRange(24, 32)
	secret, _ := util.AlphaStringRange(32, 36)

	default_config.config.AddOption("forward", "appid", appid)
	default_config.config.AddOption("forward", "secret", secret)
	default_config.config.AddOption("forward", "acl.enable", "false")

	default_config.config.AddOption("logs", "file.enable", "false")
	default_config.config.AddOption("logs", "file.name", "h2o.log")
	default_config.config.AddOption("logs", "file.level", "info")
	default_config.config.AddOption("logs", "file.rotate_max_size", "")
	default_config.config.AddOption("logs", "file.rotate_max_line", "")
	default_config.config.AddOption("logs", "file.rotate_daily", "true")
	return default_config
}