You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.9 KiB

  1. package weed_server
  2. import (
  3. "google.golang.org/grpc"
  4. "net/http"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/security"
  7. "github.com/chrislusf/seaweedfs/weed/storage"
  8. "github.com/spf13/viper"
  9. )
  10. type VolumeServer struct {
  11. SeedMasterNodes []string
  12. currentMaster string
  13. pulseSeconds int
  14. dataCenter string
  15. rack string
  16. store *storage.Store
  17. guard *security.Guard
  18. grpcDialOption grpc.DialOption
  19. needleMapKind storage.NeedleMapType
  20. FixJpgOrientation bool
  21. ReadRedirect bool
  22. compactionBytePerSecond int64
  23. }
  24. func NewVolumeServer(adminMux, publicMux *http.ServeMux, ip string,
  25. port int, publicUrl string,
  26. folders []string, maxCounts []int,
  27. needleMapKind storage.NeedleMapType,
  28. masterNodes []string, pulseSeconds int,
  29. dataCenter string, rack string,
  30. whiteList []string,
  31. fixJpgOrientation bool,
  32. readRedirect bool,
  33. compactionMBPerSecond int,
  34. ) *VolumeServer {
  35. v := viper.GetViper()
  36. signingKey := v.GetString("jwt.signing.key")
  37. v.SetDefault("jwt.signing.expires_after_seconds", 10)
  38. expiresAfterSec := v.GetInt("jwt.signing.expires_after_seconds")
  39. enableUiAccess := v.GetBool("access.ui")
  40. readSigningKey := v.GetString("jwt.signing.read.key")
  41. v.SetDefault("jwt.signing.read.expires_after_seconds", 60)
  42. readExpiresAfterSec := v.GetInt("jwt.signing.read.expires_after_seconds")
  43. vs := &VolumeServer{
  44. pulseSeconds: pulseSeconds,
  45. dataCenter: dataCenter,
  46. rack: rack,
  47. needleMapKind: needleMapKind,
  48. FixJpgOrientation: fixJpgOrientation,
  49. ReadRedirect: readRedirect,
  50. grpcDialOption: security.LoadClientTLS(viper.Sub("grpc"), "volume"),
  51. compactionBytePerSecond: int64(compactionMBPerSecond) * 1024 * 1024,
  52. }
  53. vs.SeedMasterNodes = masterNodes
  54. vs.store = storage.NewStore(vs.grpcDialOption, port, ip, publicUrl, folders, maxCounts, vs.needleMapKind)
  55. vs.guard = security.NewGuard(whiteList, signingKey, expiresAfterSec, readSigningKey, readExpiresAfterSec)
  56. handleStaticResources(adminMux)
  57. if signingKey == "" || enableUiAccess {
  58. // only expose the volume server details for safe environments
  59. adminMux.HandleFunc("/ui/index.html", vs.uiStatusHandler)
  60. adminMux.HandleFunc("/status", vs.guard.WhiteList(vs.statusHandler))
  61. adminMux.HandleFunc("/stats/counter", vs.guard.WhiteList(statsCounterHandler))
  62. adminMux.HandleFunc("/stats/memory", vs.guard.WhiteList(statsMemoryHandler))
  63. adminMux.HandleFunc("/stats/disk", vs.guard.WhiteList(vs.statsDiskHandler))
  64. }
  65. adminMux.HandleFunc("/", vs.privateStoreHandler)
  66. if publicMux != adminMux {
  67. // separated admin and public port
  68. handleStaticResources(publicMux)
  69. publicMux.HandleFunc("/", vs.publicReadOnlyHandler)
  70. }
  71. go vs.heartbeat()
  72. return vs
  73. }
  74. func (vs *VolumeServer) Shutdown() {
  75. glog.V(0).Infoln("Shutting down volume server...")
  76. vs.store.Close()
  77. glog.V(0).Infoln("Shut down successfully!")
  78. }