service.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package service
  2. import (
  3. "context"
  4. "sync"
  5. "go-common/app/admin/main/appstatic/conf"
  6. "go-common/app/admin/main/appstatic/dao"
  7. "go-common/library/log"
  8. "github.com/jinzhu/gorm"
  9. )
  10. // Service biz service def.
  11. type Service struct {
  12. c *conf.Config
  13. dao *dao.Dao
  14. DB *gorm.DB
  15. waiter *sync.WaitGroup
  16. daoClosed bool // logic close the dao's DB
  17. MaxSize int64 // max size supported for the file to upload
  18. }
  19. // New new a Service and return.
  20. func New(c *conf.Config) (s *Service) {
  21. s = &Service{
  22. c: c,
  23. dao: dao.New(c),
  24. daoClosed: false,
  25. waiter: new(sync.WaitGroup),
  26. }
  27. s.DB = s.dao.DB
  28. if s.c.Cfg.Storage == "nas" {
  29. s.MaxSize = 200 * 1024 * 1024 // 200M NAS
  30. } else {
  31. s.MaxSize = 20 * 1024 * 1024 // 20M BFS
  32. }
  33. return s
  34. }
  35. // Ping check dao health.
  36. func (s *Service) Ping(c context.Context) (err error) {
  37. return
  38. }
  39. // Wait wait all closed.
  40. func (s *Service) Wait() {
  41. if s.dao != nil {
  42. s.daoClosed = true
  43. log.Info("Dao is logically closed!")
  44. }
  45. log.Info("Wait waiter!")
  46. s.waiter.Wait()
  47. }
  48. // Close close all dao.
  49. func (s *Service) Close() {
  50. log.Info("Close Dao physically!")
  51. s.dao.Close()
  52. log.Info("Service Closed!")
  53. }