api.go 3.0 KB

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