api.go 2.2 KB

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