Exemple #1
0
// Setup is the init function of Caddy plugins and it configures the whole
// middleware thing.
func Setup(c *setup.Controller) (middleware.Middleware, error) {
	config, _ := config.ParseHugo(c)

	// Checks if there is an Hugo website in the path that is provided.
	// If not, a new website will be created.
	create := false

	if _, err := os.Stat(config.Path + "config.yaml"); os.IsNotExist(err) {
		create = true
	}

	if _, err := os.Stat(config.Path + "config.json"); os.IsNotExist(err) {
		create = true
	}

	if _, err := os.Stat(config.Path + "config.toml"); os.IsNotExist(err) {
		create = true
	}

	if create {
		cmd := &cobra.Command{}
		cmd.Flags().Bool("force", true, "")
		commands.NewSite(cmd, []string{config.Path})
	}

	// Generates the Hugo website for the first time the plugin is activated.
	utils.Run(config)

	return func(next middleware.Handler) middleware.Handler {
		return &CaddyHugo{Next: next, Config: config}
	}, nil
}
Exemple #2
0
// Setup configures the middleware
func Setup(c *setup.Controller) (middleware.Middleware, error) {
	config, _ := config.ParseHugo(c)
	utils.Run(config)

	return func(next middleware.Handler) middleware.Handler {
		return &CaddyHugo{Next: next, Config: config}
	}, nil
}
Exemple #3
0
func parseCompleteFile(r *http.Request, c *config.Config, rawFile map[string]interface{}, filename string) ([]byte, int, error) {
	// The main content of the file
	mainContent := rawFile["content"].(string)
	mainContent = "\n\n" + strings.TrimSpace(mainContent) + "\n"

	// Removes the main content from the rest of the frontmatter
	delete(rawFile, "content")

	// Schedule the post
	if r.Header.Get("X-Schedule") == "true" {
		t, err := time.Parse("2006-01-02 15:04:05-07:00", rawFile["date"].(string))

		if err != nil {
			return []byte{}, http.StatusInternalServerError, err
		}

		scheduler := cron.New()
		scheduler.AddFunc(t.In(time.Now().Location()).Format("05 04 15 02 01 *"), func() {
			// Set draft to false
			rawFile["draft"] = false

			// Converts the frontmatter in JSON
			jsonFrontmatter, err := json.Marshal(rawFile)

			if err != nil {
				return
			}

			// Indents the json
			frontMatterBuffer := new(bytes.Buffer)
			json.Indent(frontMatterBuffer, jsonFrontmatter, "", "  ")

			// Generates the final file
			f := new(bytes.Buffer)
			f.Write(frontMatterBuffer.Bytes())
			f.Write([]byte(mainContent))
			file := f.Bytes()

			// Write the file
			err = ioutil.WriteFile(filename, file, 0666)

			if err != nil {
				return
			}

			utils.Run(c)
		})
		scheduler.Start()
	}

	// Converts the frontmatter in JSON
	jsonFrontmatter, err := json.Marshal(rawFile)

	if err != nil {
		return []byte{}, http.StatusInternalServerError, err
	}

	// Indents the json
	frontMatterBuffer := new(bytes.Buffer)
	json.Indent(frontMatterBuffer, jsonFrontmatter, "", "  ")

	// Generates the final file
	f := new(bytes.Buffer)
	f.Write(frontMatterBuffer.Bytes())
	f.Write([]byte(mainContent))
	return f.Bytes(), http.StatusOK, nil
}
Exemple #4
0
func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
	// Only handle /admin path
	if middleware.Path(r.URL.Path).Matches("/admin") {
		var err error
		var page string
		code := 404

		// If the length of the components string is less than one, the variable
		// page will always be "admin"
		if len(utils.ParseComponents(r)) > 1 {
			page = utils.ParseComponents(r)[1]
		} else {
			page = utils.ParseComponents(r)[0]
		}

		// If the page isn't "assets" neither "edit", it should always put a
		// trailing slash in the path
		if page != "assets" && page != "edit" {
			if r.URL.Path[len(r.URL.Path)-1] != '/' {
				http.Redirect(w, r, r.URL.Path+"/", http.StatusTemporaryRedirect)
				return 0, nil
			}
		}

		// If the current page is only "/admin/", redirect to "/admin/browse/content/"
		if r.URL.Path == "/admin/" {
			http.Redirect(w, r, "/admin/browse/content/", http.StatusTemporaryRedirect)
			return 0, nil
		}

		// If the url matches exactly with /admin/settings/ serve that page
		// page variable isn't used here to avoid people using URLs like
		// "/admin/settings/something".
		if r.URL.Path == "/admin/settings/" {
			var frontmatter string

			if _, err := os.Stat(h.Config.Path + "config.yaml"); err == nil {
				frontmatter = "yaml"
			}

			if _, err := os.Stat(h.Config.Path + "config.json"); err == nil {
				frontmatter = "json"
			}

			if _, err := os.Stat(h.Config.Path + "config.toml"); err == nil {
				frontmatter = "toml"
			}

			http.Redirect(w, r, "/admin/edit/config."+frontmatter, http.StatusTemporaryRedirect)
			return 0, nil
		}

		// Serve the static assets
		if page == "assets" {
			return serveAssets(w, r)
		}

		// Browse page
		if page == "browse" {
			code, err = browse.ServeHTTP(w, r, h.Config)
		}

		// Edit page
		if page == "edit" {
			code, err = editor.ServeHTTP(w, r, h.Config)
		}

		// Whenever the header "X-Refenerate" is true, the website should be
		// regenerated. Used in edit and settings, for example.
		if r.Header.Get("X-Regenerate") == "true" {
			utils.Run(h.Config)
		}

		return code, err
	}

	return h.Next.ServeHTTP(w, r)
}