...

Source file src/discovery/api/configuration.go

Documentation: discovery/api

     1  /*
     2   * P2PFaaS - A framework for FaaS Load Balancing
     3   * Copyright (c) 2019. Gabriele Proietti Mattia <pm.gabriele@outlook.com>
     4   *
     5   * This program is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
    17   */
    18  
    19  package api
    20  
    21  import (
    22  	config2 "discovery/config"
    23  	"discovery/db"
    24  	"discovery/errors"
    25  	"discovery/log"
    26  	"encoding/json"
    27  	"io"
    28  	"io/ioutil"
    29  	"net/http"
    30  )
    31  
    32  func ConfigurationGet(w http.ResponseWriter, r *http.Request) {
    33  	// if we do not have the machine ip we report 404
    34  	if config2.GetMachineIp() == "" {
    35  		errors.ReplyWithError(w, errors.ConfigurationNotReady)
    36  		return
    37  	}
    38  
    39  	config, err := json.Marshal(config2.GetConfigurationDynamicCopy())
    40  	if err != nil {
    41  		log.Log.Errorf("Cannot encode configuration to json")
    42  		errors.ReplyWithError(w, errors.GenericError)
    43  		return
    44  	}
    45  
    46  	w.Header().Set("Content-Type", "application/json")
    47  	io.WriteString(w, string(config))
    48  }
    49  
    50  func ConfigurationSet(w http.ResponseWriter, r *http.Request) {
    51  	currentConfiguration := config2.GetConfigurationDynamicCopy()
    52  	reqBody, _ := ioutil.ReadAll(r.Body)
    53  
    54  	var newConfiguration *config2.ConfigurationDynamic
    55  	var err error
    56  
    57  	// do the merge with the default configuration or existing
    58  	err = json.Unmarshal(reqBody, &currentConfiguration)
    59  	newConfiguration = currentConfiguration
    60  	if err != nil {
    61  		log.Log.Errorf("Cannot encode passed configuration")
    62  		errors.ReplyWithError(w, errors.GenericError)
    63  		return
    64  	}
    65  
    66  	config2.SetMachineId(newConfiguration.MachineId)
    67  	config2.SetMachineIp(newConfiguration.MachineIp)
    68  	config2.SetMachineFogNetId(newConfiguration.MachineGroupName)
    69  	config2.SetInitServers(newConfiguration.InitServers)
    70  	db.AddInitServers(newConfiguration.InitServers)
    71  
    72  	// save configuration to file
    73  	err = config2.SaveConfigurationDynamicToConfigFile()
    74  	if err != nil {
    75  		log.Log.Warningf("Cannot save configuration to file %s", config2.GetConfigurationDynamicFilePath())
    76  		w.WriteHeader(500)
    77  		return
    78  	}
    79  
    80  	log.Log.Infof("Configuration updated")
    81  
    82  	w.WriteHeader(200)
    83  }
    84  

View as plain text