api.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. DOConf *oauth2.Config
  58. db *gorm.DB
  59. validator *vr.Validate
  60. translator *ut.Translator
  61. }
  62. // New returns a new App instance
  63. func New(conf *AppConfig) (*App, error) {
  64. // create a new validator and translator
  65. validator := validator.New()
  66. en := en.New()
  67. uni := ut.New(en, en)
  68. translator, found := uni.GetTranslator("en")
  69. if !found {
  70. return nil, fmt.Errorf("could not find \"en\" translator")
  71. }
  72. app := &App{
  73. Logger: conf.Logger,
  74. Repo: conf.Repository,
  75. ServerConf: conf.ServerConf,
  76. RedisConf: conf.RedisConf,
  77. DBConf: conf.DBConf,
  78. TestAgents: conf.TestAgents,
  79. db: conf.DB,
  80. validator: validator,
  81. translator: &translator,
  82. }
  83. // if repository not specified, default to in-memory
  84. if app.Repo == nil {
  85. app.Repo = memory.NewRepository(true)
  86. }
  87. // create the session store
  88. store, err := sessionstore.NewStore(app.Repo, app.ServerConf)
  89. if err != nil {
  90. return nil, err
  91. }
  92. app.Store = store
  93. // if server config contains OAuth client info, create clients
  94. if sc := conf.ServerConf; sc.GithubClientID != "" && sc.GithubClientSecret != "" {
  95. app.GithubConf = oauth.NewGithubClient(&oauth.Config{
  96. ClientID: sc.GithubClientID,
  97. ClientSecret: sc.GithubClientSecret,
  98. Scopes: []string{"repo", "user", "read:user"},
  99. BaseURL: sc.ServerURL,
  100. })
  101. }
  102. if sc := conf.ServerConf; sc.DOClientID != "" && sc.DOClientSecret != "" {
  103. app.DOConf = oauth.NewDigitalOceanClient(&oauth.Config{
  104. ClientID: sc.DOClientID,
  105. ClientSecret: sc.DOClientSecret,
  106. Scopes: []string{"read", "write"},
  107. BaseURL: sc.ServerURL,
  108. })
  109. }
  110. return app, nil
  111. }