config-core.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // Copyright (C) 2017-2019 Marcus Rohrmoser, http://purl.mro.name/ShaarliGo
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. //
  17. package main
  18. import (
  19. "crypto/rand"
  20. "encoding/base64"
  21. "fmt"
  22. "io"
  23. "io/ioutil"
  24. "os"
  25. "path/filepath"
  26. "gopkg.in/yaml.v2"
  27. )
  28. var configFileName string
  29. func init() {
  30. configFileName = filepath.Join(dirApp, "config.yaml")
  31. }
  32. type RegexpReplaceAllString struct {
  33. Regexp string `yaml:"regexp"`
  34. ReplaceAllString string `yaml:"replace_all_string"`
  35. }
  36. type Config struct {
  37. Title string `yaml:"title"`
  38. Uid string `yaml:"uid"`
  39. PwdBcrypt string `yaml:"pwd_bcrypt"`
  40. CookieStoreSecret string `yaml:"cookie_secret"`
  41. TimeZone string `yaml:"timezone"`
  42. Skin string `yaml:"skin"` // maybe should not be configurable, but rather be a filesystem softlink
  43. LinksPerPage int `yaml:"links_per_page"` // https://github.com/sebsauvage/Shaarli/blob/master/index.php#L18
  44. BanAfter int `yaml:"ban_after"` // https://github.com/sebsauvage/Shaarli/blob/master/index.php#L20
  45. BanSeconds int `yaml:"ban_seconds"` // https://github.com/sebsauvage/Shaarli/blob/master/index.php#L21
  46. UrlCleaner []RegexpReplaceAllString `yaml:"url_cleaner"`
  47. // Redirector string `yaml:"redirector"` // actually a prefix to href - Hardcoded in xslt
  48. }
  49. func LoadConfig() (Config, error) {
  50. if read, err := ioutil.ReadFile(configFileName); err != nil {
  51. return Config{}, err
  52. } else {
  53. ret := Config{}
  54. if err = yaml.Unmarshal(read, &ret); err != nil {
  55. return Config{}, err
  56. }
  57. if ret.CookieStoreSecret == "" {
  58. buf := make([]byte, 32)
  59. if _, err := io.ReadFull(rand.Reader, buf); err != nil {
  60. return Config{}, err
  61. }
  62. ret.CookieStoreSecret = base64.StdEncoding.EncodeToString(buf)
  63. }
  64. ret.LinksPerPage = max(1, ret.LinksPerPage)
  65. ret.BanAfter = max(1, ret.BanAfter)
  66. ret.BanSeconds = max(1, ret.BanSeconds)
  67. return ret, err
  68. }
  69. }
  70. func (cfg Config) Save() error {
  71. if out, err := yaml.Marshal(cfg); err == nil {
  72. tmpFileName := fmt.Sprintf("%s~%d", configFileName, os.Getpid())
  73. if err = os.MkdirAll(filepath.Dir(configFileName), 0770); err == nil {
  74. if err = ioutil.WriteFile(tmpFileName, out, 0660); err == nil {
  75. err = os.Rename(tmpFileName, configFileName)
  76. }
  77. }
  78. return err
  79. } else {
  80. return err
  81. }
  82. }
  83. func (cfg Config) IsConfigured() bool {
  84. return cfg.PwdBcrypt != ""
  85. }