87336d80c4d03bda219893678ac964a5b1fac02b — Louis Solofrizzo 7 months ago e89a8bc
api: Add better configuration YAML logic

Signed-off-by: Louis Solofrizzo <lsolofrizzo@online.net>
7 files changed, 42 insertions(+), 31 deletions(-)

M api/config.go
M api/database.go
M api/instance.go
M api/invite.go
M api/mail.go
M api/main.go
M api/utils.go
M api/config.go => api/config.go +30 -19
@@ 7,20 7,31 @@ )
  
  type CiscoConfig struct {
- 	Driver         string `yaml:"driver"`
- 	Database       string `yaml:"database"`
- 	Address        string `yaml:"address"`
- 	SMTPHost       string `yaml:"smtp_host"`
- 	SMTPUser       string `yaml:"smtp_user"`
- 	SMTPPassword   string `yaml:"smtp_password"`
- 	Host           string `yaml:"host"`
- 	Salt           string `yaml:"salt"`
- 	LXDCert        string `yaml:"lxd_cert"`
- 	LXDKey         string `yaml:"lxd_key"`
- 	LXDCA          string `yaml:"lxd_ca"`
- 	LXDUrl         string `yaml:"lxd_url"`
- 	LXDStoragePool string `yaml:"lxd_storage_pool"`
- 	HTTPHost       string
+ 	Database struct {
+ 		Driver string `yaml:"driver"`
+ 		Url    string `yaml:"url"`
+ 	} `yaml:"database"`
+ 
+ 	Http struct {
+ 		Address  string `yaml:"address"`
+ 		Host     string `yaml:"host"`
+ 		Salt     string `yaml:"salt"`
+ 		HTTPHost string
+ 	} `yaml:"http"`
+ 
+ 	Mail struct {
+ 		Host     string `yaml:"smtp_host"`
+ 		User     string `yaml:"smtp_user"`
+ 		Password string `yaml:"smtp_password"`
+ 	} `yaml:"mail"`
+ 
+ 	LXD struct {
+ 		Cert        string `yaml:"lxd_cert"`
+ 		Key         string `yaml:"lxd_key"`
+ 		CA          string `yaml:"lxd_ca"`
+ 		Url         string `yaml:"lxd_url"`
+ 		StoragePool string `yaml:"lxd_storage_pool"`
+ 	} `yaml:"lxd"`
  }
  
  type CiscoContext struct {


@@ 41,24 52,24 @@ panic("Cannot read configuration file: " + err.Error())
  	}
  
- 	Config.HTTPHost = Config.Host
+ 	Config.Http.HTTPHost = Config.Http.Host
  
- 	crt, err := ioutil.ReadFile(Config.LXDCert)
+ 	crt, err := ioutil.ReadFile(Config.LXD.Cert)
  	if err != nil {
  		panic("Cannot read client certificate: " + err.Error())
  	}
  
- 	key, err := ioutil.ReadFile(Config.LXDKey)
+ 	key, err := ioutil.ReadFile(Config.LXD.Key)
  	if err != nil {
  		panic("Cannot read client key: " + err.Error())
  	}
  
- 	ca, err := ioutil.ReadFile(Config.LXDCA)
+ 	ca, err := ioutil.ReadFile(Config.LXD.CA)
  	if err != nil {
  		panic("Cannot read cluster certificate: " + err.Error())
  	}
  
