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