2021-01-05 13:05:40 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-01-05 13:05:40 +00:00
|
|
|
|
2021-06-08 23:33:54 +00:00
|
|
|
package install
|
2021-01-05 13:05:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-03-04 02:12:02 +00:00
|
|
|
"html"
|
2021-01-05 13:05:40 +00:00
|
|
|
"net/http"
|
|
|
|
|
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"
|
2021-01-26 15:36:53 +00:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-06-08 23:33:54 +00:00
|
|
|
"code.gitea.io/gitea/routers/common"
|
2022-05-04 11:56:20 +00:00
|
|
|
"code.gitea.io/gitea/routers/web/healthcheck"
|
2021-04-06 19:44:05 +00:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2021-01-05 13:05:40 +00:00
|
|
|
)
|
|
|
|
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-20 18:49:06 +00:00
|
|
|
// Routes registers the installation routes
|
2024-06-18 22:32:45 +00:00
|
|
|
func Routes() *web.Router {
|
|
|
|
base := web.NewRouter()
|
2023-04-27 06:06:45 +00:00
|
|
|
base.Use(common.ProtocolMiddlewares()...)
|
2023-07-21 12:14:20 +00:00
|
|
|
base.Methods("GET, HEAD", "/assets/*", public.FileHandlerFunc())
|
2021-01-26 15:36:53 +00:00
|
|
|
|
2024-06-18 22:32:45 +00:00
|
|
|
r := web.NewRouter()
|
2023-05-04 06:36:34 +00:00
|
|
|
r.Use(common.Sessioner(), Contexter())
|
2023-03-04 02:12:02 +00: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-08 23:33:54 +00:00
|
|
|
r.Post("/", web.Bind(forms.InstallForm{}), SubmitInstall)
|
2023-03-04 02:12:02 +00:00
|
|
|
r.Get("/post-install", InstallDone)
|
2022-05-04 11:56:20 +00:00
|
|
|
r.Get("/api/healthz", healthcheck.Check)
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-20 18:49:06 +00:00
|
|
|
r.NotFound(installNotFound)
|
2023-04-27 06:06:45 +00:00
|
|
|
|
|
|
|
base.Mount("", r)
|
|
|
|
return base
|
2021-01-26 15:36:53 +00:00
|
|
|
}
|
2022-01-20 11:41:25 +00:00
|
|
|
|
|
|
|
func installNotFound(w http.ResponseWriter, req *http.Request) {
|
2023-03-04 02:12:02 +00: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 11:41:25 +00:00
|
|
|
}
|