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.

97 satır
1.9KB

  1. package main
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. )
  9. type configStaticHtml struct {
  10. Dir string
  11. StaticUrl string
  12. StripPrefix string
  13. Sync string
  14. }
  15. type configuration struct {
  16. Host string
  17. Port string
  18. DSN string
  19. TlsCert string
  20. TlsKey string
  21. RSyncKey string
  22. Static []configStaticHtml
  23. Debug bool
  24. TempDir string
  25. Session struct { //TODO: figure what is this intended for
  26. Guest bool
  27. Year int //how many years
  28. Month int //how many years
  29. Day int //how many years
  30. }
  31. }
  32. var configFile = "config.json"
  33. var config = configuration{}
  34. func (m *configuration) readConfig() (e error) {
  35. log.Printf("read Path config from %s", configFile)
  36. body, e := ioutil.ReadFile(configFile)
  37. if e != nil {
  38. log.Fatal("Cannot read config from " + configFile)
  39. return
  40. }
  41. e = json.Unmarshal(body, m)
  42. // Check upload dir and defaults
  43. if !config.checkUploadDir() {
  44. log.Fatal("bad config file", configFile)
  45. return
  46. }
  47. if config.Debug {
  48. log.Println(config)
  49. }
  50. return
  51. }
  52. func (m *configuration) checkUploadDir() (valid bool) {
  53. valid = true
  54. for idx, node := range m.Static {
  55. if node.StaticUrl == "/" {
  56. if !fileExists(node.Dir) {
  57. valid = false
  58. log.Fatal(" html / not exist ", node)
  59. } else {
  60. // convert to absolute path : fileDir
  61. p, e := filepath.Abs(node.Dir)
  62. if e != nil {
  63. valid = false
  64. log.Fatal("bad html (webroot) dir ", node, e)
  65. }
  66. m.Static[idx].Dir = p + string(os.PathSeparator) //change it to absolute dir
  67. }
  68. }
  69. }
  70. // convert rsync key file to absolute dir
  71. p, e := filepath.Abs(config.RSyncKey)
  72. if e != nil {
  73. valid = false
  74. log.Fatal("bad html (webroot) dir ", config.RSyncKey, e)
  75. }
  76. m.RSyncKey = p //change it to absolute dir
  77. return
  78. }
  79. func fileExists(path string) bool {
  80. if _, err := os.Stat(path); os.IsNotExist(err) {
  81. // path/to/whatever does not exist
  82. return false
  83. }
  84. return true
  85. }