58 lines
2.2 KiB
Go
58 lines
2.2 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
httppkg "github.com/fatedier/frp/pkg/util/http"
|
|
netpkg "github.com/fatedier/frp/pkg/util/net"
|
|
adminapi "github.com/fatedier/frp/server/http"
|
|
)
|
|
|
|
func (svr *Service) registerRouteHandlers(helper *httppkg.RouterRegisterHelper) {
|
|
helper.Router.HandleFunc("/healthz", healthz)
|
|
|
|
// root redirects to admin setup
|
|
helper.Router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
http.Redirect(w, r, "/admin/setup", http.StatusFound)
|
|
})
|
|
|
|
subRouter := helper.Router.NewRoute().Subrouter()
|
|
|
|
subRouter.Use(helper.AuthMiddleware)
|
|
subRouter.Use(httppkg.NewRequestLogger)
|
|
|
|
// metrics
|
|
if svr.cfg.EnablePrometheus {
|
|
subRouter.Handle("/metrics", promhttp.Handler())
|
|
}
|
|
|
|
apiController := adminapi.NewController(svr.cfg, svr.clientRegistry, svr.pxyManager)
|
|
|
|
// apis
|
|
subRouter.HandleFunc("/api/serverinfo", httppkg.MakeHTTPHandlerFunc(apiController.APIServerInfo)).Methods("GET")
|
|
subRouter.HandleFunc("/api/proxy/{type}", httppkg.MakeHTTPHandlerFunc(apiController.APIProxyByType)).Methods("GET")
|
|
subRouter.HandleFunc("/api/proxy/{type}/{name}", httppkg.MakeHTTPHandlerFunc(apiController.APIProxyByTypeAndName)).Methods("GET")
|
|
subRouter.HandleFunc("/api/proxies/{name}", httppkg.MakeHTTPHandlerFunc(apiController.APIProxyByName)).Methods("GET")
|
|
subRouter.HandleFunc("/api/traffic/{name}", httppkg.MakeHTTPHandlerFunc(apiController.APIProxyTraffic)).Methods("GET")
|
|
subRouter.HandleFunc("/api/clients", httppkg.MakeHTTPHandlerFunc(apiController.APIClientList)).Methods("GET")
|
|
subRouter.HandleFunc("/api/clients/{key}", httppkg.MakeHTTPHandlerFunc(apiController.APIClientDetail)).Methods("GET")
|
|
subRouter.HandleFunc("/api/proxies", httppkg.MakeHTTPHandlerFunc(apiController.DeleteProxies)).Methods("DELETE")
|
|
|
|
// view
|
|
subRouter.Handle("/favicon.ico", http.FileServer(helper.AssetsFS)).Methods("GET")
|
|
subRouter.PathPrefix("/static/").Handler(
|
|
netpkg.MakeHTTPGzipHandler(http.StripPrefix("/static/", http.FileServer(helper.AssetsFS))),
|
|
).Methods("GET")
|
|
|
|
// admin routes (htmx-based dashboard)
|
|
if svr.adminHandler != nil {
|
|
svr.adminHandler.RegisterRoutes(helper.Router)
|
|
}
|
|
}
|
|
|
|
func healthz(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(200)
|
|
}
|