2019-10-07 16:19:01 +00:00
|
|
|
package httpd
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-04-21 17:24:38 +00:00
|
|
|
"strings"
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/chi/middleware"
|
|
|
|
"github.com/go-chi/render"
|
2020-05-06 17:36:34 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
"github.com/drakkan/sftpgo/common"
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/logger"
|
2020-06-18 21:53:38 +00:00
|
|
|
"github.com/drakkan/sftpgo/metrics"
|
2020-06-19 15:08:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/version"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// GetHTTPRouter returns the configured HTTP handler
|
2019-07-20 10:26:52 +00:00
|
|
|
func GetHTTPRouter() http.Handler {
|
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
2020-06-18 21:53:38 +00:00
|
|
|
func initializeRouter(staticFilesPath string, enableProfiler, enableWebAdmin bool) {
|
2019-07-20 10:26:52 +00:00
|
|
|
router = chi.NewRouter()
|
|
|
|
router.Use(middleware.RequestID)
|
|
|
|
router.Use(middleware.RealIP)
|
|
|
|
router.Use(logger.NewStructuredLogger(logger.GetLogger()))
|
|
|
|
router.Use(middleware.Recoverer)
|
|
|
|
|
2020-06-18 21:53:38 +00:00
|
|
|
if enableProfiler {
|
2020-03-15 14:16:35 +00:00
|
|
|
logger.InfoToConsole("enabling the built-in profiler")
|
|
|
|
logger.Info(logSender, "", "enabling the built-in profiler")
|
|
|
|
router.Mount(pprofBasePath, middleware.Profiler())
|
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
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)
|
|
|
|
}))
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
router.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Redirect(w, r, webUsersPath, http.StatusMovedPermanently)
|
|
|
|
})
|
|
|
|
|
2020-02-03 23:08:00 +00:00
|
|
|
router.Group(func(router chi.Router) {
|
|
|
|
router.Use(checkAuth)
|
|
|
|
|
|
|
|
router.Get(webBasePath, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Redirect(w, r, webUsersPath, http.StatusMovedPermanently)
|
|
|
|
})
|
|
|
|
|
2020-06-18 21:53:38 +00:00
|
|
|
metrics.AddMetricsEndpoint(metricsPath, router)
|
2020-02-03 23:08:00 +00:00
|
|
|
|
|
|
|
router.Get(versionPath, func(w http.ResponseWriter, r *http.Request) {
|
2020-06-19 15:08:51 +00:00
|
|
|
render.JSON(w, r, version.Get())
|
2020-02-03 23:08:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
router.Get(providerStatusPath, func(w http.ResponseWriter, r *http.Request) {
|
2020-07-08 17:59:31 +00:00
|
|
|
err := dataprovider.GetProviderStatus()
|
2020-02-03 23:08:00 +00:00
|
|
|
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) {
|
2020-07-24 21:39:38 +00:00
|
|
|
render.JSON(w, r, common.Connections.GetStats())
|
2020-02-03 23:08:00 +00:00
|
|
|
})
|
|
|
|
|
2020-04-21 17:24:38 +00:00
|
|
|
router.Delete(activeConnectionsPath+"/{connectionID}", handleCloseConnection)
|
|
|
|
router.Get(quotaScanPath, getQuotaScans)
|
|
|
|
router.Post(quotaScanPath, startQuotaScan)
|
2020-06-07 21:30:18 +00:00
|
|
|
router.Get(quotaScanVFolderPath, getVFolderQuotaScans)
|
|
|
|
router.Post(quotaScanVFolderPath, startVFolderQuotaScan)
|
2020-04-21 17:24:38 +00:00
|
|
|
router.Get(userPath, getUsers)
|
|
|
|
router.Post(userPath, addUser)
|
|
|
|
router.Get(userPath+"/{userID}", getUserByID)
|
|
|
|
router.Put(userPath+"/{userID}", updateUser)
|
|
|
|
router.Delete(userPath+"/{userID}", deleteUser)
|
2020-06-07 21:30:18 +00:00
|
|
|
router.Get(folderPath, getFolders)
|
|
|
|
router.Post(folderPath, addFolder)
|
|
|
|
router.Delete(folderPath, deleteFolderByPath)
|
2020-04-21 17:24:38 +00:00
|
|
|
router.Get(dumpDataPath, dumpData)
|
|
|
|
router.Get(loadDataPath, loadData)
|
2020-06-20 10:38:04 +00:00
|
|
|
router.Put(updateUsedQuotaPath, updateUserQuotaUsage)
|
|
|
|
router.Put(updateFolderUsedQuotaPath, updateVFolderQuotaUsage)
|
2020-06-18 21:53:38 +00:00
|
|
|
if enableWebAdmin {
|
|
|
|
router.Get(webUsersPath, handleGetWebUsers)
|
|
|
|
router.Get(webUserPath, handleWebAddUserGet)
|
|
|
|
router.Get(webUserPath+"/{userID}", handleWebUpdateUserGet)
|
|
|
|
router.Post(webUserPath, handleWebAddUserPost)
|
|
|
|
router.Post(webUserPath+"/{userID}", handleWebUpdateUserPost)
|
|
|
|
router.Get(webConnectionsPath, handleWebGetConnections)
|
|
|
|
router.Get(webFoldersPath, handleWebGetFolders)
|
|
|
|
router.Get(webFolderPath, handleWebAddFolderGet)
|
|
|
|
router.Post(webFolderPath, handleWebAddFolderPost)
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
})
|
|
|
|
|
2020-06-18 21:53:38 +00:00
|
|
|
if enableWebAdmin {
|
|
|
|
router.Group(func(router chi.Router) {
|
|
|
|
compressor := middleware.NewCompressor(5)
|
|
|
|
router.Use(compressor.Handler)
|
|
|
|
fileServer(router, webStaticFilesPath, http.Dir(staticFilesPath))
|
|
|
|
})
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-08-08 19:42:07 +00:00
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
func handleCloseConnection(w http.ResponseWriter, r *http.Request) {
|
2019-08-08 19:42:07 +00:00
|
|
|
connectionID := chi.URLParam(r, "connectionID")
|
|
|
|
if connectionID == "" {
|
|
|
|
sendAPIResponse(w, r, nil, "connectionID is mandatory", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
if common.Connections.Close(connectionID) {
|
2019-08-08 19:42:07 +00:00
|
|
|
sendAPIResponse(w, r, nil, "Connection closed", http.StatusOK)
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, nil, "Not Found", http.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
|
|
|
|
func fileServer(r chi.Router, path string, root http.FileSystem) {
|
|
|
|
if path != "/" && path[len(path)-1] != '/' {
|
|
|
|
r.Get(path, http.RedirectHandler(path+"/", http.StatusMovedPermanently).ServeHTTP)
|
|
|
|
path += "/"
|
|
|
|
}
|
|
|
|
path += "*"
|
|
|
|
|
2020-04-21 17:24:38 +00:00
|
|
|
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
rctx := chi.RouteContext(r.Context())
|
|
|
|
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
|
|
|
|
fs := http.StripPrefix(pathPrefix, http.FileServer(root))
|
2019-10-07 16:19:01 +00:00
|
|
|
fs.ServeHTTP(w, r)
|
2020-04-21 17:24:38 +00:00
|
|
|
})
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|