httpd.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. metricsPath = "/metrics"
  24. webBasePath = "/web"
  25. webUsersPath = "/web/users"
  26. webUserPath = "/web/user"
  27. webConnectionsPath = "/web/connections"
  28. webStaticFilesPath = "/static"
  29. )
  30. var (
  31. router *chi.Mux
  32. dataProvider dataprovider.Provider
  33. )
  34. // Conf httpd daemon configuration
  35. type Conf struct {
  36. // The port used for serving HTTP requests. 0 disable the HTTP server. Default: 8080
  37. BindPort int `json:"bind_port" mapstructure:"bind_port"`
  38. // The address to listen on. A blank value means listen on all available network interfaces. Default: "127.0.0.1"
  39. BindAddress string `json:"bind_address" mapstructure:"bind_address"`
  40. // Path to the HTML web templates. This can be an absolute path or a path relative to the config dir
  41. TemplatesPath string `json:"templates_path" mapstructure:"templates_path"`
  42. // Path to the static files for the web interface. This can be an absolute path or a path relative to the config dir
  43. StaticFilesPath string `json:"static_files_path" mapstructure:"static_files_path"`
  44. }
  45. type apiResponse struct {
  46. Error string `json:"error"`
  47. Message string `json:"message"`
  48. HTTPStatus int `json:"status"`
  49. }
  50. // SetDataProvider sets the data provider to use to fetch the data about users
  51. func SetDataProvider(provider dataprovider.Provider) {
  52. dataProvider = provider
  53. }
  54. // Initialize the HTTP server
  55. func (c Conf) Initialize(configDir string) error {
  56. logger.Debug(logSender, "", "initializing HTTP server with config %+v", c)
  57. staticFilesPath := c.StaticFilesPath
  58. if !filepath.IsAbs(staticFilesPath) {
  59. staticFilesPath = filepath.Join(configDir, staticFilesPath)
  60. }
  61. templatesPath := c.TemplatesPath
  62. if !filepath.IsAbs(templatesPath) {
  63. templatesPath = filepath.Join(configDir, templatesPath)
  64. }
  65. loadTemplates(templatesPath)
  66. initializeRouter(staticFilesPath)
  67. httpServer := &http.Server{
  68. Addr: fmt.Sprintf("%s:%d", c.BindAddress, c.BindPort),
  69. Handler: router,
  70. ReadTimeout: 300 * time.Second,
  71. WriteTimeout: 300 * time.Second,
  72. MaxHeaderBytes: 1 << 20, // 1MB
  73. }
  74. return httpServer.ListenAndServe()
  75. }