Ejemplo n.º 1
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
	}
}
Ejemplo n.º 2
0
func deployPushCommand(ctx *cli.Context) {
	workdir := ctx.GlobalString("workdir")
	if workdir == "" {
		fmt.Println("unknown working directory, please use -w to provide.")
		os.Exit(1)
	}

	// directory
	absworkdir, err := filepath.Abs(workdir)
	if err != nil {
		fmt.Println("workdir:", err)
		return
	}

	dirs := ctx.Args()
	if len(dirs) == 0 {
		dirs = append(dirs, "h2object.conf:h2object:third", "markdowns", "templates", "statics")
	}

	h2oconf, err := app.LoadCONFIG(path.Join(absworkdir, "h2object.conf"))
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	h2oconf.SetSection("deploy")
	Host := h2oconf.StringDefault("host", "")
	Port := h2oconf.IntDefault("port", 80)
	AppID := h2oconf.StringDefault("appid", "")
	Secret := h2oconf.StringDefault("secret", "")

	h2oconf.SetSection("h2object")
	markdown_suffixs := h2oconf.MultiStringDefault("markdown.suffix", ",", []string{"md", "markdown"})

	client := api.NewClient(Host, Port)
	auth := api.NewAdminAuth(AppID, Secret)
	
	for _, directory := range dirs {
		if strings.HasPrefix(directory, "markdowns") {

			size := util.FolderSize(path.Join(absworkdir, directory), []string{})

			bar := pb.New(int(size)).SetUnits(pb.U_BYTES)
			bar.Prefix(directory + " ")
			bar.Start()

			if err := push(client, auth, bar, absworkdir, path.Join(absworkdir, directory), []string{}); err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
			bar.FinishPrint(fmt.Sprintf("%s push completed.", directory))
			continue
		}

		if strings.HasPrefix(directory, "templates") {

			size := util.FolderSize(path.Join(absworkdir, directory), []string{})

			bar := pb.New(int(size)).SetUnits(pb.U_BYTES)

			bar.Prefix(directory + " ")
			bar.Start()

			if err := push(client, auth, bar, absworkdir, path.Join(absworkdir, directory), []string{}); err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
			bar.FinishPrint(fmt.Sprintf("%s push completed.", directory))
			continue
		}

		if strings.HasPrefix(directory, "statics") {
			size := util.FolderSize(path.Join(absworkdir, directory), markdown_suffixs)
			bar := pb.New(int(size)).SetUnits(pb.U_BYTES)
			bar.Prefix(directory + " ")
			bar.Start()

			if err := push(client, auth, bar, absworkdir, path.Join(absworkdir, directory), markdown_suffixs); err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
			bar.FinishPrint(fmt.Sprintf("%s push completed.", directory))
			continue
		}

		if strings.HasPrefix(directory, "h2object.conf") {
			ds := strings.Split(directory, ":")
			
			sections := []string{}
			if len(ds) > 1 {
				sections = append(sections, ds[1:]...)
			} else {
				sections = append(sections, "h2object", "third")
			}

			for _, section := range sections {
				if h2oconf.HasSection(section) {
					h2oconf.SetSection(section)
					opts := h2oconf.Options("")

					data := map[string]string{}
					for _, opt := range opts {
						if section == "h2object" && (opt == "appid" || opt == "secret") {
							continue
						}
						data[opt] = h2oconf.StringDefault(opt, "")
					}

					if err := client.SetConfig(nil, auth, section, data); err != nil {
						fmt.Println(err)
						os.Exit(1)
					}
					fmt.Printf("section (%s) push succeed.\n", section)
				}
			}
			continue
		}

		fmt.Printf("push path ignored: %s\n", directory)
	}
}