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

server.go 1.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package http
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "net/http"
  6. "github.com/gin-gonic/gin"
  7. "gitlink.org.cn/cloudream/common/pkgs/async"
  8. "gitlink.org.cn/cloudream/common/pkgs/logger"
  9. "gitlink.org.cn/cloudream/jcs-pub/client/internal/http/auth"
  10. "gitlink.org.cn/cloudream/jcs-pub/client/internal/http/types"
  11. v1 "gitlink.org.cn/cloudream/jcs-pub/client/internal/http/v1"
  12. "gitlink.org.cn/cloudream/jcs-pub/client/internal/services"
  13. "golang.org/x/net/http2"
  14. )
  15. type ServerEventChan = async.UnboundChannel[ServerEvent]
  16. type ServerEvent interface {
  17. IsServerEvent() bool
  18. }
  19. type ExitEvent struct {
  20. ServerEvent
  21. Err error
  22. }
  23. type Server struct {
  24. cfg types.Config
  25. httpSrv *http.Server
  26. svc *services.Service
  27. eventChan *ServerEventChan
  28. auth *auth.Auth
  29. v1Svr *v1.Server
  30. }
  31. func NewServer(cfg types.Config, svc *services.Service) *Server {
  32. return &Server{
  33. cfg: cfg,
  34. svc: svc,
  35. eventChan: async.NewUnboundChannel[ServerEvent](),
  36. v1Svr: v1.NewServer(&cfg, svc),
  37. }
  38. }
  39. func (s *Server) Start() *ServerEventChan {
  40. go func() {
  41. if !s.cfg.Enabled {
  42. return
  43. }
  44. engine := gin.New()
  45. s.httpSrv = &http.Server{
  46. Addr: s.cfg.Listen,
  47. Handler: engine,
  48. }
  49. s.auth = auth.New(&s.cfg)
  50. s.httpSrv.TLSConfig = &tls.Config{
  51. GetConfigForClient: s.auth.TLSConfigSelector,
  52. }
  53. http2.ConfigureServer(s.httpSrv, &http2.Server{})
  54. s.v1Svr.InitRouters(engine.Group("/v1"), s.auth)
  55. logger.Infof("start serving http at: %s", s.cfg.Listen)
  56. err := s.httpSrv.ListenAndServeTLS("", "")
  57. s.eventChan.Send(ExitEvent{Err: err})
  58. }()
  59. return s.eventChan
  60. }
  61. func (s *Server) Stop() {
  62. if s.httpSrv == nil {
  63. s.eventChan.Send(ExitEvent{})
  64. return
  65. }
  66. s.httpSrv.Shutdown(context.Background())
  67. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。