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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. )
  14. type ServerEventChan = async.UnboundChannel[ServerEvent]
  15. type ServerEvent interface {
  16. IsServerEvent() bool
  17. }
  18. type ExitEvent struct {
  19. ServerEvent
  20. Err error
  21. }
  22. type Server struct {
  23. cfg types.Config
  24. httpSrv *http.Server
  25. svc *services.Service
  26. eventChan *ServerEventChan
  27. auth *auth.Auth
  28. v1Svr *v1.Server
  29. }
  30. func NewServer(cfg types.Config, svc *services.Service) *Server {
  31. return &Server{
  32. cfg: cfg,
  33. svc: svc,
  34. eventChan: async.NewUnboundChannel[ServerEvent](),
  35. v1Svr: v1.NewServer(&cfg, svc),
  36. }
  37. }
  38. func (s *Server) Start() *ServerEventChan {
  39. go func() {
  40. if !s.cfg.Enabled {
  41. return
  42. }
  43. engine := gin.New()
  44. s.httpSrv = &http.Server{
  45. Addr: s.cfg.Listen,
  46. Handler: engine,
  47. }
  48. s.auth = auth.New(&s.cfg)
  49. s.httpSrv.TLSConfig = &tls.Config{
  50. GetConfigForClient: s.auth.TLSConfigSelector,
  51. }
  52. s.v1Svr.InitRouters(engine.Group("/v1"), s.auth)
  53. logger.Infof("start serving http at: %s", s.cfg.Listen)
  54. err := s.httpSrv.ListenAndServeTLS("", "")
  55. s.eventChan.Send(ExitEvent{Err: err})
  56. }()
  57. return s.eventChan
  58. }
  59. func (s *Server) Stop() {
  60. if s.httpSrv == nil {
  61. s.eventChan.Send(ExitEvent{})
  62. return
  63. }
  64. s.httpSrv.Shutdown(context.Background())
  65. }

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