summaryrefslogtreecommitdiffstats
path: root/modules/middleware
diff options
context:
space:
mode:
Diffstat (limited to 'modules/middleware')
-rw-r--r--modules/middleware/auth.go6
-rw-r--r--modules/middleware/binding/binding.go103
-rw-r--r--modules/middleware/context.go263
-rw-r--r--modules/middleware/logger.go52
-rw-r--r--modules/middleware/render.go281
-rw-r--r--modules/middleware/repo.go99
-rw-r--r--modules/middleware/static.go127
7 files changed, 168 insertions, 763 deletions
diff --git a/modules/middleware/auth.go b/modules/middleware/auth.go
index 214dda23b9..741337da0b 100644
--- a/modules/middleware/auth.go
+++ b/modules/middleware/auth.go
@@ -8,7 +8,7 @@ import (
"net/url"
"strings"
- "github.com/go-martini/martini"
+ "github.com/Unknwon/macaron"
"github.com/gogits/gogs/modules/setting"
)
@@ -20,7 +20,7 @@ type ToggleOptions struct {
DisableCsrf bool
}
-func Toggle(options *ToggleOptions) martini.Handler {
+func Toggle(options *ToggleOptions) macaron.Handler {
return func(ctx *Context) {
// Cannot view any page before installation.
if !setting.InstallLock {
@@ -49,7 +49,7 @@ func Toggle(options *ToggleOptions) martini.Handler {
ctx.Redirect("/user/login")
return
} else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
- ctx.Data["Title"] = "Activate Your Account"
+ // ctx.Data["Title"] = "Activate Your Account"
ctx.HTML(200, "user/activate")
return
}
diff --git a/modules/middleware/binding/binding.go b/modules/middleware/binding/binding.go
index bf8ab52b2b..4103f5543f 100644
--- a/modules/middleware/binding/binding.go
+++ b/modules/middleware/binding/binding.go
@@ -16,7 +16,8 @@ import (
"strings"
"unicode/utf8"
- "github.com/go-martini/martini"
+ "github.com/Unknwon/macaron"
+ "github.com/macaron-contrib/i18n"
)
/*
@@ -37,44 +38,44 @@ import (
// your own error handling, use Form or Json middleware directly.
// An interface pointer can be added as a second argument in order
// to map the struct to a specific interface.
-func Bind(obj interface{}, ifacePtr ...interface{}) martini.Handler {
- return func(context martini.Context, req *http.Request) {
- contentType := req.Header.Get("Content-Type")
+func Bind(obj interface{}, ifacePtr ...interface{}) macaron.Handler {
+ return func(ctx *macaron.Context) {
+ contentType := ctx.Req.Header.Get("Content-Type")
if strings.Contains(contentType, "form-urlencoded") {
- context.Invoke(Form(obj, ifacePtr...))
+ ctx.Invoke(Form(obj, ifacePtr...))
} else if strings.Contains(contentType, "multipart/form-data") {
- context.Invoke(MultipartForm(obj, ifacePtr...))
+ ctx.Invoke(MultipartForm(obj, ifacePtr...))
} else if strings.Contains(contentType, "json") {
- context.Invoke(Json(obj, ifacePtr...))
+ ctx.Invoke(Json(obj, ifacePtr...))
} else {
- context.Invoke(Json(obj, ifacePtr...))
- if getErrors(context).Count() > 0 {
- context.Invoke(Form(obj, ifacePtr...))
+ ctx.Invoke(Json(obj, ifacePtr...))
+ if getErrors(ctx).Count() > 0 {
+ ctx.Invoke(Form(obj, ifacePtr...))
}
}
- context.Invoke(ErrorHandler)
+ ctx.Invoke(ErrorHandler)
}
}
// BindIgnErr will do the exactly same thing as Bind but without any
// error handling, which user has freedom to deal with them.
// This allows user take advantages of validation.
-func BindIgnErr(obj interface{}, ifacePtr ...interface{}) martini.Handler {
- return func(context martini.Context, req *http.Request) {
+func BindIgnErr(obj interface{}, ifacePtr ...interface{}) macaron.Handler {
+ return func(ctx *macaron.Context, req *http.Request) {
contentType := req.Header.Get("Content-Type")
if strings.Contains(contentType, "form-urlencoded") {
- context.Invoke(Form(obj, ifacePtr...))
+ ctx.Invoke(Form(obj, ifacePtr...))
} else if strings.Contains(contentType, "multipart/form-data") {
- context.Invoke(MultipartForm(obj, ifacePtr...))
+ ctx.Invoke(MultipartForm(obj, ifacePtr...))
} else if strings.Contains(contentType, "json") {
- context.Invoke(Json(obj, ifacePtr...))
+ ctx.Invoke(Json(obj, ifacePtr...))
} else {
- context.Invoke(Json(obj, ifacePtr...))
- if getErrors(context).Count() > 0 {
- context.Invoke(Form(obj, ifacePtr...))
+ ctx.Invoke(Json(obj, ifacePtr...))
+ if getErrors(ctx).Count() > 0 {
+ ctx.Invoke(Form(obj, ifacePtr...))
}
}
}
@@ -89,12 +90,12 @@ func BindIgnErr(obj interface{}, ifacePtr ...interface{}) martini.Handler {
// keys, for example: key=val1&key=val2&key=val3
// An interface pointer can be added as a second argument in order
// to map the struct to a specific interface.
-func Form(formStruct interface{}, ifacePtr ...interface{}) martini.Handler {
- return func(context martini.Context, req *http.Request) {
+func Form(formStruct interface{}, ifacePtr ...interface{}) macaron.Handler {
+ return func(ctx *macaron.Context) {
ensureNotPointer(formStruct)
formStruct := reflect.New(reflect.TypeOf(formStruct))
errors := newErrors()
- parseErr := req.ParseForm()
+ parseErr := ctx.Req.ParseForm()
// Format validation of the request body or the URL would add considerable overhead,
// and ParseForm does not complain when URL encoding is off.
@@ -104,14 +105,14 @@ func Form(formStruct interface{}, ifacePtr ...interface{}) martini.Handler {
errors.Overall[BindingDeserializationError] = parseErr.Error()
}
- mapForm(formStruct, req.Form, errors)
+ mapForm(formStruct, ctx.Req.Form, errors)
- validateAndMap(formStruct, context, errors, ifacePtr...)
+ validateAndMap(formStruct, ctx, errors, ifacePtr...)
}
}
-func MultipartForm(formStruct interface{}, ifacePtr ...interface{}) martini.Handler {
- return func(context martini.Context, req *http.Request) {
+func MultipartForm(formStruct interface{}, ifacePtr ...interface{}) macaron.Handler {
+ return func(ctx *macaron.Context) {
ensureNotPointer(formStruct)
formStruct := reflect.New(reflect.TypeOf(formStruct))
errors := newErrors()
@@ -119,7 +120,7 @@ func MultipartForm(formStruct interface{}, ifacePtr ...interface{}) martini.Hand
// Workaround for multipart forms returning nil instead of an error
// when content is not multipart
// https://code.google.com/p/go/issues/detail?id=6334
- multipartReader, err := req.MultipartReader()
+ multipartReader, err := ctx.Req.MultipartReader()
if err != nil {
errors.Overall[BindingDeserializationError] = err.Error()
} else {
@@ -129,12 +130,12 @@ func MultipartForm(formStruct interface{}, ifacePtr ...interface{}) martini.Hand
errors.Overall[BindingDeserializationError] = parseErr.Error()
}
- req.MultipartForm = form
+ ctx.Req.MultipartForm = form
}
- mapForm(formStruct, req.MultipartForm.Value, errors)
+ mapForm(formStruct, ctx.Req.MultipartForm.Value, errors)
- validateAndMap(formStruct, context, errors, ifacePtr...)
+ validateAndMap(formStruct, ctx, errors, ifacePtr...)
}
}
@@ -143,21 +144,21 @@ func MultipartForm(formStruct interface{}, ifacePtr ...interface{}) martini.Hand
// validated, but no error handling is actually performed here.
// An interface pointer can be added as a second argument in order
// to map the struct to a specific interface.
-func Json(jsonStruct interface{}, ifacePtr ...interface{}) martini.Handler {
- return func(context martini.Context, req *http.Request) {
+func Json(jsonStruct interface{}, ifacePtr ...interface{}) macaron.Handler {
+ return func(ctx *macaron.Context) {
ensureNotPointer(jsonStruct)
jsonStruct := reflect.New(reflect.TypeOf(jsonStruct))
errors := newErrors()
- if req.Body != nil {
- defer req.Body.Close()
+ if ctx.Req.Body != nil {
+ defer ctx.Req.Body.Close()
}
- if err := json.NewDecoder(req.Body).Decode(jsonStruct.Interface()); err != nil && err != io.EOF {
+ if err := json.NewDecoder(ctx.Req.Body).Decode(jsonStruct.Interface()); err != nil && err != io.EOF {
errors.Overall[BindingDeserializationError] = err.Error()
}
- validateAndMap(jsonStruct, context, errors, ifacePtr...)
+ validateAndMap(jsonStruct, ctx, errors, ifacePtr...)
}
}
@@ -165,15 +166,15 @@ func Json(jsonStruct interface{}, ifacePtr ...interface{}) martini.Handler {
// passed in is a Validator, then the user-defined Validate method
// is executed, and its errors are mapped to the context. This middleware
// performs no error handling: it merely detects them and maps them.
-func Validate(obj interface{}) martini.Handler {
- return func(context martini.Context, req *http.Request) {
+func Validate(obj interface{}) macaron.Handler {
+ return func(ctx *macaron.Context, l i18n.Locale) {
errors := newErrors()
validateStruct(errors, obj)
if validator, ok := obj.(Validator); ok {
- validator.Validate(errors, req, context)
+ validator.Validate(ctx, errors, l)
}
- context.Map(*errors)
+ ctx.Map(*errors)
}
}
@@ -387,9 +388,7 @@ func setWithProperType(valueKind reflect.Kind, val string, structField reflect.V
}
}
-// Don't pass in pointers to bind to. Can lead to bugs. See:
-// https://github.com/codegangsta/martini-contrib/issues/40
-// https://github.com/codegangsta/martini-contrib/pull/34#issuecomment-29683659
+// Don't pass in pointers to bind to. Can lead to bugs.
func ensureNotPointer(obj interface{}) {
if reflect.TypeOf(obj).Kind() == reflect.Ptr {
panic("Pointers are not accepted as binding models")
@@ -399,13 +398,13 @@ func ensureNotPointer(obj interface{}) {
// Performs validation and combines errors from validation
// with errors from deserialization, then maps both the
// resulting struct and the errors to the context.
-func validateAndMap(obj reflect.Value, context martini.Context, errors *Errors, ifacePtr ...interface{}) {
- context.Invoke(Validate(obj.Interface()))
- errors.Combine(getErrors(context))
- context.Map(*errors)
- context.Map(obj.Elem().Interface())
+func validateAndMap(obj reflect.Value, ctx *macaron.Context, errors *Errors, ifacePtr ...interface{}) {
+ ctx.Invoke(Validate(obj.Interface()))
+ errors.Combine(getErrors(ctx))
+ ctx.Map(*errors)
+ ctx.Map(obj.Elem().Interface())
if len(ifacePtr) > 0 {
- context.MapTo(obj.Elem().Interface(), ifacePtr[0])
+ ctx.MapTo(obj.Elem().Interface(), ifacePtr[0])
}
}
@@ -413,8 +412,8 @@ func newErrors() *Errors {
return &Errors{make(map[string]string), make(map[string]string)}
}
-func getErrors(context martini.Context) Errors {
- return context.Get(reflect.TypeOf(Errors{})).Interface().(Errors)
+func getErrors(ctx *macaron.Context) Errors {
+ return ctx.GetVal(reflect.TypeOf(Errors{})).Interface().(Errors)
}
type (
@@ -422,7 +421,7 @@ type (
// validation before the request even gets to your application.
// The Validate method will be executed during the validation phase.
Validator interface {
- Validate(*Errors, *http.Request, martini.Context)
+ Validate(*macaron.Context, *Errors, i18n.Locale)
}
)
diff --git a/modules/middleware/context.go b/modules/middleware/context.go
index cf849802d9..463d9ba980 100644
--- a/modules/middleware/context.go
+++ b/modules/middleware/context.go
@@ -5,42 +5,33 @@
package middleware
import (
- "crypto/hmac"
- "crypto/sha1"
- "encoding/base64"
"fmt"
"html/template"
"io"
"net/http"
- "net/url"
- "path/filepath"
- "strconv"
+ "path"
"strings"
"time"
- "github.com/go-martini/martini"
-
- "github.com/gogits/cache"
- "github.com/gogits/git"
- "github.com/gogits/session"
+ "github.com/Unknwon/macaron"
+ "github.com/macaron-contrib/i18n"
+ "github.com/macaron-contrib/session"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/base"
+ "github.com/gogits/gogs/modules/git"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/setting"
)
// Context represents context of a request.
type Context struct {
- *Render
- c martini.Context
- p martini.Params
- Req *http.Request
- Res http.ResponseWriter
- Flash *Flash
- Session session.SessionStore
- Cache cache.Cache
+ *macaron.Context
+ i18n.Locale
+ Flash *session.Flash
+ Session session.Store
+
User *models.User
IsSigned bool
@@ -68,7 +59,8 @@ type Context struct {
HTTPS string
Git string
}
- Mirror *models.Mirror
+ CommitsCount int
+ Mirror *models.Mirror
}
}
@@ -107,12 +99,12 @@ func (ctx *Context) HasError() bool {
}
// HTML calls render.HTML underlying but reduce one argument.
-func (ctx *Context) HTML(status int, name base.TplName, htmlOpt ...HTMLOptions) {
- ctx.Render.HTML(status, string(name), ctx.Data, htmlOpt...)
+func (ctx *Context) HTML(status int, name base.TplName) {
+ ctx.Render.HTML(status, string(name), ctx.Data)
}
// RenderWithErr used for page has form validation but need to prompt error to users.
-func (ctx *Context) RenderWithErr(msg string, tpl base.TplName, form auth.Form) {
+func (ctx *Context) RenderWithErr(msg string, tpl base.TplName, form interface{}) {
if form != nil {
auth.AssignForm(form, ctx.Data)
}
@@ -124,8 +116,8 @@ func (ctx *Context) RenderWithErr(msg string, tpl base.TplName, form auth.Form)
// Handle handles and logs error by given status.
func (ctx *Context) Handle(status int, title string, err error) {
if err != nil {
- log.Error("%s: %v", title, err)
- if martini.Dev != martini.Prod {
+ log.Error(4, "%s: %v", title, err)
+ if macaron.Env != macaron.PROD {
ctx.Data["ErrorMsg"] = err
}
}
@@ -139,106 +131,6 @@ func (ctx *Context) Handle(status int, title string, err error) {
ctx.HTML(status, base.TplName(fmt.Sprintf("status/%d", status)))
}
-func (ctx *Context) GetCookie(name string) string {
- cookie, err := ctx.Req.Cookie(name)
- if err != nil {
- return ""
- }
- return cookie.Value
-}
-
-func (ctx *Context) SetCookie(name string, value string, others ...interface{}) {
- cookie := http.Cookie{}
- cookie.Name = name
- cookie.Value = value
-
- if len(others) > 0 {
- switch v := others[0].(type) {
- case int:
- cookie.MaxAge = v
- case int64:
- cookie.MaxAge = int(v)
- case int32:
- cookie.MaxAge = int(v)
- }
- }
-
- // default "/"
- if len(others) > 1 {
- if v, ok := others[1].(string); ok && len(v) > 0 {
- cookie.Path = v
- }
- } else {
- cookie.Path = "/"
- }
-
- // default empty
- if len(others) > 2 {
- if v, ok := others[2].(string); ok && len(v) > 0 {
- cookie.Domain = v
- }
- }
-
- // default empty
- if len(others) > 3 {
- switch v := others[3].(type) {
- case bool:
- cookie.Secure = v
- default:
- if others[3] != nil {
- cookie.Secure = true
- }
- }
- }
-
- // default false. for session cookie default true
- if len(others) > 4 {
- if v, ok := others[4].(bool); ok && v {
- cookie.HttpOnly = true
- }
- }
-
- ctx.Res.Header().Add("Set-Cookie", cookie.String())
-}
-
-// Get secure cookie from request by a given key.
-func (ctx *Context) GetSecureCookie(Secret, key string) (string, bool) {
- val := ctx.GetCookie(key)
- if val == "" {
- return "", false
- }
-
- parts := strings.SplitN(val, "|", 3)
-
- if len(parts) != 3 {
- return "", false
- }
-
- vs := parts[0]
- timestamp := parts[1]
- sig := parts[2]
-
- h := hmac.New(sha1.New, []byte(Secret))
- fmt.Fprintf(h, "%s%s", vs, timestamp)
-
- if fmt.Sprintf("%02x", h.Sum(nil)) != sig {
- return "", false
- }
- res, _ := base64.URLEncoding.DecodeString(vs)
- return string(res), true
-}
-
-// Set Secure cookie for response.
-func (ctx *Context) SetSecureCookie(Secret, name, value string, others ...interface{}) {
- vs := base64.URLEncoding.EncodeToString([]byte(value))
- timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- h := hmac.New(sha1.New, []byte(Secret))
- fmt.Fprintf(h, "%s%s", vs, timestamp)
- sig := fmt.Sprintf("%02x", h.Sum(nil))
- cookie := strings.Join([]string{vs, timestamp, sig}, "|")
- ctx.SetCookie(name, cookie, others...)
-}
-
func (ctx *Context) CsrfToken() string {
if len(ctx.csrfToken) > 0 {
return ctx.csrfToken
@@ -271,16 +163,16 @@ func (ctx *Context) ServeFile(file string, names ...string) {
if len(names) > 0 {
name = names[0]
} else {
- name = filepath.Base(file)
+ name = path.Base(file)
}
- ctx.Res.Header().Set("Content-Description", "File Transfer")
- ctx.Res.Header().Set("Content-Type", "application/octet-stream")
- ctx.Res.Header().Set("Content-Disposition", "attachment; filename="+name)
- ctx.Res.Header().Set("Content-Transfer-Encoding", "binary")
- ctx.Res.Header().Set("Expires", "0")
- ctx.Res.Header().Set("Cache-Control", "must-revalidate")
- ctx.Res.Header().Set("Pragma", "public")
- http.ServeFile(ctx.Res, ctx.Req, file)
+ ctx.Resp.Header().Set("Content-Description", "File Transfer")
+ ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
+ ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+name)
+ ctx.Resp.Header().Set("Content-Transfer-Encoding", "binary")
+ ctx.Resp.Header().Set("Expires", "0")
+ ctx.Resp.Header().Set("Cache-Control", "must-revalidate")
+ ctx.Resp.Header().Set("Pragma", "public")
+ http.ServeFile(ctx.Resp, ctx.Req, file)
}
func (ctx *Context) ServeContent(name string, r io.ReadSeeker, params ...interface{}) {
@@ -291,87 +183,52 @@ func (ctx *Context) ServeContent(name string, r io.ReadSeeker, params ...interfa
modtime = v
}
}
- ctx.Res.Header().Set("Content-Description", "File Transfer")
- ctx.Res.Header().Set("Content-Type", "application/octet-stream")
- ctx.Res.Header().Set("Content-Disposition", "attachment; filename="+name)
- ctx.Res.Header().Set("Content-Transfer-Encoding", "binary")
- ctx.Res.Header().Set("Expires", "0")
- ctx.Res.Header().Set("Cache-Control", "must-revalidate")
- ctx.Res.Header().Set("Pragma", "public")
- http.ServeContent(ctx.Res, ctx.Req, name, modtime, r)
-}
-
-type Flash struct {
- url.Values
- ErrorMsg, SuccessMsg string
-}
-
-func (f *Flash) Error(msg string) {
- f.Set("error", msg)
- f.ErrorMsg = msg
+ ctx.Resp.Header().Set("Content-Description", "File Transfer")
+ ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
+ ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+name)
+ ctx.Resp.Header().Set("Content-Transfer-Encoding", "binary")
+ ctx.Resp.Header().Set("Expires", "0")
+ ctx.Resp.Header().Set("Cache-Control", "must-revalidate")
+ ctx.Resp.Header().Set("Pragma", "public")
+ http.ServeContent(ctx.Resp, ctx.Req, name, modtime, r)
}
-func (f *Flash) Success(msg string) {
- f.Set("success", msg)
- f.SuccessMsg = msg
-}
-
-// InitContext initializes a classic context for a request.
-func InitContext() martini.Handler {
- return func(res http.ResponseWriter, r *http.Request, c martini.Context, rd *Render) {
+// Contexter initializes a classic context for a request.
+func Contexter() macaron.Handler {
+ return func(c *macaron.Context, l i18n.Locale, sess session.Store, f *session.Flash) {
ctx := &Context{
- c: c,
- // p: p,
- Req: r,
- Res: res,
- Cache: setting.Cache,
- Render: rd,
+ Context: c,
+ Locale: l,
+ Flash: f,
+ Session: sess,
}
- ctx.Data["PageStartTime"] = time.Now()
-
- // start session
- ctx.Session = setting.SessionManager.SessionStart(res, r)
+ // Cache: setting.Cache,
- // Get flash.
- values, err := url.ParseQuery(ctx.GetCookie("gogs_flash"))
- if err != nil {
- log.Error("InitContext.ParseQuery(flash): %v", err)
- } else if len(values) > 0 {
- ctx.Flash = &Flash{Values: values}
- ctx.Flash.ErrorMsg = ctx.Flash.Get("error")
- ctx.Flash.SuccessMsg = ctx.Flash.Get("success")
- ctx.Data["Flash"] = ctx.Flash
- ctx.SetCookie("gogs_flash", "", -1)
+ // Compute current URL for real-time change language.
+ link := ctx.Req.RequestURI
+ i := strings.Index(link, "?")
+ if i > -1 {
+ link = link[:i]
}
- ctx.Flash = &Flash{Values: url.Values{}}
+ ctx.Data["Link"] = link
- rw := res.(martini.ResponseWriter)
- rw.Before(func(martini.ResponseWriter) {
- ctx.Session.SessionRelease(res)
-
- if flash := ctx.Flash.Encode(); len(flash) > 0 {
- ctx.SetCookie("gogs_flash", flash, 0)
- }
- })
+ ctx.Data["PageStartTime"] = time.Now()
// Get user from session if logined.
- user := auth.SignedInUser(ctx.req.Header, ctx.Session)
- ctx.User = user
- ctx.IsSigned = user != nil
-
- ctx.Data["IsSigned"] = ctx.IsSigned
-
- if user != nil {
- ctx.Data["SignedUser"] = user
- ctx.Data["SignedUserId"] = user.Id
- ctx.Data["SignedUserName"] = user.Name
+ ctx.User = auth.SignedInUser(ctx.Req.Header, ctx.Session)
+ if ctx.User != nil {
+ ctx.IsSigned = true
+ ctx.Data["IsSigned"] = ctx.IsSigned
+ ctx.Data["SignedUser"] = ctx.User
+ ctx.Data["SignedUserId"] = ctx.User.Id
+ ctx.Data["SignedUserName"] = ctx.User.Name
ctx.Data["IsAdmin"] = ctx.User.IsAdmin
}
// If request sends files, parse them here otherwise the Query() can't be parsed and the CsrfToken will be invalid.
- if strings.Contains(r.Header.Get("Content-Type"), "multipart/form-data") {
- if err = ctx.Req.ParseMultipartForm(setting.AttachmentMaxSize << 20); err != nil { // 32MB max size
- ctx.Handle(500, "issue.Comment(ctx.Req.ParseMultipartForm)", err)
+ if ctx.Req.Method == "POST" && strings.Contains(ctx.Req.Header.Get("Content-Type"), "multipart/form-data") {
+ if err := ctx.Req.ParseMultipartForm(setting.AttachmentMaxSize << 20); err != nil && !strings.Contains(err.Error(), "EOF") { // 32MB max size
+ ctx.Handle(500, "ParseMultipartForm", err)
return
}
}
@@ -381,7 +238,5 @@ func InitContext() martini.Handler {
ctx.Data["CsrfTokenHtml"] = template.HTML(`<input type="hidden" name="_csrf" value="` + ctx.csrfToken + `">`)
c.Map(ctx)
-
- c.Next()
}
}
diff --git a/modules/middleware/logger.go b/modules/middleware/logger.go
deleted file mode 100644
index f918281a04..0000000000
--- a/modules/middleware/logger.go
+++ /dev/null
@@ -1,52 +0,0 @@
-// 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 middleware
-
-import (
- "fmt"
- "log"
- "net/http"
- "runtime"
- "time"
-
- "github.com/go-martini/martini"
-
- "github.com/gogits/gogs/modules/setting"
-)
-
-var isWindows bool
-
-func init() {
- isWindows = runtime.GOOS == "windows"
-}
-
-func Logger() martini.Handler {
- return func(res http.ResponseWriter, req *http.Request, ctx martini.Context, log *log.Logger) {
- if setting.DisableRouterLog {
- return
- }
-
- start := time.Now()
- log.Printf("Started %s %s", req.Method, req.URL.Path)
-
- rw := res.(martini.ResponseWriter)
- ctx.Next()
-
- content := fmt.Sprintf("Completed %v %s in %v", rw.Status(), http.StatusText(rw.Status()), time.Since(start))
- if !isWindows {
- switch rw.Status() {
- case 200:
- content = fmt.Sprintf("\033[1;32m%s\033[0m", content)
- case 304:
- content = fmt.Sprintf("\033[1;33m%s\033[0m", content)
- case 404:
- content = fmt.Sprintf("\033[1;31m%s\033[0m", content)
- case 500:
- content = fmt.Sprintf("\033[1;36m%s\033[0m", content)
- }
- }
- log.Println(content)
- }
-}
diff --git a/modules/middleware/render.go b/modules/middleware/render.go
deleted file mode 100644
index b5a0d697ae..0000000000
--- a/modules/middleware/render.go
+++ /dev/null
@@ -1,281 +0,0 @@
-// 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.
-
-// foked from https://github.com/martini-contrib/render/blob/master/render.go
-package middleware
-
-import (
- "bytes"
- "encoding/json"
- "fmt"
- "html/template"
- "io"
- "io/ioutil"
- "net/http"
- "os"
- "path/filepath"
- "time"
-
- "github.com/go-martini/martini"
-
- "github.com/gogits/gogs/modules/base"
-)
-
-const (
- ContentType = "Content-Type"
- ContentLength = "Content-Length"
- ContentJSON = "application/json"
- ContentHTML = "text/html"
- ContentXHTML = "application/xhtml+xml"
- defaultCharset = "UTF-8"
-)
-
-var helperFuncs = template.FuncMap{
- "yield": func() (string, error) {
- return "", fmt.Errorf("yield called with no layout defined")
- },
-}
-
-type Delims struct {
- Left string
- Right string
-}
-
-type RenderOptions struct {
- Directory string
- Layout string
- Extensions []string
- Funcs []template.FuncMap
- Delims Delims
- Charset string
- IndentJSON bool
- HTMLContentType string
-}
-
-type HTMLOptions struct {
- Layout string
-}
-
-func Renderer(options ...RenderOptions) martini.Handler {
- opt := prepareOptions(options)
- cs := prepareCharset(opt.Charset)
- t := compile(opt)
- return func(res http.ResponseWriter, req *http.Request, c martini.Context) {
- var tc *template.Template
- if martini.Env == martini.Dev {
-
- tc = compile(opt)
- } else {
-
- tc, _ = t.Clone()
- }
-
- rd := &Render{res, req, tc, opt, cs, base.TmplData{}, time.Time{}}
-
- rd.Data["TmplLoadTimes"] = func() string {
- if rd.startTime.IsZero() {
- return ""
- }
- return fmt.Sprint(time.Since(rd.startTime).Nanoseconds()/1e6) + "ms"
- }
-
- c.Map(rd.Data)
- c.Map(rd)
- }
-}
-
-func prepareCharset(charset string) string {
- if len(charset) != 0 {
- return "; charset=" + charset
- }
-
- return "; charset=" + defaultCharset
-}
-
-func prepareOptions(options []RenderOptions) RenderOptions {
- var opt RenderOptions
- if len(options) > 0 {
- opt = options[0]
- }
-
- if len(opt.Directory) == 0 {
- opt.Directory = "templates"
- }
- if len(opt.Extensions) == 0 {
- opt.Extensions = []string{".tmpl"}
- }
- if len(opt.HTMLContentType) == 0 {
- opt.HTMLContentType = ContentHTML
- }
-
- return opt
-}
-
-func compile(options RenderOptions) *template.Template {
- dir := options.Directory
- t := template.New(dir)
- t.Delims(options.Delims.Left, options.Delims.Right)
-
- template.Must(t.Parse("Martini"))
-
- filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
- r, err := filepath.Rel(dir, path)
- if err != nil {
- return err
- }
-
- ext := filepath.Ext(r)
- for _, extension := range options.Extensions {
- if ext == extension {
-
- buf, err := ioutil.ReadFile(path)
- if err != nil {
- panic(err)
- }
-
- name := (r[0 : len(r)-len(ext)])
- tmpl := t.New(filepath.ToSlash(name))
-
- for _, funcs := range options.Funcs {
- tmpl = tmpl.Funcs(funcs)
- }
-
- template.Must(tmpl.Funcs(helperFuncs).Parse(string(buf)))
- break
- }
- }
-
- return nil
- })
-
- return t
-}
-
-type Render struct {
- http.ResponseWriter
- req *http.Request
- t *template.Template
- opt RenderOptions
- compiledCharset string
-
- Data base.TmplData
-
- startTime time.Time
-}
-
-func (r *Render) JSON(status int, v interface{}) {
- var result []byte
- var err error
- if r.opt.IndentJSON {
- result, err = json.MarshalIndent(v, "", " ")
- } else {
- result, err = json.Marshal(v)
- }
- if err != nil {
- http.Error(r, err.Error(), 500)
- return
- }
-
- r.Header().Set(ContentType, ContentJSON+r.compiledCharset)
- r.WriteHeader(status)
- r.Write(result)
-}
-
-func (r *Render) JSONString(v interface{}) (string, error) {
- var result []byte
- var err error
- if r.opt.IndentJSON {
- result, err = json.MarshalIndent(v, "", " ")
- } else {
- result, err = json.Marshal(v)
- }
- if err != nil {
- return "", err
- }
- return string(result), nil
-}
-
-func (r *Render) renderBytes(name string, binding interface{}, htmlOpt ...HTMLOptions) (*bytes.Buffer, error) {
- opt := r.prepareHTMLOptions(htmlOpt)
-
- if len(opt.Layout) > 0 {
- r.addYield(name, binding)
- name = opt.Layout
- }
-
- out, err := r.execute(name, binding)
- if err != nil {
- return nil, err
- }
-
- return out, nil
-}
-
-func (r *Render) HTML(status int, name string, binding interface{}, htmlOpt ...HTMLOptions) {
- r.startTime = time.Now()
-
- out, err := r.renderBytes(name, binding, htmlOpt...)
- if err != nil {
- http.Error(r, err.Error(), http.StatusInternalServerError)
- return
- }
-
- r.Header().Set(ContentType, r.opt.HTMLContentType+r.compiledCharset)
- r.WriteHeader(status)
- io.Copy(r, out)
-}
-
-func (r *Render) HTMLString(name string, binding interface{}, htmlOpt ...HTMLOptions) (string, error) {
- if out, err := r.renderBytes(name, binding, htmlOpt...); err != nil {
- return "", err
- } else {
- return out.String(), nil
- }
-}
-
-func (r *Render) Error(status int, message ...string) {
- r.WriteHeader(status)
- if len(message) > 0 {
- r.Write([]byte(message[0]))
- }
-}
-
-func (r *Render) Redirect(location string, status ...int) {
- code := http.StatusFound
- if len(status) == 1 {
- code = status[0]
- }
-
- http.Redirect(r, r.req, location, code)
-}
-
-func (r *Render) Template() *template.Template {
- return r.t
-}
-
-func (r *Render) execute(name string, binding interface{}) (*bytes.Buffer, error) {
- buf := new(bytes.Buffer)
- return buf, r.t.ExecuteTemplate(buf, name, binding)
-}
-
-func (r *Render) addYield(name string, binding interface{}) {
- funcs := template.FuncMap{
- "yield": func() (template.HTML, error) {
- buf, err := r.execute(name, binding)
-
- return template.HTML(buf.String()), err
- },
- }
- r.t.Funcs(funcs)
-}
-
-func (r *Render) prepareHTMLOptions(htmlOpt []HTMLOptions) HTMLOptions {
- if len(htmlOpt) > 0 {
- return htmlOpt[0]
- }
-
- return HTMLOptions{
- Layout: r.opt.Layout,
- }
-}
diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go
index c6febffb2e..c3d26c2837 100644
--- a/modules/middleware/repo.go
+++ b/modules/middleware/repo.go
@@ -10,20 +10,19 @@ import (
"net/url"
"strings"
- "github.com/go-martini/martini"
-
- "github.com/gogits/git"
+ "github.com/Unknwon/macaron"
"github.com/gogits/gogs/models"
+ "github.com/gogits/gogs/modules/git"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/setting"
)
-func RepoAssignment(redirect bool, args ...bool) martini.Handler {
- return func(ctx *Context, params martini.Params) {
- // valid brachname
+func RepoAssignment(redirect bool, args ...bool) macaron.Handler {
+ return func(ctx *Context) {
+ // To valid brach name.
var validBranch bool
- // display bare quick start if it is a bare repo
+ // To display bare quick start if it is a bare repo.
var displayBare bool
if len(args) >= 1 {
@@ -35,51 +34,53 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler {
}
var (
- user *models.User
- err error
+ u *models.User
+ err error
)
- userName := params["username"]
- repoName := params["reponame"]
- refName := params["branchname"]
+ userName := ctx.Params(":username")
+ repoName := ctx.Params(":reponame")
+ refName := ctx.Params(":branchname")
+ if len(refName) == 0 {
+ refName = ctx.Params(":path")
+ }
- // TODO: need more advanced onwership and access level check.
// Collaborators who have write access can be seen as owners.
if ctx.IsSigned {
ctx.Repo.IsOwner, err = models.HasAccess(ctx.User.Name, userName+"/"+repoName, models.WRITABLE)
if err != nil {
- ctx.Handle(500, "RepoAssignment(HasAccess)", err)
+ ctx.Handle(500, "HasAccess", err)
return
}
ctx.Repo.IsTrueOwner = ctx.User.LowerName == strings.ToLower(userName)
}
if !ctx.Repo.IsTrueOwner {
- user, err = models.GetUserByName(userName)
+ u, err = models.GetUserByName(userName)
if err != nil {
if err == models.ErrUserNotExist {
- ctx.Handle(404, "RepoAssignment(GetUserByName)", err)
+ ctx.Handle(404, "GetUserByName", err)
return
} else if redirect {
ctx.Redirect("/")
return
}
- ctx.Handle(500, "RepoAssignment(GetUserByName)", err)
+ ctx.Handle(500, "GetUserByName", err)
return
}
} else {
- user = ctx.User
+ u = ctx.User
}
- if user == nil {
+ if u == nil {
if redirect {
ctx.Redirect("/")
return
}
- ctx.Handle(403, "RepoAssignment", errors.New("invliad user account for single repository"))
+ ctx.Handle(404, "RepoAssignment", errors.New("invliad user account for single repository"))
return
}
- ctx.Repo.Owner = user
+ ctx.Repo.Owner = u
// Organization owner team members are true owners as well.
if ctx.IsSigned && ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgOwner(ctx.User.Id) {
@@ -87,16 +88,19 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler {
}
// get repository
- repo, err := models.GetRepositoryByName(user.Id, repoName)
+ repo, err := models.GetRepositoryByName(u.Id, repoName)
if err != nil {
if err == models.ErrRepoNotExist {
- ctx.Handle(404, "RepoAssignment", err)
+ ctx.Handle(404, "GetRepositoryByName", err)
return
} else if redirect {
ctx.Redirect("/")
return
}
- ctx.Handle(500, "RepoAssignment", err)
+ ctx.Handle(500, "GetRepositoryByName", err)
+ return
+ } else if err = repo.GetOwner(); err != nil {
+ ctx.Handle(500, "GetOwner", err)
return
}
@@ -108,16 +112,16 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler {
// Check access.
if repo.IsPrivate && !ctx.Repo.IsOwner {
if ctx.User == nil {
- ctx.Handle(404, "RepoAssignment(HasAccess)", nil)
+ ctx.Handle(404, "HasAccess", nil)
return
}
hasAccess, err := models.HasAccess(ctx.User.Name, ctx.Repo.Owner.Name+"/"+repo.Name, models.READABLE)
if err != nil {
- ctx.Handle(500, "RepoAssignment(HasAccess)", err)
+ ctx.Handle(500, "HasAccess", err)
return
} else if !hasAccess {
- ctx.Handle(404, "RepoAssignment(HasAccess)", nil)
+ ctx.Handle(404, "HasAccess", nil)
return
}
}
@@ -127,7 +131,7 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler {
if repo.IsMirror {
ctx.Repo.Mirror, err = models.GetMirror(repo.Id)
if err != nil {
- ctx.Handle(500, "RepoAssignment(GetMirror)", err)
+ ctx.Handle(500, "GetMirror", err)
return
}
ctx.Data["MirrorInterval"] = ctx.Repo.Mirror.Interval
@@ -144,34 +148,33 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler {
return
}
ctx.Repo.GitRepo = gitRepo
- ctx.Repo.RepoLink = "/" + user.Name + "/" + repo.Name
+ ctx.Repo.RepoLink = "/" + u.Name + "/" + repo.Name
tags, err := ctx.Repo.GitRepo.GetTags()
if err != nil {
- ctx.Handle(500, "RepoAssignment(GetTags))", err)
+ ctx.Handle(500, "GetTags", err)
return
}
ctx.Repo.Repository.NumTags = len(tags)
- ctx.Data["Title"] = user.Name + "/" + repo.Name
+ ctx.Data["Title"] = u.Name + "/" + repo.Name
ctx.Data["Repository"] = repo
- ctx.Data["Owner"] = user
+ ctx.Data["Owner"] = ctx.Repo.Repository.Owner
ctx.Data["RepoLink"] = ctx.Repo.RepoLink
ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner
ctx.Data["IsRepositoryTrueOwner"] = ctx.Repo.IsTrueOwner
- ctx.Data["BranchName"] = ""
if setting.SshPort != 22 {
- ctx.Repo.CloneLink.SSH = fmt.Sprintf("ssh://%s@%s/%s/%s.git", setting.RunUser, setting.Domain, user.LowerName, repo.LowerName)
+ ctx.Repo.CloneLink.SSH = fmt.Sprintf("ssh://%s@%s/%s/%s.git", setting.RunUser, setting.Domain, u.LowerName, repo.LowerName)
} else {
- ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", setting.RunUser, setting.Domain, user.LowerName, repo.LowerName)
+ ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", setting.RunUser, setting.Domain, u.LowerName, repo.LowerName)
}
- ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s%s/%s.git", setting.AppUrl, user.LowerName, repo.LowerName)
+ ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s%s/%s.git", setting.AppUrl, u.LowerName, repo.LowerName)
ctx.Data["CloneLink"] = ctx.Repo.CloneLink
if ctx.Repo.Repository.IsGoget {
- ctx.Data["GoGetLink"] = fmt.Sprintf("%s%s/%s", setting.AppUrl, user.LowerName, repo.LowerName)
- ctx.Data["GoGetImport"] = fmt.Sprintf("%s/%s/%s", setting.Domain, user.LowerName, repo.LowerName)
+ ctx.Data["GoGetLink"] = fmt.Sprintf("%s%s/%s", setting.AppUrl, u.LowerName, repo.LowerName)
+ ctx.Data["GoGetImport"] = fmt.Sprintf("%s/%s/%s", setting.Domain, u.LowerName, repo.LowerName)
}
// when repo is bare, not valid branch
@@ -211,7 +214,7 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler {
return
}
} else {
- ctx.Handle(404, "RepoAssignment invalid repo", nil)
+ ctx.Handle(404, "RepoAssignment invalid repo", errors.New("branch or tag not exist"))
return
}
@@ -222,7 +225,7 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler {
} else {
brs, err := gitRepo.GetBranches()
if err != nil {
- ctx.Handle(500, "RepoAssignment(GetBranches))", err)
+ ctx.Handle(500, "GetBranches", err)
return
}
refName = brs[0]
@@ -233,6 +236,13 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler {
ctx.Data["IsBranch"] = ctx.Repo.IsBranch
ctx.Data["IsCommit"] = ctx.Repo.IsCommit
+
+ ctx.Repo.CommitsCount, err = ctx.Repo.Commit.CommitsCount()
+ if err != nil {
+ ctx.Handle(500, "CommitsCount", err)
+ return
+ }
+ ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
}
log.Debug("displayBare: %v; IsBare: %v", displayBare, ctx.Repo.Repository.IsBare)
@@ -240,7 +250,7 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler {
// repo is bare and display enable
if displayBare && ctx.Repo.Repository.IsBare {
log.Debug("Bare repository: %s", ctx.Repo.RepoLink)
- ctx.HTML(200, "repo/single_bare")
+ ctx.HTML(200, "repo/bare")
return
}
@@ -251,9 +261,10 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler {
ctx.Data["TagName"] = ctx.Repo.TagName
brs, err := ctx.Repo.GitRepo.GetBranches()
if err != nil {
- log.Error("RepoAssignment(GetBranches): %v", err)
+ log.Error(4, "GetBranches: %v", err)
}
ctx.Data["Branches"] = brs
+ ctx.Data["BrancheCount"] = len(brs)
// If not branch selected, try default one.
// If default branch doesn't exists, fall back to some other branch.
@@ -267,11 +278,11 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler {
ctx.Data["BranchName"] = ctx.Repo.BranchName
ctx.Data["CommitId"] = ctx.Repo.CommitId
- ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching
+ ctx.Data["IsWatchingRepo"] = ctx.Repo.IsWatching
}
}
-func RequireTrueOwner() martini.Handler {
+func RequireTrueOwner() macaron.Handler {
return func(ctx *Context) {
if !ctx.Repo.IsTrueOwner {
if !ctx.IsSigned {
diff --git a/modules/middleware/static.go b/modules/middleware/static.go
deleted file mode 100644
index 35f03f721a..0000000000
--- a/modules/middleware/static.go
+++ /dev/null
@@ -1,127 +0,0 @@
-// Copyright 2013 The Martini Authors. All rights reserved.
-// 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 middleware
-
-import (
- "log"
- "net/http"
- "path"
- "runtime"
- "strings"
-
- "github.com/go-martini/martini"
-
- "github.com/gogits/gogs/modules/setting"
-)
-
-// StaticOptions is a struct for specifying configuration options for the martini.Static middleware.
-type StaticOptions struct {
- // Prefix is the optional prefix used to serve the static directory content
- Prefix string
- // SkipLogging will disable [Static] log messages when a static file is served.
- SkipLogging bool
- // IndexFile defines which file to serve as index if it exists.
- IndexFile string
- // Expires defines which user-defined function to use for producing a HTTP Expires Header
- // https://developers.google.com/speed/docs/insights/LeverageBrowserCaching
- Expires func() string
-}
-
-func prepareStaticOptions(options []StaticOptions) StaticOptions {
- var opt StaticOptions
- if len(options) > 0 {
- opt = options[0]
- }
-
- // Defaults
- if len(opt.IndexFile) == 0 {
- opt.IndexFile = "index.html"
- }
- // Normalize the prefix if provided
- if opt.Prefix != "" {
- // Ensure we have a leading '/'
- if opt.Prefix[0] != '/' {
- opt.Prefix = "/" + opt.Prefix
- }
- // Remove any trailing '/'
- opt.Prefix = strings.TrimRight(opt.Prefix, "/")
- }
- return opt
-}
-
-// Static returns a middleware handler that serves static files in the given directory.
-func Static(directory string, staticOpt ...StaticOptions) martini.Handler {
- if runtime.GOOS == "windows" {
- if len(directory) < 2 || directory[1] != ':' {
- directory = path.Join(setting.StaticRootPath, directory)
- }
- } else if !path.IsAbs(directory) {
- directory = path.Join(setting.StaticRootPath, directory)
- }
-
- dir := http.Dir(directory)
- opt := prepareStaticOptions(staticOpt)
-
- return func(res http.ResponseWriter, req *http.Request, log *log.Logger) {
- if req.Method != "GET" && req.Method != "HEAD" {
- return
- }
- file := req.URL.Path
- // if we have a prefix, filter requests by stripping the prefix
- if opt.Prefix != "" {
- if !strings.HasPrefix(file, opt.Prefix) {
- return
- }
- file = file[len(opt.Prefix):]
- if file != "" && file[0] != '/' {
- return
- }
- }
- f, err := dir.Open(file)
- if err != nil {
- // discard the error?
- return
- }
- defer f.Close()
-
- fi, err := f.Stat()
- if err != nil {
- return
- }
-
- // try to serve index file
- if fi.IsDir() {
- // redirect if missing trailing slash
- if !strings.HasSuffix(req.URL.Path, "/") {
- http.Redirect(res, req, req.URL.Path+"/", http.StatusFound)
- return
- }
-
- file = path.Join(file, opt.IndexFile)
- f, err = dir.Open(file)
- if err != nil {
- return
- }
- defer f.Close()
-
- fi, err = f.Stat()
- if err != nil || fi.IsDir() {
- return
- }
- }
-
- if !opt.SkipLogging {
- log.Println("[Static] Serving " + file)
- }
-
- // Add an Expires header to the static content
- if opt.Expires != nil {
- res.Header().Set("Expires", opt.Expires())
- }
-
- http.ServeContent(res, req, file, fi.ModTime(), f)
- }
-}