options.go 714 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package rest
  2. import (
  3. "git.nspix.com/golang/micro/gateway/http"
  4. "gorm.io/gorm"
  5. "strings"
  6. )
  7. type Options struct {
  8. EnableNamespace bool
  9. DB *gorm.DB
  10. Prefix string
  11. TablePrefixes []string
  12. RemoveTablePrefix bool
  13. Formatter *Formatter
  14. Middleware []http.Middleware
  15. }
  16. type Option func(o *Options)
  17. func WithPrefix(prefix string) Option {
  18. return func(o *Options) {
  19. if !strings.HasPrefix(prefix, "/") {
  20. prefix = "/" + prefix
  21. }
  22. o.Prefix = prefix
  23. }
  24. }
  25. func WithDB(db *gorm.DB) Option {
  26. return func(o *Options) {
  27. o.DB = db
  28. }
  29. }
  30. func NewOptions() *Options {
  31. return &Options{
  32. Formatter: DefaultFormatter,
  33. Middleware: make([]http.Middleware, 0),
  34. }
  35. }