api.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. )
  16. // TestAgents are the k8s agents used for testing
  17. type TestAgents struct {
  18. HelmAgent *helm.Agent
  19. HelmTestStorageDriver *storage.Storage
  20. K8sAgent *kubernetes.Agent
  21. }
  22. // App represents an API instance with handler methods attached, a DB connection
  23. // and a logger instance
  24. type App struct {
  25. db *gorm.DB
  26. logger *lr.Logger
  27. repo *repository.Repository
  28. validator *validator.Validate
  29. store sessions.Store
  30. translator *ut.Translator
  31. cookieName string
  32. testing bool
  33. TestAgents *TestAgents
  34. GithubConfig *oauth2.Config
  35. }
  36. // New returns a new App instance
  37. func New(
  38. logger *lr.Logger,
  39. db *gorm.DB,
  40. repo *repository.Repository,
  41. validator *validator.Validate,
  42. store sessions.Store,
  43. cookieName string,
  44. testing bool,
  45. githubConfig *oauth.Config,
  46. ) *App {
  47. // for now, will just support the english translator from the
  48. // validator/translations package
  49. en := en.New()
  50. uni := ut.New(en, en)
  51. trans, _ := uni.GetTranslator("en")
  52. var testAgents *TestAgents = nil
  53. if testing {
  54. memStorage := helm.StorageMap["memory"](nil, nil, "")
  55. testAgents = &TestAgents{
  56. HelmAgent: helm.GetAgentTesting(&helm.Form{}, nil, logger),
  57. HelmTestStorageDriver: memStorage,
  58. K8sAgent: kubernetes.GetAgentTesting(),
  59. }
  60. }
  61. var oauthGithubConf *oauth2.Config
  62. if githubConfig != nil {
  63. oauthGithubConf = oauth.NewGithubClient(githubConfig)
  64. }
  65. return &App{
  66. db: db,
  67. logger: logger,
  68. repo: repo,
  69. validator: validator,
  70. store: store,
  71. translator: &trans,
  72. cookieName: cookieName,
  73. testing: testing,
  74. TestAgents: testAgents,
  75. GithubConfig: oauthGithubConf,
  76. }
  77. }
  78. // Logger returns the logger instance in use by App
  79. func (app *App) Logger() *lr.Logger {
  80. return app.logger
  81. }