handler.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package handlers
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/shared"
  5. "github.com/porter-dev/porter/internal/models"
  6. )
  7. type PorterHandler interface {
  8. Config() *shared.Config
  9. }
  10. type PorterHandlerWriter interface {
  11. PorterHandler
  12. WriteResult(w http.ResponseWriter, v interface{})
  13. }
  14. type PorterHandlerReader interface {
  15. PorterHandler
  16. DecodeAndValidate(w http.ResponseWriter, r *http.Request, v interface{})
  17. }
  18. type PorterHandlerReadWriter interface {
  19. PorterHandlerWriter
  20. PorterHandlerReader
  21. }
  22. // default
  23. // type PorterHandler struct {
  24. // config *shared.Config
  25. // decoderValidator shared.RequestDecoderValidator
  26. // writer shared.ResultWriter
  27. // }
  28. // handler needs:
  29. // - interface for decodervalidator+writer
  30. // - shared configuration
  31. // - writer
  32. // - context set (user, project, etc)
  33. // - standard error
  34. // notes:
  35. // decode and validate should happen above the handler itself. the scopes and strongly typed
  36. // "handlers" refer to an aggregation of application-logic. they should not contain any logic
  37. // for:
  38. // - error aggregation (so they accept api error interface)
  39. // - analytics
  40. // - authentication
  41. // - reading in required context (part of authentication)
  42. // ProjectScopedHandler()
  43. // - read project model from context
  44. // - so "Get Project" accepts a ProjectGetter, which calls readUser() and readProject()
  45. // The errors that a handler can throw should be defined in API spec
  46. type UserGetter interface {
  47. readUser() *models.User
  48. }
  49. type ProjectGetter interface {
  50. UserGetter
  51. readProject() *models.Project
  52. }