router.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package api
  2. import (
  3. "net/http"
  4. "github.com/drakkan/sftpgo/logger"
  5. "github.com/drakkan/sftpgo/sftpd"
  6. "github.com/go-chi/chi"
  7. "github.com/go-chi/chi/middleware"
  8. "github.com/go-chi/render"
  9. )
  10. // GetHTTPRouter returns the configured HTTP handler
  11. func GetHTTPRouter() http.Handler {
  12. return router
  13. }
  14. func initializeRouter() {
  15. router = chi.NewRouter()
  16. router.Use(middleware.RequestID)
  17. router.Use(middleware.RealIP)
  18. router.Use(logger.NewStructuredLogger(logger.GetLogger()))
  19. router.Use(middleware.Recoverer)
  20. router.NotFound(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  21. sendAPIResponse(w, r, nil, "Not Found", http.StatusNotFound)
  22. }))
  23. router.MethodNotAllowed(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  24. sendAPIResponse(w, r, nil, "Method not allowed", http.StatusMethodNotAllowed)
  25. }))
  26. router.Get(activeConnectionsPath, func(w http.ResponseWriter, r *http.Request) {
  27. render.JSON(w, r, sftpd.GetConnectionsStats())
  28. })
  29. router.Delete(activeConnectionsPath+"/{connectionID}", func(w http.ResponseWriter, r *http.Request) {
  30. connectionID := chi.URLParam(r, "connectionID")
  31. if connectionID == "" {
  32. sendAPIResponse(w, r, nil, "connectionID is mandatory", http.StatusBadRequest)
  33. return
  34. }
  35. if sftpd.CloseActiveConnection(connectionID) {
  36. sendAPIResponse(w, r, nil, "Connection closed", http.StatusOK)
  37. } else {
  38. sendAPIResponse(w, r, nil, "Not Found", http.StatusNotFound)
  39. }
  40. })
  41. router.Get(quotaScanPath, func(w http.ResponseWriter, r *http.Request) {
  42. getQuotaScans(w, r)
  43. })
  44. router.Post(quotaScanPath, func(w http.ResponseWriter, r *http.Request) {
  45. startQuotaScan(w, r)
  46. })
  47. router.Get(userPath, func(w http.ResponseWriter, r *http.Request) {
  48. getUsers(w, r)
  49. })
  50. router.Post(userPath, func(w http.ResponseWriter, r *http.Request) {
  51. addUser(w, r)
  52. })
  53. router.Get(userPath+"/{userID}", func(w http.ResponseWriter, r *http.Request) {
  54. getUserByID(w, r)
  55. })
  56. router.Put(userPath+"/{userID}", func(w http.ResponseWriter, r *http.Request) {
  57. updateUser(w, r)
  58. })
  59. router.Delete(userPath+"/{userID}", func(w http.ResponseWriter, r *http.Request) {
  60. deleteUser(w, r)
  61. })
  62. }