api.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. isLocal bool
  34. TestAgents *TestAgents
  35. GithubConfig *oauth2.Config
  36. }
  37. // New returns a new App instance
  38. // TODO -- this should accept an app/server config
  39. func New(
  40. logger *lr.Logger,
  41. db *gorm.DB,
  42. repo *repository.Repository,
  43. validator *validator.Validate,
  44. store sessions.Store,
  45. cookieName string,
  46. testing bool,
  47. isLocal bool,
  48. githubConfig *oauth.Config,
  49. ) *App {
  50. // for now, will just support the english translator from the
  51. // validator/translations package
  52. en := en.New()
  53. uni := ut.New(en, en)
  54. trans, _ := uni.GetTranslator("en")
  55. var testAgents *TestAgents = nil
  56. if testing {
  57. memStorage := helm.StorageMap["memory"](nil, nil, "")
  58. testAgents = &TestAgents{
  59. HelmAgent: helm.GetAgentTesting(&helm.Form{}, nil, logger),
  60. HelmTestStorageDriver: memStorage,
  61. K8sAgent: kubernetes.GetAgentTesting(),
  62. }
  63. }
  64. var oauthGithubConf *oauth2.Config
  65. if githubConfig != nil {
  66. oauthGithubConf = oauth.NewGithubClient(githubConfig)
  67. }
  68. return &App{
  69. db: db,
  70. logger: logger,
  71. repo: repo,
  72. validator: validator,
  73. store: store,
  74. translator: &trans,
  75. cookieName: cookieName,
  76. testing: testing,
  77. isLocal: isLocal,
  78. TestAgents: testAgents,
  79. GithubConfig: oauthGithubConf,
  80. }
  81. }
  82. // Logger returns the logger instance in use by App
  83. func (app *App) Logger() *lr.Logger {
  84. return app.logger
  85. }