httpd.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Package httpd implements REST API and Web interface for SFTPGo.
  2. // REST API allows to manage users and quota and to get real time reports for the active connections
  3. // with possibility of forcibly closing a connection.
  4. // The OpenAPI 3 schema for the exposed API can be found inside the source tree:
  5. // https://github.com/drakkan/sftpgo/tree/master/api/schema/openapi.yaml
  6. // A basic Web interface to manage users and connections is provided too
  7. package httpd
  8. import (
  9. "fmt"
  10. "net/http"
  11. "path/filepath"
  12. "time"
  13. "github.com/drakkan/sftpgo/dataprovider"
  14. "github.com/drakkan/sftpgo/logger"
  15. "github.com/go-chi/chi"
  16. )
  17. const (
  18. logSender = "httpd"
  19. activeConnectionsPath = "/api/v1/connection"
  20. quotaScanPath = "/api/v1/quota_scan"
  21. userPath = "/api/v1/user"
  22. versionPath = "/api/v1/version"
  23. providerStatusPath = "/api/v1/providerstatus"
  24. dumpDataPath = "/api/v1/dumpdata"
  25. loadDataPath = "/api/v1/loaddata"
  26. metricsPath = "/metrics"
  27. webBasePath = "/web"
  28. webUsersPath = "/web/users"
  29. webUserPath = "/web/user"
  30. webConnectionsPath = "/web/connections"
  31. webStaticFilesPath = "/static"
  32. maxRestoreSize = 10485760 // 10 MB
  33. )
  34. var (
  35. router *chi.Mux
  36. dataProvider dataprovider.Provider
  37. backupsPath string
  38. )
  39. // Conf httpd daemon configuration
  40. type Conf struct {
  41. // The port used for serving HTTP requests. 0 disable the HTTP server. Default: 8080
  42. BindPort int `json:"bind_port" mapstructure:"bind_port"`
  43. // The address to listen on. A blank value means listen on all available network interfaces. Default: "127.0.0.1"
  44. BindAddress string `json:"bind_address" mapstructure:"bind_address"`
  45. // Path to the HTML web templates. This can be an absolute path or a path relative to the config dir
  46. TemplatesPath string `json:"templates_path" mapstructure:"templates_path"`
  47. // Path to the static files for the web interface. This can be an absolute path or a path relative to the config dir
  48. StaticFilesPath string `json:"static_files_path" mapstructure:"static_files_path"`
  49. // Path to the backup directory. This can be an absolute path or a path relative to the config dir
  50. BackupsPath string `json:"backups_path" mapstructure:"backups_path"`
  51. }
  52. // BackupData defines the structure for the backup/restore files
  53. type BackupData struct {
  54. Users []dataprovider.User `json:"users"`
  55. }
  56. type apiResponse struct {
  57. Error string `json:"error"`
  58. Message string `json:"message"`
  59. HTTPStatus int `json:"status"`
  60. }
  61. // SetDataProvider sets the data provider to use to fetch the data about users
  62. func SetDataProvider(provider dataprovider.Provider) {
  63. dataProvider = provider
  64. }
  65. // Initialize the HTTP server
  66. func (c Conf) Initialize(configDir string) error {
  67. logger.Debug(logSender, "", "initializing HTTP server with config %+v", c)
  68. backupsPath = c.BackupsPath
  69. if !filepath.IsAbs(backupsPath) {
  70. backupsPath = filepath.Join(configDir, backupsPath)
  71. }
  72. staticFilesPath := c.StaticFilesPath
  73. if !filepath.IsAbs(staticFilesPath) {
  74. staticFilesPath = filepath.Join(configDir, staticFilesPath)
  75. }
  76. templatesPath := c.TemplatesPath
  77. if !filepath.IsAbs(templatesPath) {
  78. templatesPath = filepath.Join(configDir, templatesPath)
  79. }
  80. loadTemplates(templatesPath)
  81. initializeRouter(staticFilesPath)
  82. httpServer := &http.Server{
  83. Addr: fmt.Sprintf("%s:%d", c.BindAddress, c.BindPort),
  84. Handler: router,
  85. ReadTimeout: 300 * time.Second,
  86. WriteTimeout: 300 * time.Second,
  87. MaxHeaderBytes: 1 << 16, // 64KB
  88. }
  89. return httpServer.ListenAndServe()
  90. }