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