2021-01-05 18:35:40 +05:30
|
|
|
// 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"
|
2021-01-26 21:06:53 +05:30
|
|
|
"path"
|
2021-01-05 18:35:40 +05:30
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-01-26 21:06:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/public"
|
2021-01-05 18:35:40 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/templates"
|
2021-01-26 21:06:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-01-30 14:25:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
2021-01-26 21:06:53 +05:30
|
|
|
"code.gitea.io/gitea/routers"
|
2021-04-07 01:14:05 +05:30
|
|
|
"code.gitea.io/gitea/services/forms"
|
2021-01-05 18:35:40 +05:30
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
"gitea.com/go-chi/session"
|
2021-01-05 18:35:40 +05:30
|
|
|
)
|
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
func installRecovery() func(next http.Handler) http.Handler {
|
|
|
|
var rnd = templates.HTMLRenderer()
|
2021-01-05 18:35:40 +05:30
|
|
|
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)
|
2021-01-15 02:47:03 +05:30
|
|
|
if setting.IsProd() {
|
2021-01-05 18:35:40 +05:30
|
|
|
http.Error(w, http.StatusText(500), 500)
|
2021-01-15 02:47:03 +05:30
|
|
|
} else {
|
|
|
|
http.Error(w, combinedErr, 500)
|
2021-01-05 18:35:40 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
combinedErr := fmt.Sprintf("PANIC: %v\n%s", err, string(log.Stack(2)))
|
|
|
|
log.Error("%v", combinedErr)
|
|
|
|
|
2021-01-30 14:25:53 +05:30
|
|
|
lc := middleware.Locale(w, req)
|
2021-01-05 18:35:40 +05:30
|
|
|
var store = dataStore{
|
|
|
|
Data: templates.Vars{
|
2021-01-26 21:06:53 +05:30
|
|
|
"Language": lc.Language(),
|
|
|
|
"CurrentURL": setting.AppSubURL + req.URL.RequestURI(),
|
|
|
|
"i18n": lc,
|
|
|
|
"SignedUserID": int64(0),
|
|
|
|
"SignedUserName": "",
|
2021-01-05 18:35:40 +05:30
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set(`X-Frame-Options`, `SAMEORIGIN`)
|
|
|
|
|
2021-01-15 02:47:03 +05:30
|
|
|
if !setting.IsProd() {
|
2021-01-14 21:05:10 +05:30
|
|
|
store.Data["ErrorMsg"] = combinedErr
|
2021-01-05 18:35:40 +05:30
|
|
|
}
|
2021-01-08 17:45:06 +05:30
|
|
|
err = rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(store.Data))
|
2021-01-05 18:35:40 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("%v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 21:06:53 +05:30
|
|
|
|
|
|
|
// 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,
|
2021-04-30 21:29:16 +05:30
|
|
|
Prefix: "/assets",
|
2021-01-26 21:06:53 +05:30
|
|
|
},
|
|
|
|
))
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|