api.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "github.com/go-redis/redis/v8"
  8. sessionstore "github.com/porter-dev/porter/internal/auth"
  9. "github.com/porter-dev/porter/internal/oauth"
  10. "golang.org/x/oauth2"
  11. "gorm.io/gorm"
  12. "github.com/gorilla/sessions"
  13. "github.com/porter-dev/porter/internal/helm"
  14. "github.com/porter-dev/porter/internal/kubernetes"
  15. lr "github.com/porter-dev/porter/internal/logger"
  16. "github.com/porter-dev/porter/internal/repository"
  17. memory "github.com/porter-dev/porter/internal/repository/memory"
  18. "github.com/porter-dev/porter/internal/validator"
  19. "helm.sh/helm/v3/pkg/storage"
  20. "github.com/porter-dev/porter/internal/config"
  21. )
  22. // TestAgents are the k8s agents used for testing
  23. type TestAgents struct {
  24. HelmAgent *helm.Agent
  25. HelmTestStorageDriver *storage.Storage
  26. K8sAgent *kubernetes.Agent
  27. }
  28. // AppConfig is the configuration required for creating a new App
  29. type AppConfig struct {
  30. DB *gorm.DB
  31. Logger *lr.Logger
  32. Repository *repository.Repository
  33. ServerConf config.ServerConf
  34. RedisClient *redis.Client
  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 conf for redis connection
  52. RedisClient *redis.Client
  53. // oauth-specific clients
  54. GithubConf *oauth2.Config
  55. db *gorm.DB
  56. validator *vr.Validate
  57. translator *ut.Translator
  58. }
  59. // New returns a new App instance
  60. func New(conf *AppConfig) (*App, error) {
  61. // create a new validator and translator
  62. validator := validator.New()
  63. en := en.New()
  64. uni := ut.New(en, en)
  65. translator, found := uni.GetTranslator("en")
  66. if !found {
  67. return nil, fmt.Errorf("could not find \"en\" translator")
  68. }
  69. app := &App{
  70. Logger: conf.Logger,
  71. Repo: conf.Repository,
  72. ServerConf: conf.ServerConf,
  73. RedisClient: conf.RedisClient,
  74. TestAgents: conf.TestAgents,
  75. db: conf.DB,
  76. validator: validator,
  77. translator: &translator,
  78. }
  79. // if repository not specified, default to in-memory
  80. if app.Repo == nil {
  81. app.Repo = memory.NewRepository(true)
  82. }
  83. // create the session store
  84. store, err := sessionstore.NewStore(app.Repo, app.ServerConf)
  85. if err != nil {
  86. return nil, err
  87. }
  88. app.Store = store
  89. // if server config contains OAuth client info, create clients
  90. if sc := conf.ServerConf; sc.GithubClientID != "" && sc.GithubClientSecret != "" {
  91. app.GithubConf = oauth.NewGithubClient(&oauth.Config{
  92. ClientID: sc.GithubClientID,
  93. ClientSecret: sc.GithubClientSecret,
  94. Scopes: []string{"repo", "user", "read:user"},
  95. BaseURL: sc.ServerURL,
  96. })
  97. }
  98. return app, nil
  99. }