You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
3.2 KiB
117 lines
3.2 KiB
4 years ago
|
// Copyright 2020 The Gitea 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 routes
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net/http"
|
||
4 years ago
|
"path"
|
||
4 years ago
|
|
||
4 years ago
|
"code.gitea.io/gitea/modules/forms"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/log"
|
||
|
"code.gitea.io/gitea/modules/middlewares"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/public"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/setting"
|
||
|
"code.gitea.io/gitea/modules/templates"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/web"
|
||
|
"code.gitea.io/gitea/routers"
|
||
4 years ago
|
|
||
4 years ago
|
"gitea.com/go-chi/session"
|
||
4 years ago
|
)
|
||
|
|
||
4 years ago
|
func installRecovery() func(next http.Handler) http.Handler {
|
||
|
var rnd = templates.HTMLRenderer()
|
||
4 years ago
|
return func(next http.Handler) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||
|
defer func() {
|
||
|
// Why we need this? The first recover will try to render a beautiful
|
||
|
// error page for user, but the process can still panic again, then
|
||
|
// we have to just recover twice and send a simple error page that
|
||
|
// should not panic any more.
|
||
|
defer func() {
|
||
|
if err := recover(); err != nil {
|
||
|
combinedErr := fmt.Sprintf("PANIC: %v\n%s", err, string(log.Stack(2)))
|
||
|
log.Error(combinedErr)
|
||
4 years ago
|
if setting.IsProd() {
|
||
4 years ago
|
http.Error(w, http.StatusText(500), 500)
|
||
4 years ago
|
} else {
|
||
|
http.Error(w, combinedErr, 500)
|
||
4 years ago
|
}
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
if err := recover(); err != nil {
|
||
|
combinedErr := fmt.Sprintf("PANIC: %v\n%s", err, string(log.Stack(2)))
|
||
|
log.Error("%v", combinedErr)
|
||
|
|
||
|
lc := middlewares.Locale(w, req)
|
||
|
var store = dataStore{
|
||
|
Data: templates.Vars{
|
||
4 years ago
|
"Language": lc.Language(),
|
||
|
"CurrentURL": setting.AppSubURL + req.URL.RequestURI(),
|
||
|
"i18n": lc,
|
||
|
"SignedUserID": int64(0),
|
||
|
"SignedUserName": "",
|
||
4 years ago
|
},
|
||
|
}
|
||
|
|
||
|
w.Header().Set(`X-Frame-Options`, `SAMEORIGIN`)
|
||
|
|
||
4 years ago
|
if !setting.IsProd() {
|
||
4 years ago
|
store.Data["ErrorMsg"] = combinedErr
|
||
4 years ago
|
}
|
||
4 years ago
|
err = rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(store.Data))
|
||
4 years ago
|
if err != nil {
|
||
|
log.Error("%v", err)
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
next.ServeHTTP(w, req)
|
||
|
})
|
||
|
}
|
||
|
}
|
||
4 years ago
|
|
||
|
// InstallRoutes registers the install routes
|
||
|
func InstallRoutes() *web.Route {
|
||
|
r := web.NewRoute()
|
||
|
for _, middle := range commonMiddlewares() {
|
||
|
r.Use(middle)
|
||
|
}
|
||
|
|
||
|
r.Use(session.Sessioner(session.Options{
|
||
|
Provider: setting.SessionConfig.Provider,
|
||
|
ProviderConfig: setting.SessionConfig.ProviderConfig,
|
||
|
CookieName: setting.SessionConfig.CookieName,
|
||
|
CookiePath: setting.SessionConfig.CookiePath,
|
||
|
Gclifetime: setting.SessionConfig.Gclifetime,
|
||
|
Maxlifetime: setting.SessionConfig.Maxlifetime,
|
||
|
Secure: setting.SessionConfig.Secure,
|
||
|
Domain: setting.SessionConfig.Domain,
|
||
|
}))
|
||
|
|
||
|
r.Use(installRecovery())
|
||
|
|
||
|
r.Use(public.Custom(
|
||
|
&public.Options{
|
||
|
SkipLogging: setting.DisableRouterLog,
|
||
|
},
|
||
|
))
|
||
|
r.Use(public.Static(
|
||
|
&public.Options{
|
||
|
Directory: path.Join(setting.StaticRootPath, "public"),
|
||
|
SkipLogging: setting.DisableRouterLog,
|
||
|
},
|
||
|
))
|
||
|
|
||
|
r.Use(routers.InstallInit)
|
||
|
r.Get("/", routers.Install)
|
||
|
r.Post("/", web.Bind(forms.InstallForm{}), routers.InstallPost)
|
||
|
r.NotFound(func(w http.ResponseWriter, req *http.Request) {
|
||
|
http.Redirect(w, req, setting.AppURL, 302)
|
||
|
})
|
||
|
return r
|
||
|
}
|