- 	Context.LXD, err = lxd.ConnectLXD(Config.LXDUrl, &lxd.ConnectionArgs{
+ 	Context.LXD, err = lxd.ConnectLXD(Config.LXD.Url, &lxd.ConnectionArgs{
  		TLSServerCert: string(ca[:]),
  		TLSClientCert: string(crt[:]),
  		TLSClientKey:  string(key[:]),

M api/database.go => api/database.go +1 -1
@@ 82,7 82,7 @@ func InitDB() {
  	var err error
  
- 	DB, err = gorm.Open(Config.Driver, Config.Database)
+ 	DB, err = gorm.Open(Config.Database.Driver, Config.Database.Url)
  	if err != nil {
  		panic("Failed to connect to database:" + err.Error())
  	}

M api/instance.go => api/instance.go +1 -1
@@ 155,7 155,7 @@ Devices: map[string]map[string]string{
  				"root": map[string]string{
  					"path": "/",
- 					"pool": Config.LXDStoragePool,
+ 					"pool": Config.LXD.StoragePool,
  					"size": strconv.Itoa(int(instance.Size)) + "GB",
  					"type": "disk",
  				},

M api/invite.go => api/invite.go +4 -4
@@ 21,7 21,7 @@ Take Care,
  
  [1] %s/invite/claim/%s
- [2] https://louifox.s3.fr-par.scw.cloud/cisco/csc/latest/csc
+ [2] https://cdn.ne02ptzero/cisco/
  `
  
  func invite_new(ctx iris.Context) {


@@ 51,9 51,9 @@ sum := sha256.Sum256([]byte(invite.Email + time.Now().String()))
  	sumHex := hex.EncodeToString(sum[:])
  
- 	err := SendMail(invite.Email, "Your invitation to "+Config.Host,
- 		fmt.Sprintf(mailTemplateFmt, Config.Host, sumHex, sumHex,
- 			Config.Host, Config.Host, sumHex))
+ 	err := SendMail(invite.Email, "Your invitation to "+Config.Http.Host,
+ 		fmt.Sprintf(mailTemplateFmt, Config.Http.Host, sumHex, sumHex,
+ 			Config.Http.Host, Config.Http.Host, sumHex))
  
  	if err != nil {
  		APIError(ctx, 500, "Cannot send email: "+err.Error())

M api/mail.go => api/mail.go +3 -3
@@ 8,16 8,16 @@ )
  
  func SendMail(to string, subject string, msg string) error {
- 	auth := sasl.NewPlainClient("", Config.SMTPUser, Config.SMTPPassword)
+ 	auth := sasl.NewPlainClient("", Config.SMTP.User, Config.SMTP.Password)
  
  	body := strings.NewReader(
  		"To: " + to + "\r\n" +
- 			"From: " + Config.SMTPUser + "\r\n" +
+ 			"From: " + Config.SMTP.User + "\r\n" +
  			"Subject: " + subject + "\r\n" +
  			"\r\n" +
  			msg + "\r\n" +
  			"---\r\n" +
  			"I'm just a dumb script, please don't respond!\r\n")
  
- 	return smtp.SendMail(Config.SMTPHost, auth, Config.SMTPUser, []string{to}, body)
+ 	return smtp.SendMail(Config.SMTP.Host, auth, Config.SMTP.User, []string{to}, body)
  }

M api/main.go => api/main.go +2 -2
@@ 26,7 26,7 @@ return false
  	}
  
- 	toSign = ctx.Method() + Config.HTTPHost + ctx.RequestPath(false) + ctx.GetHeader("Date") + key
+ 	toSign = ctx.Method() + Config.Http.HTTPHost + ctx.RequestPath(false) + ctx.GetHeader("Date") + key
  
  	rawData := RequestBody(ctx)
  	if rawData != nil && len(rawData) != 0 {


@@ 160,5 160,5 @@ InitInstanceRoutes(app)
  	InitImageRoutes(app)
  
- 	app.Run(iris.Addr(Config.Address))
+ 	app.Run(iris.Addr(Config.Http.Address))
  }

M api/utils.go => api/utils.go +1 -1
@@ 31,7 31,7 @@ }
  
  func HashNSalt(password string) string {
- 	before := password + Config.Salt
+ 	before := password + Config.Http.Salt
  	sum := sha256.Sum256([]byte(before))
  	return hex.EncodeToString(sum[:])
  }