config.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2019 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero 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 Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package config
  17. import (
  18. "io/ioutil"
  19. "gopkg.in/yaml.v2"
  20. "maunium.net/go/mautrix-appservice"
  21. )
  22. type Config struct {
  23. Homeserver struct {
  24. Address string `yaml:"address"`
  25. Domain string `yaml:"domain"`
  26. } `yaml:"homeserver"`
  27. AppService struct {
  28. Address string `yaml:"address"`
  29. Hostname string `yaml:"hostname"`
  30. Port uint16 `yaml:"port"`
  31. Database struct {
  32. Type string `yaml:"type"`
  33. URI string `yaml:"uri"`
  34. MaxOpenConns int `yaml:"max_open_conns"`
  35. MaxIdleConns int `yaml:"max_idle_conns"`
  36. } `yaml:"database"`
  37. StateStore string `yaml:"state_store_path"`
  38. ID string `yaml:"id"`
  39. Bot struct {
  40. Username string `yaml:"username"`
  41. Displayname string `yaml:"displayname"`
  42. Avatar string `yaml:"avatar"`
  43. } `yaml:"bot"`
  44. ASToken string `yaml:"as_token"`
  45. HSToken string `yaml:"hs_token"`
  46. } `yaml:"appservice"`
  47. Bridge BridgeConfig `yaml:"bridge"`
  48. Logging appservice.LogConfig `yaml:"logging"`
  49. }
  50. func (config *Config) setDefaults() {
  51. config.AppService.Database.MaxOpenConns = 20
  52. config.AppService.Database.MaxIdleConns = 2
  53. }
  54. func Load(path string) (*Config, error) {
  55. data, err := ioutil.ReadFile(path)
  56. if err != nil {
  57. return nil, err
  58. }
  59. var config = &Config{}
  60. config.setDefaults()
  61. err = yaml.Unmarshal(data, config)
  62. return config, err
  63. }
  64. func (config *Config) Save(path string) error {
  65. data, err := yaml.Marshal(config)
  66. if err != nil {
  67. return err
  68. }
  69. return ioutil.WriteFile(path, data, 0600)
  70. }
  71. func (config *Config) MakeAppService() (*appservice.AppService, error) {
  72. as := appservice.Create()
  73. as.HomeserverDomain = config.Homeserver.Domain
  74. as.HomeserverURL = config.Homeserver.Address
  75. as.Host.Hostname = config.AppService.Hostname
  76. as.Host.Port = config.AppService.Port
  77. var err error
  78. as.Registration, err = config.GetRegistration()
  79. return as, err
  80. }