github.com/readium/readium-lcp-server@v0.0.0-20240509124024-799e77a0bbd6/config/config.go (about) 1 // Copyright 2020 Readium Foundation. All rights reserved. 2 // Use of this source code is governed by a BSD-style license 3 // that can be found in the LICENSE file exposed on Github (readium) in the project repository. 4 5 package config 6 7 import ( 8 "os" 9 "path/filepath" 10 "strconv" 11 "strings" 12 13 "gopkg.in/yaml.v2" 14 ) 15 16 type Configuration struct { 17 Certificate Certificate `yaml:"certificate"` 18 Storage Storage `yaml:"storage"` 19 License License `yaml:"license"` 20 LcpServer ServerInfo `yaml:"lcp"` 21 LsdServer LsdServerInfo `yaml:"lsd"` 22 FrontendServer FrontendServerInfo `yaml:"frontend"` 23 LsdNotifyAuth Auth `yaml:"lsd_notify_auth"` 24 LcpUpdateAuth Auth `yaml:"lcp_update_auth"` 25 CMSAccessAuth Auth `yaml:"cms_access_auth"` 26 LicenseStatus LicenseStatus `yaml:"license_status"` 27 Localization Localization `yaml:"localization"` 28 Logging Logging `yaml:"logging"` 29 TestMode bool `yaml:"test_mode"` 30 GoofyMode bool `yaml:"goofy_mode"` 31 Profile string `yaml:"profile,omitempty"` 32 33 // DISABLED, see https://github.com/readium/readium-lcp-server/issues/109 34 //AES256_CBC_OR_GCM string `yaml:"aes256_cbc_or_gcm,omitempty"` 35 } 36 37 type ServerInfo struct { 38 Host string `yaml:"host,omitempty"` 39 Port int `yaml:"port,omitempty"` 40 AuthFile string `yaml:"auth_file"` 41 ReadOnly bool `yaml:"readonly,omitempty"` 42 PublicBaseUrl string `yaml:"public_base_url,omitempty"` 43 Database string `yaml:"database,omitempty"` 44 CertDate string `yaml:"cert_date,omitempty"` 45 Resources string `yaml:"resources,omitempty"` 46 } 47 48 type LsdServerInfo struct { 49 ServerInfo `yaml:",inline"` 50 LicenseLinkUrl string `yaml:"license_link_url,omitempty"` 51 UserDataUrl string `yaml:"user_data_url,omitempty"` 52 } 53 54 type FrontendServerInfo struct { 55 ServerInfo `yaml:",inline"` 56 Directory string `yaml:"directory,omitempty"` 57 ProviderUri string `yaml:"provider_uri"` 58 RightPrint int32 `yaml:"right_print"` 59 RightCopy int32 `yaml:"right_copy"` 60 MasterRepository string `yaml:"master_repository"` 61 EncryptedRepository string `yaml:"encrypted_repository"` 62 } 63 64 type Auth struct { 65 Username string `yaml:"username"` 66 Password string `yaml:"password"` 67 } 68 69 type Certificate struct { 70 Cert string `yaml:"cert"` 71 PrivateKey string `yaml:"private_key"` 72 } 73 74 type FileSystem struct { 75 Directory string `yaml:"directory"` 76 URL string `yaml:"url,omitempty"` 77 } 78 79 type Storage struct { 80 FileSystem FileSystem `yaml:"filesystem"` 81 AccessId string `yaml:"access_id"` 82 DisableSSL bool `yaml:"disable_ssl"` 83 PathStyle bool `yaml:"path_style"` 84 Mode string `yaml:"mode"` 85 Secret string `yaml:"secret"` 86 Endpoint string `yaml:"endpoint"` 87 Bucket string `yaml:"bucket"` 88 Region string `yaml:"region"` 89 Token string `yaml:"token"` 90 } 91 92 type License struct { 93 Links map[string]string `yaml:"links"` 94 } 95 96 type LicenseStatus struct { 97 Renew bool `yaml:"renew"` 98 Register bool `yaml:"register"` 99 Return bool `yaml:"return"` 100 RentingDays int `yaml:"renting_days"` 101 RenewDays int `yaml:"renew_days"` 102 RenewPageUrl string `yaml:"renew_page_url,omitempty"` 103 RenewCustomUrl string `yaml:"renew_custom_url,omitempty"` 104 } 105 106 type Localization struct { 107 Languages []string `yaml:"languages"` 108 Folder string `yaml:"folder"` 109 DefaultLanguage string `yaml:"default_language"` 110 } 111 112 type Logging struct { 113 Directory string `yaml:"directory"` 114 SlackToken string `yaml:"slack_token"` 115 SlackChannelID string `yaml:"slack_channel"` 116 } 117 118 // Config is a global variable which contains the server configuration 119 var Config Configuration 120 121 // ReadConfig parses the configuration file 122 func ReadConfig(configFileName string) { 123 filename, _ := filepath.Abs(configFileName) 124 yamlFile, err := os.ReadFile(filename) 125 126 if err != nil { 127 panic("Can't read config file: " + configFileName) 128 } 129 130 err = yaml.Unmarshal(yamlFile, &Config) 131 132 if err != nil { 133 panic("Can't unmarshal config. " + configFileName + " -> " + err.Error()) 134 } 135 } 136 137 // GetDatabase gets the driver name and connection string corresponding to the input 138 func GetDatabase(uri string) (string, string) { 139 // use a sqlite memory db by default 140 if uri == "" { 141 uri = "sqlite3://:memory:" 142 } 143 144 parts := strings.Split(uri, "://") 145 if parts[0] == "postgres" { 146 return parts[0], uri 147 } 148 149 return parts[0], parts[1] 150 } 151 152 // SetPublicUrls sets the default publics urls of the 3 servers from the config 153 func SetPublicUrls() error { 154 var lcpPublicBaseUrl, lsdPublicBaseUrl, frontendPublicBaseUrl, lcpHost, lsdHost, frontendHost string 155 var lcpPort, lsdPort, frontendPort int 156 var err error 157 158 if lcpHost = Config.LcpServer.Host; lcpHost == "" { 159 lcpHost, err = os.Hostname() 160 if err != nil { 161 return err 162 } 163 } 164 165 if lsdHost = Config.LsdServer.Host; lsdHost == "" { 166 lsdHost, err = os.Hostname() 167 if err != nil { 168 return err 169 } 170 } 171 172 if frontendHost = Config.FrontendServer.Host; frontendHost == "" { 173 frontendHost, err = os.Hostname() 174 if err != nil { 175 return err 176 } 177 } 178 179 if lcpPort = Config.LcpServer.Port; lcpPort == 0 { 180 lcpPort = 8989 181 } 182 if lsdPort = Config.LsdServer.Port; lsdPort == 0 { 183 lsdPort = 8990 184 } 185 if frontendPort = Config.FrontendServer.Port; frontendPort == 0 { 186 frontendPort = 80 187 } 188 189 if lcpPublicBaseUrl = Config.LcpServer.PublicBaseUrl; lcpPublicBaseUrl == "" { 190 lcpPublicBaseUrl = "http://" + lcpHost + ":" + strconv.Itoa(lcpPort) 191 Config.LcpServer.PublicBaseUrl = lcpPublicBaseUrl 192 } 193 if lsdPublicBaseUrl = Config.LsdServer.PublicBaseUrl; lsdPublicBaseUrl == "" { 194 lsdPublicBaseUrl = "http://" + lsdHost + ":" + strconv.Itoa(lsdPort) 195 Config.LsdServer.PublicBaseUrl = lsdPublicBaseUrl 196 } 197 if frontendPublicBaseUrl = Config.FrontendServer.PublicBaseUrl; frontendPublicBaseUrl == "" { 198 frontendPublicBaseUrl = "http://" + frontendHost + ":" + strconv.Itoa(frontendPort) 199 Config.FrontendServer.PublicBaseUrl = frontendPublicBaseUrl 200 } 201 202 return err 203 }