2021-01-05 14:05:40 +01:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-01-05 14:05:40 +01:00
|
|
|
|
2021-06-09 01:33:54 +02:00
|
|
|
package install
|
2021-01-05 14:05:40 +01:00
|
|
|
|
|
|
|
import (
|
2022-08-28 11:43:25 +02:00
|
|
|
goctx "context"
|
2021-01-05 14:05:40 +01:00
|
|
|
"fmt"
|
2023-03-05 11:59:58 +01:00
|
|
|
"html"
|
2021-01-05 14:05:40 +01:00
|
|
|
"net/http"
|
2021-01-26 16:36:53 +01:00
|
|
|
"path"
|
2021-01-05 14:05:40 +01:00
|
|
|
|
2022-07-23 08:38:03 +02:00
|
|
|
"code.gitea.io/gitea/modules/httpcache"
|
2021-01-05 14:05:40 +01:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-01-26 16:36:53 +01:00
|
|
|
"code.gitea.io/gitea/modules/public"
|
2021-01-05 14:05:40 +01:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/templates"
|
2021-01-26 16:36:53 +01:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-01-30 09:55:53 +01:00
|
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
2021-06-09 01:33:54 +02:00
|
|
|
"code.gitea.io/gitea/routers/common"
|
2022-05-04 13:56:20 +02:00
|
|
|
"code.gitea.io/gitea/routers/web/healthcheck"
|
2021-04-06 21:44:05 +02:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2021-01-05 14:05:40 +01:00
|
|
|
|
2021-01-26 16:36:53 +01:00
|
|
|
"gitea.com/go-chi/session"
|
2021-01-05 14:05:40 +01:00
|
|
|
)
|
|
|
|
|
2021-06-09 01:33:54 +02:00
|
|
|
type dataStore map[string]interface{}
|
|
|
|
|
|
|
|
func (d *dataStore) GetData() map[string]interface{} {
|
|
|
|
return *d
|
|
|
|
}
|
|
|
|
|
2022-08-28 11:43:25 +02:00
|
|
|
func installRecovery(ctx goctx.Context) func(next http.Handler) http.Handler {
|
|
|
|
_, rnd := templates.HTMLRenderer(ctx)
|
2021-01-05 14:05:40 +01: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
|
2023-03-05 11:59:58 +01:00
|
|
|
// should not panic anymore.
|
2021-01-05 14:05:40 +01:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2022-01-20 12:41:25 +01:00
|
|
|
combinedErr := fmt.Sprintf("PANIC: %v\n%s", err, log.Stack(2))
|
|
|
|
log.Error("%s", combinedErr)
|
2021-10-20 16:37:19 +02:00
|
|
|
if setting.IsProd {
|
2022-03-23 05:54:07 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-01-14 22:17:03 +01:00
|
|
|
} else {
|
2022-03-23 05:54:07 +01:00
|
|
|
http.Error(w, combinedErr, http.StatusInternalServerError)
|
2021-01-05 14:05:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := recover(); err != nil {
|
2022-01-20 12:41:25 +01:00
|
|
|
combinedErr := fmt.Sprintf("PANIC: %v\n%s", err, log.Stack(2))
|
|
|
|
log.Error("%s", combinedErr)
|
2021-01-05 14:05:40 +01:00
|
|
|
|
2021-01-30 09:55:53 +01:00
|
|
|
lc := middleware.Locale(w, req)
|
2022-01-20 18:46:10 +01:00
|
|
|
store := dataStore{
|
2021-06-09 01:33:54 +02:00
|
|
|
"Language": lc.Language(),
|
|
|
|
"CurrentURL": setting.AppSubURL + req.URL.RequestURI(),
|
2022-06-27 22:58:46 +02:00
|
|
|
"locale": lc,
|
2021-06-09 01:33:54 +02:00
|
|
|
"SignedUserID": int64(0),
|
|
|
|
"SignedUserName": "",
|
2021-01-05 14:05:40 +01:00
|
|
|
}
|
|
|
|
|
2022-07-23 08:38:03 +02:00
|
|
|
httpcache.AddCacheControlToHeader(w.Header(), 0, "no-transform")
|
2021-08-06 22:47:10 +02:00
|
|
|
w.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions)
|
2021-01-05 14:05:40 +01:00
|
|
|
|
2021-10-20 16:37:19 +02:00
|
|
|
if !setting.IsProd {
|
2021-06-09 01:33:54 +02:00
|
|
|
store["ErrorMsg"] = combinedErr
|
2021-01-05 14:05:40 +01:00
|
|
|
}
|
2022-03-23 05:54:07 +01:00
|
|
|
err = rnd.HTML(w, http.StatusInternalServerError, "status/500", templates.BaseVars().Merge(store))
|
2021-01-05 14:05:40 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error("%v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 16:36:53 +01:00
|
|
|
|
2021-06-09 01:33:54 +02:00
|
|
|
// Routes registers the install routes
|
2022-08-28 11:43:25 +02:00
|
|
|
func Routes(ctx goctx.Context) *web.Route {
|
2021-01-26 16:36:53 +01:00
|
|
|
r := web.NewRoute()
|
2021-06-09 01:33:54 +02:00
|
|
|
for _, middle := range common.Middlewares() {
|
2021-01-26 16:36:53 +01:00
|
|
|
r.Use(middle)
|
|
|
|
}
|
|
|
|
|
2022-01-20 12:41:25 +01:00
|
|
|
r.Use(web.WrapWithPrefix(public.AssetsURLPathPrefix, public.AssetsHandlerFunc(&public.Options{
|
2021-05-30 12:25:11 +02:00
|
|
|
Directory: path.Join(setting.StaticRootPath, "public"),
|
2022-01-20 12:41:25 +01:00
|
|
|
Prefix: public.AssetsURLPathPrefix,
|
|
|
|
}), "InstallAssetsHandler"))
|
2021-05-30 12:25:11 +02:00
|
|
|
|
2021-01-26 16:36:53 +01: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 20:22:36 +02:00
|
|
|
SameSite: setting.SessionConfig.SameSite,
|
2021-01-26 16:36:53 +01:00
|
|
|
Domain: setting.SessionConfig.Domain,
|
|
|
|
}))
|
|
|
|
|
2022-08-28 11:43:25 +02:00
|
|
|
r.Use(installRecovery(ctx))
|
|
|
|
r.Use(Init(ctx))
|
2023-03-05 11:59:58 +01:00
|
|
|
r.Get("/", Install) // it must be on the root, because the "install.js" use the window.location to replace the "localhost" AppURL
|
2021-06-09 01:33:54 +02:00
|
|
|
r.Post("/", web.Bind(forms.InstallForm{}), SubmitInstall)
|
2023-03-05 11:59:58 +01:00
|
|
|
r.Get("/post-install", InstallDone)
|
2022-05-04 13:56:20 +02:00
|
|
|
r.Get("/api/healthz", healthcheck.Check)
|
2022-01-20 12:41:25 +01:00
|
|
|
|
|
|
|
r.NotFound(web.Wrap(installNotFound))
|
2021-01-26 16:36:53 +01:00
|
|
|
return r
|
|
|
|
}
|
2022-01-20 12:41:25 +01:00
|
|
|
|
|
|
|
func installNotFound(w http.ResponseWriter, req *http.Request) {
|
2023-03-05 11:59:58 +01:00
|
|
|
w.Header().Add("Content-Type", "text/html; charset=utf-8")
|
|
|
|
w.Header().Add("Refresh", fmt.Sprintf("1; url=%s", setting.AppSubURL+"/"))
|
|
|
|
// do not use 30x status, because the "post-install" page needs to use 404/200 to detect if Gitea has been installed.
|
|
|
|
// the fetch API could follow 30x requests to the page with 200 status.
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
_, _ = fmt.Fprintf(w, `Not Found. <a href="%s">Go to default page</a>.`, html.EscapeString(setting.AppSubURL+"/"))
|
2022-01-20 12:41:25 +01:00
|
|
|
}
|