Exemple #1
0
func Docs(ctx *middleware.Context) {
	toc := models.Tocs[ctx.Locale.Language()]
	if toc == nil {
		toc = models.Tocs[setting.Docs.Langs[0]]
	}
	ctx.Data["Toc"] = toc

	nodeName := strings.TrimPrefix(strings.ToLower(strings.TrimSuffix(ctx.Req.URL.Path, ".html")), setting.Page.DocsBaseURL)
	node, isDefault := toc.GetDoc(nodeName)
	if node == nil {
		NotFound(ctx)
		return
	}
	if !setting.ProdMode {
		node.ReloadContent()
	}

	langVer := toc.Lang
	if isDefault {
		ctx.Data["IsShowingDefault"] = isDefault
		langVer = setting.Docs.Langs[0]
	}
	ctx.Data["Title"] = node.Title
	ctx.Data["Content"] = fmt.Sprintf(`<script type="text/javascript" src="/%s/%s?=%d"></script>`, langVer, node.DocumentPath+".js", node.LastBuildTime)

	renderEditPage(ctx, node.DocumentPath)
	ctx.HTML(200, "docs")
}
Exemple #2
0
func Pages(ctx *middleware.Context) {
	toc := models.Tocs[ctx.Locale.Language()]
	if toc == nil {
		toc = models.Tocs[setting.Docs.Langs[0]]
	}

	pageName := strings.ToLower(strings.TrimSuffix(ctx.Req.URL.Path[1:], ".html"))
	for i := range toc.Pages {
		if toc.Pages[i].Name == pageName {
			page := toc.Pages[i]
			if !com.IsFile(page.FileName) {
				ctx.Data["IsShowingDefault"] = true
				page = models.Tocs[setting.Docs.Langs[0]].Pages[i]
			}
			if !setting.ProdMode {
				page.ReloadContent()
			}

			ctx.Data["Title"] = page.Title
			ctx.Data["Content"] = fmt.Sprintf(`<script type="text/javascript" src="/%s/%s?=%d"></script>`, toc.Lang, page.DocumentPath+".js", page.LastBuildTime)
			ctx.Data["Pages"] = toc.Pages

			renderEditPage(ctx, page.DocumentPath)
			ctx.HTML(200, "docs")
			return
		}
	}

	NotFound(ctx)
}
Exemple #3
0
func Home(ctx *middleware.Context) {
	if !setting.Page.HasLandingPage {
		ctx.Redirect(setting.Page.DocsBaseURL)
		return
	}

	ctx.HTML(200, "home")
}
Exemple #4
0
func Docs(ctx *middleware.Context) {
	toc := models.Tocs[ctx.Locale.Language()]
	if toc == nil {
		toc = models.Tocs[setting.Docs.Langs[0]]
	}
	ctx.Data["Toc"] = toc

	nodeName := strings.TrimPrefix(strings.ToLower(strings.TrimSuffix(ctx.Req.URL.Path, ".html")), setting.Page.DocsBaseURL)
	title, content, isDefault := toc.GetDoc(nodeName)
	if content == nil {
		NotFound(ctx)
		return
	}
	ctx.Data["Title"] = title
	ctx.Data["Content"] = string(content)
	ctx.Data["IsShowingDefault"] = isDefault
	ctx.HTML(200, "docs")
}
Exemple #5
0
func Hook(ctx *middleware.Context) {
	if ctx.Query("secret") != setting.Docs.Secret {
		ctx.Error(403)
		return
	}

	log.Info("Incoming hook update request")
	if err := models.ReloadDocs(); err != nil {
		ctx.Error(500)
		return
	}
	ctx.Status(200)
}
Exemple #6
0
func DocsStatic(ctx *middleware.Context) {
	if len(ctx.Params("*")) > 0 {
		f, err := os.Open(path.Join(models.Tocs[setting.Docs.Langs[0]].RootPath, "images", ctx.Params("*")))
		if err != nil {
			ctx.JSON(500, map[string]interface{}{
				"error": err.Error(),
			})
			return
		}
		defer f.Close()

		_, err = io.Copy(ctx.Resp, f)
		if err != nil {
			ctx.JSON(500, map[string]interface{}{
				"error": err.Error(),
			})
			return
		}
		return
	}
	ctx.Error(404)
}
Exemple #7
0
func Pages(ctx *middleware.Context) {
	toc := models.Tocs[ctx.Locale.Language()]
	if toc == nil {
		toc = models.Tocs[setting.Docs.Langs[0]]
	}

	pageName := strings.ToLower(strings.TrimSuffix(ctx.Req.URL.Path[1:], ".html"))
	for i := range toc.Pages {
		if toc.Pages[i].Name == pageName {
			page := toc.Pages[i]
			if !com.IsFile(page.FileName) {
				ctx.Data["IsShowingDefault"] = true
				page = models.Tocs[setting.Docs.Langs[0]].Pages[i]
			}
			ctx.Data["Title"] = page.Title
			ctx.Data["Content"] = string(page.Content())
			ctx.Data["Pages"] = toc.Pages
			ctx.HTML(200, "docs")
			return
		}
	}

	NotFound(ctx)
}
Exemple #8
0
func Protect(ctx *middleware.Context) {
	if !models.Protector.HasProtection {
		return
	}

	// Check if resource is protected.
	allows, yes := models.Protector.Resources[strings.TrimPrefix(ctx.Req.URL.Path, "/docs/")]
	if !yes {
		return
	}

	// Check if auth is presented.
	authHead := ctx.Req.Header.Get("Authorization")
	if len(authHead) == 0 {
		authRequired(ctx)
		return
	}

	auths := strings.Fields(authHead)
	if len(auths) != 2 || auths[0] != "Basic" {
		ctx.Error(401)
		return
	}

	uname, passwd, err := basicAuthDecode(auths[1])
	if err != nil {
		ctx.Error(401)
		return
	}

	// Check if auth is valid.
	if models.Protector.Users[uname] != encodeMd5(passwd) {
		ctx.Error(401)
		return
	}

	// Check if user has access to the resource.
	if !allows[uname] {
		ctx.Error(403)
		return
	}
}
Exemple #9
0
func NotFound(ctx *middleware.Context) {
	ctx.Data["Title"] = "404"
	ctx.HTML(404, "404")
}
Exemple #10
0
func authRequired(ctx *middleware.Context) {
	ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=\".\"")
	ctx.Error(401)
}