api.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package api
  2. import (
  3. "fmt"
  4. "github.com/go-playground/locales/en"
  5. ut "github.com/go-playground/universal-translator"
  6. vr "github.com/go-playground/validator/v10"
  7. sessionstore "github.com/porter-dev/porter/internal/auth"
  8. "github.com/porter-dev/porter/internal/oauth"
  9. "golang.org/x/oauth2"
  10. "gorm.io/gorm"
  11. "github.com/gorilla/sessions"
  12. "github.com/porter-dev/porter/internal/helm"
  13. "github.com/porter-dev/porter/internal/kubernetes"
  14. lr "github.com/porter-dev/porter/internal/logger"
  15. "github.com/porter-dev/porter/internal/repository"
  16. memory "github.com/porter-dev/porter/internal/repository/memory"
  17. "github.com/porter-dev/porter/internal/validator"
  18. "helm.sh/helm/v3/pkg/storage"
  19. "github.com/porter-dev/porter/internal/config"
  20. )
  21. // TestAgents are the k8s agents used for testing
  22. type TestAgents struct {
  23. HelmAgent *helm.Agent
  24. HelmTestStorageDriver *storage.Storage
  25. K8sAgent *kubernetes.Agent
  26. }
  27. // AppConfig is the configuration required for creating a new App
  28. type AppConfig struct {
  29. DB *gorm.DB
  30. Logger *lr.Logger
  31. Repository *repository.Repository
  32. ServerConf config.ServerConf
  33. // TestAgents if API is in testing mode
  34. TestAgents *TestAgents
  35. }
  36. // App represents an API instance with handler methods attached, a DB connection
  37. // and a logger instance
  38. type App struct {
  39. // Server configuration
  40. ServerConf config.ServerConf
  41. // Logger for logging
  42. Logger *lr.Logger
  43. // Repo implements a query repository
  44. Repo *repository.Repository
  45. // session store for cookie-based sessions
  46. Store sessions.Store
  47. // agents exposed for testing
  48. TestAgents *TestAgents
  49. // oauth-specific clients
  50. GithubConf *oauth2.Config
  51. db *gorm.DB
  52. validator *vr.Validate
  53. translator *ut.Translator
  54. }
  55. // New returns a new App instance
  56. func New(conf *AppConfig) (*App, error) {
  57. // create a new validator and translator
  58. validator := validator.New()
  59. en := en.New()
  60. uni := ut.New(en, en)
  61. translator, found := uni.GetTranslator("en")
  62. if !found {
  63. return nil, fmt.Errorf("could not find \"en\" translator")
  64. }
  65. app := &App{
  66. Logger: conf.Logger,
  67. Repo: conf.Repository,
  68. ServerConf: conf.ServerConf,
  69. TestAgents: conf.TestAgents,
  70. db: conf.DB,
  71. validator: validator,
  72. translator: &translator,
  73. }
  74. // if repository not specified, default to in-memory
  75. if app.Repo == nil {
  76. app.Repo = memory.NewRepository(true)
  77. }
  78. // create the session store
  79. store, err := sessionstore.NewStore(app.Repo, app.ServerConf)
  80. if err != nil {
  81. return nil, err
  82. }
  83. app.Store = store
  84. // if server config contains OAuth client info, create clients
  85. if sc := conf.ServerConf; sc.GithubClientID != "" && sc.GithubClientSecret != "" {
  86. app.GithubConf = oauth.NewGithubClient(&oauth.Config{
  87. ClientID: sc.GithubClientID,
  88. ClientSecret: sc.GithubClientSecret,
  89. Scopes: []string{"repo", "user", "read:user"},
  90. BaseURL: sc.ServerURL,
  91. })
  92. }
  93. return app, nil
  94. }
  95. // // New returns a new App instance
  96. // // TODO -- this should accept an app/server config
  97. // func New(
  98. // logger *lr.Logger,
  99. // db *gorm.DB,
  100. // repo *repository.Repository,
  101. // validator *validator.Validate,
  102. // store sessions.Store,
  103. // cookieName string,
  104. // testing bool,
  105. // isLocal bool,
  106. // githubConfig *oauth.Config,
  107. // serverConf config.ServerConf,
  108. // ) *App {
  109. // // for now, will just support the english translator from the
  110. // // validator/translations package
  111. // en := en.New()
  112. // uni := ut.New(en, en)
  113. // trans, _ := uni.GetTranslator("en")
  114. // var testAgents *TestAgents = nil
  115. // if testing {
  116. // memStorage := helm.StorageMap["memory"](nil, nil, "")
  117. // testAgents = &TestAgents{
  118. // HelmAgent: helm.GetAgentTesting(&helm.Form{}, nil, logger),
  119. // HelmTestStorageDriver: memStorage,
  120. // K8sAgent: kubernetes.GetAgentTesting(),
  121. // }
  122. // }
  123. // var oauthGithubConf *oauth2.Config
  124. // if githubConfig != nil {
  125. // oauthGithubConf = oauth.NewGithubClient(githubConfig)
  126. // }
  127. // return &App{
  128. // db: db,
  129. // logger: logger,
  130. // repo: repo,
  131. // validator: validator,
  132. // store: store,
  133. // translator: &trans,
  134. // cookieName: cookieName,
  135. // testing: testing,
  136. // isLocal: isLocal,
  137. // TestAgents: testAgents,
  138. // GithubConfig: oauthGithubConf,
  139. // ServerConf: serverConf,
  140. // }
  141. // }
  142. // // Logger returns the logger instance in use by App
  143. // func (app *App) Logger() *lr.Logger {
  144. // return app.logger
  145. // }