api.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package api
  2. import (
  3. "github.com/go-playground/locales/en"
  4. ut "github.com/go-playground/universal-translator"
  5. "github.com/go-playground/validator/v10"
  6. "github.com/porter-dev/porter/internal/oauth"
  7. "golang.org/x/oauth2"
  8. "gorm.io/gorm"
  9. "github.com/gorilla/sessions"
  10. "github.com/porter-dev/porter/internal/helm"
  11. "github.com/porter-dev/porter/internal/kubernetes"
  12. lr "github.com/porter-dev/porter/internal/logger"
  13. "github.com/porter-dev/porter/internal/repository"
  14. "helm.sh/helm/v3/pkg/storage"
  15. "github.com/porter-dev/porter/internal/config"
  16. )
  17. // TestAgents are the k8s agents used for testing
  18. type TestAgents struct {
  19. HelmAgent *helm.Agent
  20. HelmTestStorageDriver *storage.Storage
  21. K8sAgent *kubernetes.Agent
  22. }
  23. // App represents an API instance with handler methods attached, a DB connection
  24. // and a logger instance
  25. type App struct {
  26. db *gorm.DB
  27. logger *lr.Logger
  28. repo *repository.Repository
  29. validator *validator.Validate
  30. store sessions.Store
  31. translator *ut.Translator
  32. cookieName string
  33. testing bool
  34. isLocal bool
  35. ServerConf config.ServerConf
  36. TestAgents *TestAgents
  37. GithubConfig *oauth2.Config
  38. }
  39. // New returns a new App instance
  40. // TODO -- this should accept an app/server config
  41. func New(
  42. logger *lr.Logger,
  43. db *gorm.DB,
  44. repo *repository.Repository,
  45. validator *validator.Validate,
  46. store sessions.Store,
  47. cookieName string,
  48. testing bool,
  49. isLocal bool,
  50. githubConfig *oauth.Config,
  51. serverConf config.ServerConf,
  52. ) *App {
  53. // for now, will just support the english translator from the
  54. // validator/translations package
  55. en := en.New()
  56. uni := ut.New(en, en)
  57. trans, _ := uni.GetTranslator("en")
  58. var testAgents *TestAgents = nil
  59. if testing {
  60. memStorage := helm.StorageMap["memory"](nil, nil, "")
  61. testAgents = &TestAgents{
  62. HelmAgent: helm.GetAgentTesting(&helm.Form{}, nil, logger),
  63. HelmTestStorageDriver: memStorage,
  64. K8sAgent: kubernetes.GetAgentTesting(),
  65. }
  66. }
  67. var oauthGithubConf *oauth2.Config
  68. if githubConfig != nil {
  69. oauthGithubConf = oauth.NewGithubClient(githubConfig)
  70. }
  71. return &App{
  72. db: db,
  73. logger: logger,
  74. repo: repo,
  75. validator: validator,
  76. store: store,
  77. translator: &trans,
  78. cookieName: cookieName,
  79. testing: testing,
  80. isLocal: isLocal,
  81. TestAgents: testAgents,
  82. GithubConfig: oauthGithubConf,
  83. ServerConf: serverConf,
  84. }
  85. }
  86. // Logger returns the logger instance in use by App
  87. func (app *App) Logger() *lr.Logger {
  88. return app.logger
  89. }