mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-25 11:22:16 +00:00
Clean api code
This commit is contained in:
parent
5f653898f3
commit
c1eb4d894a
20 changed files with 277 additions and 121 deletions
89
modules/auth/apiv1/miscellaneous.go
Normal file
89
modules/auth/apiv1/miscellaneous.go
Normal file
|
@ -0,0 +1,89 @@
|
|||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package apiv1
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"github.com/go-martini/martini"
|
||||
|
||||
"github.com/gogits/gogs/modules/auth"
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
"github.com/gogits/gogs/modules/log"
|
||||
"github.com/gogits/gogs/modules/middleware/binding"
|
||||
)
|
||||
|
||||
type MarkdownForm struct {
|
||||
Text string `form:"text" binding:"Required"`
|
||||
Mode string `form:"mode"`
|
||||
Context string `form:"context"`
|
||||
}
|
||||
|
||||
func (f *MarkdownForm) Name(field string) string {
|
||||
names := map[string]string{
|
||||
"Text": "text",
|
||||
}
|
||||
return names[field]
|
||||
}
|
||||
|
||||
func (f *MarkdownForm) Validate(errs *binding.BindingErrors, req *http.Request, ctx martini.Context) {
|
||||
data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
|
||||
validateApiReq(errs, data, f)
|
||||
}
|
||||
|
||||
func validateApiReq(errs *binding.BindingErrors, data base.TmplData, f auth.Form) {
|
||||
if errs.Count() == 0 {
|
||||
return
|
||||
} else if len(errs.Overall) > 0 {
|
||||
for _, err := range errs.Overall {
|
||||
log.Error("%s: %v", reflect.TypeOf(f), err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
data["HasError"] = true
|
||||
|
||||
typ := reflect.TypeOf(f)
|
||||
val := reflect.ValueOf(f)
|
||||
|
||||
if typ.Kind() == reflect.Ptr {
|
||||
typ = typ.Elem()
|
||||
val = val.Elem()
|
||||
}
|
||||
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
field := typ.Field(i)
|
||||
|
||||
fieldName := field.Tag.Get("form")
|
||||
// Allow ignored fields in the struct
|
||||
if fieldName == "-" {
|
||||
continue
|
||||
}
|
||||
|
||||
if err, ok := errs.Fields[field.Name]; ok {
|
||||
data["Err_"+field.Name] = true
|
||||
switch err {
|
||||
case binding.BindingRequireError:
|
||||
data["ErrorMsg"] = f.Name(field.Name) + " cannot be empty"
|
||||
case binding.BindingAlphaDashError:
|
||||
data["ErrorMsg"] = f.Name(field.Name) + " must be valid alpha or numeric or dash(-_) characters"
|
||||
case binding.BindingAlphaDashDotError:
|
||||
data["ErrorMsg"] = f.Name(field.Name) + " must be valid alpha or numeric or dash(-_) or dot characters"
|
||||
case binding.BindingMinSizeError:
|
||||
data["ErrorMsg"] = f.Name(field.Name) + " must contain at least " + auth.GetMinMaxSize(field) + " characters"
|
||||
case binding.BindingMaxSizeError:
|
||||
data["ErrorMsg"] = f.Name(field.Name) + " must contain at most " + auth.GetMinMaxSize(field) + " characters"
|
||||
case binding.BindingEmailError:
|
||||
data["ErrorMsg"] = f.Name(field.Name) + " is not a valid e-mail address"
|
||||
case binding.BindingUrlError:
|
||||
data["ErrorMsg"] = f.Name(field.Name) + " is not a valid URL"
|
||||
default:
|
||||
data["ErrorMsg"] = "Unknown error: " + err
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
|
@ -91,7 +91,7 @@ func (f *LogInForm) Validate(errors *binding.BindingErrors, req *http.Request, c
|
|||
validate(errors, data, f)
|
||||
}
|
||||
|
||||
func getMinMaxSize(field reflect.StructField) string {
|
||||
func GetMinMaxSize(field reflect.StructField) string {
|
||||
for _, rule := range strings.Split(field.Tag.Get("binding"), ";") {
|
||||
if strings.HasPrefix(rule, "MinSize(") || strings.HasPrefix(rule, "MaxSize(") {
|
||||
return rule[8 : len(rule)-1]
|
||||
|
@ -128,9 +128,9 @@ func validate(errors *binding.BindingErrors, data base.TmplData, form Form) {
|
|||
case binding.BindingAlphaDashDotError:
|
||||
data["ErrorMsg"] = form.Name(field.Name) + " must be valid alpha or numeric or dash(-_) or dot characters"
|
||||
case binding.BindingMinSizeError:
|
||||
data["ErrorMsg"] = form.Name(field.Name) + " must contain at least " + getMinMaxSize(field) + " characters"
|
||||
data["ErrorMsg"] = form.Name(field.Name) + " must contain at least " + GetMinMaxSize(field) + " characters"
|
||||
case binding.BindingMaxSizeError:
|
||||
data["ErrorMsg"] = form.Name(field.Name) + " must contain at most " + getMinMaxSize(field) + " characters"
|
||||
data["ErrorMsg"] = form.Name(field.Name) + " must contain at most " + GetMinMaxSize(field) + " characters"
|
||||
case binding.BindingEmailError:
|
||||
data["ErrorMsg"] = form.Name(field.Name) + " is not a valid e-mail address"
|
||||
case binding.BindingUrlError:
|
||||
|
|
|
@ -7,6 +7,11 @@ package base
|
|||
type (
|
||||
// Type TmplData represents data in the templates.
|
||||
TmplData map[string]interface{}
|
||||
|
||||
ApiJsonErr struct {
|
||||
Message string `json:"message"`
|
||||
DocUrl string `json:"documentation_url"`
|
||||
}
|
||||
)
|
||||
|
||||
var GoGetMetas = make(map[string]bool)
|
||||
|
|
|
@ -132,9 +132,7 @@ func RenderSpecialLink(rawBytes []byte, urlPrefix string) []byte {
|
|||
return rawBytes
|
||||
}
|
||||
|
||||
func RenderMarkdown(rawBytes []byte, urlPrefix string) []byte {
|
||||
body := RenderSpecialLink(rawBytes, urlPrefix)
|
||||
// fmt.Println(string(body))
|
||||
func RenderRawMarkdown(body []byte, urlPrefix string) []byte {
|
||||
htmlFlags := 0
|
||||
// htmlFlags |= gfm.HTML_USE_XHTML
|
||||
// htmlFlags |= gfm.HTML_USE_SMARTYPANTS
|
||||
|
@ -163,7 +161,12 @@ func RenderMarkdown(rawBytes []byte, urlPrefix string) []byte {
|
|||
extensions |= gfm.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
|
||||
|
||||
body = gfm.Markdown(body, renderer, extensions)
|
||||
// fmt.Println(string(body))
|
||||
return body
|
||||
}
|
||||
|
||||
func RenderMarkdown(rawBytes []byte, urlPrefix string) []byte {
|
||||
body := RenderSpecialLink(rawBytes, urlPrefix)
|
||||
body = RenderRawMarkdown(body, urlPrefix)
|
||||
return body
|
||||
}
|
||||
|
||||
|
|
|
@ -21,23 +21,21 @@ type ToggleOptions struct {
|
|||
|
||||
func Toggle(options *ToggleOptions) martini.Handler {
|
||||
return func(ctx *Context) {
|
||||
// Cannot view any page before installation.
|
||||
if !base.InstallLock {
|
||||
ctx.Redirect("/install")
|
||||
return
|
||||
}
|
||||
|
||||
// Redirect to dashboard if user tries to visit any non-login page.
|
||||
if options.SignOutRequire && ctx.IsSigned && ctx.Req.RequestURI != "/" {
|
||||
ctx.Redirect("/")
|
||||
return
|
||||
}
|
||||
|
||||
if !options.DisableCsrf {
|
||||
if ctx.Req.Method == "POST" {
|
||||
if !ctx.CsrfTokenValid() {
|
||||
ctx.Error(403, "CSRF token does not match")
|
||||
return
|
||||
}
|
||||
}
|
||||
if !options.DisableCsrf && ctx.Req.Method == "POST" && !ctx.CsrfTokenValid() {
|
||||
ctx.Error(403, "CSRF token does not match")
|
||||
return
|
||||
}
|
||||
|
||||
if options.SignInRequire {
|
||||
|
|
|
@ -78,6 +78,19 @@ func (ctx *Context) Query(name string) string {
|
|||
// return ctx.p[name]
|
||||
// }
|
||||
|
||||
// HasError returns true if error occurs in form validation.
|
||||
func (ctx *Context) HasApiError() bool {
|
||||
hasErr, ok := ctx.Data["HasError"]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return hasErr.(bool)
|
||||
}
|
||||
|
||||
func (ctx *Context) GetErrMsg() string {
|
||||
return ctx.Data["ErrorMsg"].(string)
|
||||
}
|
||||
|
||||
// HasError returns true if error occurs in form validation.
|
||||
func (ctx *Context) HasError() bool {
|
||||
hasErr, ok := ctx.Data["HasError"]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue