api.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. RedisConf *config.RedisConf
  34. DBConf config.DBConf
  35. // TestAgents if API is in testing mode
  36. TestAgents *TestAgents
  37. }
  38. // App represents an API instance with handler methods attached, a DB connection
  39. // and a logger instance
  40. type App struct {
  41. // Server configuration
  42. ServerConf config.ServerConf
  43. // Logger for logging
  44. Logger *lr.Logger
  45. // Repo implements a query repository
  46. Repo *repository.Repository
  47. // session store for cookie-based sessions
  48. Store sessions.Store
  49. // agents exposed for testing
  50. TestAgents *TestAgents
  51. // redis client for redis connection
  52. RedisConf *config.RedisConf
  53. // config for db
  54. DBConf config.DBConf
  55. // oauth-specific clients
  56. GithubConf *oauth2.Config
  57. db *gorm.DB
  58. validator *vr.Validate
  59. translator *ut.Translator
  60. }
  61. // New returns a new App instance
  62. func New(conf *AppConfig) (*App, error) {
  63. // create a new validator and translator
  64. validator := validator.New()
  65. en := en.New()
  66. uni := ut.New(en, en)
  67. translator, found := uni.GetTranslator("en")
  68. if !found {
  69. return nil, fmt.Errorf("could not find \"en\" translator")
  70. }
  71. app := &App{
  72. Logger: conf.Logger,
  73. Repo: conf.Repository,
  74. ServerConf: conf.ServerConf,
  75. RedisConf: conf.RedisConf,
  76. DBConf: conf.DBConf,
  77. TestAgents: conf.TestAgents,
  78. db: conf.DB,
  79. validator: validator,
  80. translator: &translator,
  81. }
  82. // if repository not specified, default to in-memory
  83. if app.Repo == nil {
  84. app.Repo = memory.NewRepository(true)
  85. }
  86. // create the session store
  87. store, err := sessionstore.NewStore(app.Repo, app.ServerConf)
  88. if err != nil {
  89. return nil, err
  90. }
  91. app.Store = store
  92. // if server config contains OAuth client info, create clients
  93. if sc := conf.ServerConf; sc.GithubClientID != "" && sc.GithubClientSecret != "" {
  94. app.GithubConf = oauth.NewGithubClient(&oauth.Config{
  95. ClientID: sc.GithubClientID,
  96. ClientSecret: sc.GithubClientSecret,
  97. Scopes: []string{"repo", "user", "read:user"},
  98. BaseURL: sc.ServerURL,
  99. })
  100. }
  101. return app, nil
  102. }