2021-01-05 13:05:40 +00:00
|
|
|
// 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.
|
|
|
|
|
2021-06-08 23:33:54 +00:00
|
|
|
package install
|
2021-01-05 13:05:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-01-26 15:36:53 +00:00
|
|
|
"path"
|
2021-01-05 13:05:40 +00:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-01-26 15:36:53 +00:00
|
|
|
"code.gitea.io/gitea/modules/public"
|
2021-01-05 13:05:40 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/templates"
|
2021-01-26 15:36:53 +00:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-01-30 08:55:53 +00:00
|
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
2021-06-08 23:33:54 +00:00
|
|
|
"code.gitea.io/gitea/routers/common"
|
2021-04-06 19:44:05 +00:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2021-01-05 13:05:40 +00:00
|
|
|
|
2021-01-26 15:36:53 +00:00
|
|
|
"gitea.com/go-chi/session"
|
2021-01-05 13:05:40 +00:00
|
|
|
)
|
|
|
|
|
2021-06-08 23:33:54 +00:00
|
|
|
type dataStore map[string]interface{}
|
|
|
|
|
|
|
|
func (d *dataStore) GetData() map[string]interface{} {
|
|
|
|
return *d
|
|
|
|
}
|
|
|
|
|
2021-01-26 15:36:53 +00:00
|
|
|
func installRecovery() func(next http.Handler) http.Handler {
|
2022-01-20 17:46:10 +00:00
|
|
|
rnd := templates.HTMLRenderer()
|
2021-01-05 13:05:40 +00:00
|
|
|
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 {
|
2022-01-20 11:41:25 +00:00
|
|
|
combinedErr := fmt.Sprintf("PANIC: %v\n%s", err, log.Stack(2))
|
|
|
|
log.Error("%s", combinedErr)
|
2021-10-20 14:37:19 +00:00
|
|
|
if setting.IsProd {
|
2022-03-23 04:54:07 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-01-14 21:17:03 +00:00
|
|
|
} else {
|
2022-03-23 04:54:07 +00:00
|
|
|
http.Error(w, combinedErr, http.StatusInternalServerError)
|
2021-01-05 13:05:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := recover(); err != nil {
|
2022-01-20 11:41:25 +00:00
|
|
|
combinedErr := fmt.Sprintf("PANIC: %v\n%s", err, log.Stack(2))
|
|
|
|
log.Error("%s", combinedErr)
|
2021-01-05 13:05:40 +00:00
|
|
|
|
2021-01-30 08:55:53 +00:00
|
|
|
lc := middleware.Locale(w, req)
|
2022-01-20 17:46:10 +00:00
|
|
|
store := dataStore{
|
2021-06-08 23:33:54 +00:00
|
|
|
"Language": lc.Language(),
|
|
|
|
"CurrentURL": setting.AppSubURL + req.URL.RequestURI(),
|
|
|
|
"i18n": lc,
|
|
|
|
"SignedUserID": int64(0),
|
|
|
|
"SignedUserName": "",
|
2021-01-05 13:05:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 20:47:10 +00:00
|
|
|
w.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions)
|
2021-01-05 13:05:40 +00:00
|
|
|
|
2021-10-20 14:37:19 +00:00
|
|
|
if !setting.IsProd {
|
2021-06-08 23:33:54 +00:00
|
|
|
store["ErrorMsg"] = combinedErr
|
2021-01-05 13:05:40 +00:00
|
|
|
}
|
2022-03-23 04:54:07 +00:00
|
|
|
err = rnd.HTML(w, http.StatusInternalServerError, "status/500", templates.BaseVars().Merge(store))
|
2021-01-05 13:05:40 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("%v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 15:36:53 +00:00
|
|
|
|
2021-06-08 23:33:54 +00:00
|
|
|
// Routes registers the install routes
|
|
|
|
func Routes() *web.Route {
|
2021-01-26 15:36:53 +00:00
|
|
|
r := web.NewRoute()
|
2021-06-08 23:33:54 +00:00
|
|
|
for _, middle := range common.Middlewares() {
|
2021-01-26 15:36:53 +00:00
|
|
|
r.Use(middle)
|
|
|
|
}
|
|
|
|
|
2022-01-20 11:41:25 +00:00
|
|
|
r.Use(web.WrapWithPrefix(public.AssetsURLPathPrefix, public.AssetsHandlerFunc(&public.Options{
|
2021-05-30 10:25:11 +00:00
|
|
|
Directory: path.Join(setting.StaticRootPath, "public"),
|
2022-01-20 11:41:25 +00:00
|
|
|
Prefix: public.AssetsURLPathPrefix,
|
|
|
|
}), "InstallAssetsHandler"))
|
2021-05-30 10:25:11 +00:00
|
|
|
|
2021-01-26 15:36:53 +00:00
|
|
|
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,
|
2021-05-31 18:22:36 +00:00
|
|
|
SameSite: setting.SessionConfig.SameSite,
|
2021-01-26 15:36:53 +00:00
|
|
|
Domain: setting.SessionConfig.Domain,
|
|
|
|
}))
|
|
|
|
|
|
|
|
r.Use(installRecovery())
|
2021-06-08 23:33:54 +00:00
|
|
|
r.Use(Init)
|
|
|
|
r.Get("/", Install)
|
|
|
|
r.Post("/", web.Bind(forms.InstallForm{}), SubmitInstall)
|
2022-01-20 11:41:25 +00:00
|
|
|
|
|
|
|
r.NotFound(web.Wrap(installNotFound))
|
2021-01-26 15:36:53 +00:00
|
|
|
return r
|
|
|
|
}
|
2022-01-20 11:41:25 +00:00
|
|
|
|
|
|
|
func installNotFound(w http.ResponseWriter, req *http.Request) {
|
|
|
|
http.Redirect(w, req, setting.AppURL, http.StatusFound)
|
|
|
|
}
|