route.go 856 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package route
  5. import (
  6. "strconv"
  7. "github.com/gorilla/mux"
  8. "github.com/miniflux/miniflux/logger"
  9. )
  10. // Path returns the defined route based on given arguments.
  11. func Path(router *mux.Router, name string, args ...interface{}) string {
  12. route := router.Get(name)
  13. if route == nil {
  14. logger.Fatal("[Route] Route not found: %s", name)
  15. }
  16. var pairs []string
  17. for _, param := range args {
  18. switch param.(type) {
  19. case string:
  20. pairs = append(pairs, param.(string))
  21. case int64:
  22. val := param.(int64)
  23. pairs = append(pairs, strconv.FormatInt(val, 10))
  24. }
  25. }
  26. result, err := route.URLPath(pairs...)
  27. if err != nil {
  28. logger.Fatal("[Route] %v", err)
  29. }
  30. return result.String()
  31. }