A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

63 lines
1.4 KiB

  1. /*
  2. * Copyright © 2018, 2020-2021 A Bunch Tell LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. package config
  11. import (
  12. "github.com/writeas/web-core/log"
  13. "golang.org/x/net/idna"
  14. "net/http"
  15. "net/url"
  16. "strings"
  17. "time"
  18. )
  19. // FriendlyHost returns the app's Host sans any schema
  20. func (ac AppCfg) FriendlyHost() string {
  21. rawHost := ac.Host[strings.Index(ac.Host, "://")+len("://"):]
  22. u, err := url.Parse(ac.Host)
  23. if err != nil {
  24. log.Error("url.Parse failed on %s: %s", ac.Host, err)
  25. return rawHost
  26. }
  27. d, err := idna.ToUnicode(u.Hostname())
  28. if err != nil {
  29. log.Error("idna.ToUnicode failed on %s: %s", ac.Host, err)
  30. return rawHost
  31. }
  32. res := d
  33. if u.Port() != "" {
  34. res += ":" + u.Port()
  35. }
  36. return res
  37. }
  38. func (ac AppCfg) CanCreateBlogs(currentlyUsed uint64) bool {
  39. if ac.MaxBlogs <= 0 {
  40. return true
  41. }
  42. return int(currentlyUsed) < ac.MaxBlogs
  43. }
  44. // OrDefaultString returns input or a default value if input is empty.
  45. func OrDefaultString(input, defaultValue string) string {
  46. if len(input) == 0 {
  47. return defaultValue
  48. }
  49. return input
  50. }
  51. // DefaultHTTPClient returns a sane default HTTP client.
  52. func DefaultHTTPClient() *http.Client {
  53. return &http.Client{Timeout: 10 * time.Second}
  54. }