utils.go 855 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package blademaster
  2. import (
  3. "os"
  4. "path"
  5. )
  6. func lastChar(str string) uint8 {
  7. if str == "" {
  8. panic("The length of the string can't be 0")
  9. }
  10. return str[len(str)-1]
  11. }
  12. func joinPaths(absolutePath, relativePath string) string {
  13. if relativePath == "" {
  14. return absolutePath
  15. }
  16. finalPath := path.Join(absolutePath, relativePath)
  17. appendSlash := lastChar(relativePath) == '/' && lastChar(finalPath) != '/'
  18. if appendSlash {
  19. return finalPath + "/"
  20. }
  21. return finalPath
  22. }
  23. func resolveAddress(addr []string) string {
  24. switch len(addr) {
  25. case 0:
  26. if port := os.Getenv("PORT"); port != "" {
  27. //debugPrint("Environment variable PORT=\"%s\"", port)
  28. return ":" + port
  29. }
  30. //debugPrint("Environment variable PORT is undefined. Using port :8080 by default")
  31. return ":8080"
  32. case 1:
  33. return addr[0]
  34. default:
  35. panic("too much parameters")
  36. }
  37. }