options.go 652 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. Formatter *Formatter
  12. Middleware []http.Middleware
  13. }
  14. type Option func(o *Options)
  15. func WithPrefix(prefix string) Option {
  16. return func(o *Options) {
  17. if !strings.HasPrefix(prefix, "/") {
  18. prefix = "/" + prefix
  19. }
  20. o.Prefix = prefix
  21. }
  22. }
  23. func WithDB(db *gorm.DB) Option {
  24. return func(o *Options) {
  25. o.DB = db
  26. }
  27. }
  28. func NewOptions() *Options {
  29. return &Options{
  30. Formatter: DefaultFormatter,
  31. Middleware: make([]http.Middleware, 0),
  32. }
  33. }