| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package httpd
- import (
- "net/http"
- "github.com/drakkan/sftpgo/dataprovider"
- "github.com/drakkan/sftpgo/logger"
- "github.com/drakkan/sftpgo/sftpd"
- "github.com/drakkan/sftpgo/utils"
- "github.com/go-chi/chi"
- "github.com/go-chi/chi/middleware"
- "github.com/go-chi/render"
- "github.com/prometheus/client_golang/prometheus/promhttp"
- )
- // GetHTTPRouter returns the configured HTTP handler
- func GetHTTPRouter() http.Handler {
- return router
- }
- func initializeRouter(staticFilesPath string) {
- router = chi.NewRouter()
- router.Use(middleware.RequestID)
- router.Use(middleware.RealIP)
- router.Use(logger.NewStructuredLogger(logger.GetLogger()))
- router.Use(middleware.Recoverer)
- router.NotFound(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- sendAPIResponse(w, r, nil, "Not Found", http.StatusNotFound)
- }))
- router.MethodNotAllowed(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- sendAPIResponse(w, r, nil, "Method not allowed", http.StatusMethodNotAllowed)
- }))
- router.Get("/", func(w http.ResponseWriter, r *http.Request) {
- http.Redirect(w, r, webUsersPath, http.StatusMovedPermanently)
- })
- router.Get(webBasePath, func(w http.ResponseWriter, r *http.Request) {
- http.Redirect(w, r, webUsersPath, http.StatusMovedPermanently)
- })
- router.Handle(metricsPath, promhttp.Handler())
- router.Get(versionPath, func(w http.ResponseWriter, r *http.Request) {
- render.JSON(w, r, utils.GetAppVersion())
- })
- router.Get(providerStatusPath, func(w http.ResponseWriter, r *http.Request) {
- err := dataprovider.GetProviderStatus(dataProvider)
- if err != nil {
- sendAPIResponse(w, r, err, "", http.StatusInternalServerError)
- } else {
- sendAPIResponse(w, r, err, "Alive", http.StatusOK)
- }
- })
- router.Get(activeConnectionsPath, func(w http.ResponseWriter, r *http.Request) {
- render.JSON(w, r, sftpd.GetConnectionsStats())
- })
- router.Delete(activeConnectionsPath+"/{connectionID}", func(w http.ResponseWriter, r *http.Request) {
- handleCloseConnection(w, r)
- })
- router.Get(quotaScanPath, func(w http.ResponseWriter, r *http.Request) {
- getQuotaScans(w, r)
- })
- router.Post(quotaScanPath, func(w http.ResponseWriter, r *http.Request) {
- startQuotaScan(w, r)
- })
- router.Get(userPath, func(w http.ResponseWriter, r *http.Request) {
- getUsers(w, r)
- })
- router.Post(userPath, func(w http.ResponseWriter, r *http.Request) {
- addUser(w, r)
- })
- router.Get(userPath+"/{userID}", func(w http.ResponseWriter, r *http.Request) {
- getUserByID(w, r)
- })
- router.Put(userPath+"/{userID}", func(w http.ResponseWriter, r *http.Request) {
- updateUser(w, r)
- })
- router.Delete(userPath+"/{userID}", func(w http.ResponseWriter, r *http.Request) {
- deleteUser(w, r)
- })
- router.Get(webUsersPath, func(w http.ResponseWriter, r *http.Request) {
- handleGetWebUsers(w, r)
- })
- router.Get(webUserPath, func(w http.ResponseWriter, r *http.Request) {
- handleWebAddUserGet(w, r)
- })
- router.Get(webUserPath+"/{userID}", func(w http.ResponseWriter, r *http.Request) {
- handleWebUpdateUserGet(chi.URLParam(r, "userID"), w, r)
- })
- router.Post(webUserPath, func(w http.ResponseWriter, r *http.Request) {
- handleWebAddUserPost(w, r)
- })
- router.Post(webUserPath+"/{userID}", func(w http.ResponseWriter, r *http.Request) {
- handleWebUpdateUserPost(chi.URLParam(r, "userID"), w, r)
- })
- router.Get(webConnectionsPath, func(w http.ResponseWriter, r *http.Request) {
- handleWebGetConnections(w, r)
- })
- router.Group(func(router chi.Router) {
- router.Use(middleware.DefaultCompress)
- fileServer(router, webStaticFilesPath, http.Dir(staticFilesPath))
- })
- }
- func handleCloseConnection(w http.ResponseWriter, r *http.Request) {
- connectionID := chi.URLParam(r, "connectionID")
- if connectionID == "" {
- sendAPIResponse(w, r, nil, "connectionID is mandatory", http.StatusBadRequest)
- return
- }
- if sftpd.CloseActiveConnection(connectionID) {
- sendAPIResponse(w, r, nil, "Connection closed", http.StatusOK)
- } else {
- sendAPIResponse(w, r, nil, "Not Found", http.StatusNotFound)
- }
- }
- func fileServer(r chi.Router, path string, root http.FileSystem) {
- fs := http.StripPrefix(path, http.FileServer(root))
- if path != "/" && path[len(path)-1] != '/' {
- r.Get(path, http.RedirectHandler(path+"/", http.StatusMovedPermanently).ServeHTTP)
- path += "/"
- }
- path += "*"
- r.Get(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- fs.ServeHTTP(w, r)
- }))
- }
|