2019-10-07 16:19:01 +00:00
|
|
|
package httpd
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2019-11-14 17:48:01 +00:00
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
2019-07-20 10:26:52 +00:00
|
|
|
"github.com/drakkan/sftpgo/logger"
|
|
|
|
"github.com/drakkan/sftpgo/sftpd"
|
2019-08-08 08:01:33 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2019-07-20 10:26:52 +00:00
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/chi/middleware"
|
|
|
|
"github.com/go-chi/render"
|
2019-09-13 16:45:36 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
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
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
func initializeRouter(staticFilesPath string) {
|
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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
|
|
|
router.Get(webBasePath, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Redirect(w, r, webUsersPath, http.StatusMovedPermanently)
|
|
|
|
})
|
|
|
|
|
2019-09-13 16:45:36 +00:00
|
|
|
router.Handle(metricsPath, promhttp.Handler())
|
|
|
|
|
2019-08-08 08:01:33 +00:00
|
|
|
router.Get(versionPath, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
render.JSON(w, r, utils.GetAppVersion())
|
|
|
|
})
|
|
|
|
|
2019-11-14 17:48:01 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
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) {
|
2019-08-24 12:41:15 +00:00
|
|
|
handleCloseConnection(w, r)
|
2019-07-20 10:26:52 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
2019-10-07 16:19:01 +00:00
|
|
|
|
|
|
|
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)
|
2019-10-08 06:20:26 +00:00
|
|
|
fileServer(router, webStaticFilesPath, http.Dir(staticFilesPath))
|
2019-10-07 16:19:01 +00:00
|
|
|
})
|
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
|
|
|
|
}
|
|
|
|
if sftpd.CloseActiveConnection(connectionID) {
|
|
|
|
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) {
|
|
|
|
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)
|
|
|
|
}))
|
|
|
|
}
|