app.go 827 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "os"
  6. "SIMANC-WCS/app"
  7. "SIMANC-WCS/config"
  8. )
  9. type App struct {
  10. ctx context.Context
  11. config.Config
  12. }
  13. func NewApp() *App {
  14. return &App{}
  15. }
  16. func (a *App) initApp() {
  17. // 初始化上下文
  18. app.Context = a.ctx
  19. // 初始化资源数据目录
  20. stat, err := os.Stat(app.DataPath)
  21. if err == nil {
  22. // 存在但不是文件夹时
  23. if !stat.IsDir() {
  24. log.Panicf("initApp: [%s] is not a directory", app.DataPath)
  25. }
  26. return
  27. }
  28. // 如果不是"不存在"的错误时, 即: 其他错误
  29. if !os.IsNotExist(err) {
  30. log.Panicf("initApp: %s", err)
  31. }
  32. if err = os.MkdirAll(app.DataPath, os.ModePerm); err != nil {
  33. log.Panicf("initApp: %s", err)
  34. }
  35. }
  36. // Exit 退出
  37. func (a *App) Exit() {
  38. os.Exit(0)
  39. }
  40. func (a *App) startup(ctx context.Context) {
  41. a.ctx = ctx
  42. a.initApp()
  43. }