func parseListServicesHandlerParameters(c *routing.Context) (string, int, error) {
	page := 1
	var err error
	searchQuery := ""
	if c.QueryArgs().Has("page") {
		page, err = strconv.Atoi(string(c.QueryArgs().Peek("page")))
		if err != nil {
			return "", page, errors.New("Invalid page")
		}
	}
	if c.QueryArgs().Has("q") {
		searchQuery = string(c.QueryArgs().Peek("q"))
	}
	return searchQuery, page, nil
}
parseListServicesHandlerParameters parses page and search query parameters for GET /services
package controllers
import (
	"encoding/json"
	"errors"
	"strconv"
	"github.com/Glintt/gAPI/api/authentication"
	"github.com/Glintt/gAPI/api/config"
	"github.com/Glintt/gAPI/api/http"
	"github.com/Glintt/gAPI/api/servicediscovery"
	"github.com/Glintt/gAPI/api/servicediscovery/constants"
	"github.com/Glintt/gAPI/api/servicediscovery/service"
	sdUtils "github.com/Glintt/gAPI/api/servicediscovery/utils"
	"github.com/Glintt/gAPI/api/utils"
	routing "github.com/qiangxue/fasthttp-routing"
	"gopkg.in/mgo.v2/bson"
)
func ServiceDiscoveryServiceName() string {
	return constants.SERVICE_NAME
}
func ServiceDiscovery(c *routing.Context) *servicediscovery.ServiceDiscovery {
	user := authentication.GetAuthenticatedUser(c)
	return servicediscovery.GetServiceDiscoveryObject(user)
}
func InternalServiceDiscovery() *servicediscovery.ServiceDiscovery {
	return servicediscovery.GetInternalServiceDiscoveryObject()
}
func ServiceNotFound(c *routing.Context) error {
	return http.NotFound(c, "Service not found.", ServiceDiscoveryServiceName())
}
func NormalizeServices(c *routing.Context) error {
	err := ServiceDiscovery(c).NormalizeServices()
	if err != nil {
		return http.Error(c, "Normalization failed", 400, ServiceDiscoveryServiceName())
	}
	return http.OkFormated(c, "Normalization done.", ServiceDiscoveryServiceName())
}
func UpdateHandler(c *routing.Context) error {
	serviceDiscovery := ServiceDiscovery(c)
	serviceID := c.Param("service_id")
	s, status, err := servicediscovery.ValidateServiceBodyAndServiceExists(c, serviceID)
	if err != nil {
		return http.Error(c, err.Error(), status, ServiceDiscoveryServiceName())
	}
	s, err = serviceDiscovery.UpdateService(s)
	if err != nil {
		return http.Error(c, err.Error(), 400, ServiceDiscoveryServiceName())
	}
	return http.Created(c, "Service updated successfuly", ServiceDiscoveryServiceName())
}
func AutoRegisterHandler(c *routing.Context) error {
	serviceDiscovery := InternalServiceDiscovery()
	var s map[string]string
	json.Unmarshal(c.Request.Body(), &s)
	if s["MatchingUri"] == "" || s["ToUri"] == "" || s["Name"] == "" || s["Port"] == "" {
		return http.Error(c, "Missing body parameters.", 400, ServiceDiscoveryServiceName())
	}
	host := c.RemoteIP().String() + ":" + s["Port"]
	serv := service.Service{
		Hosts:            []string{host},
		MatchingURI:      s["MatchingUri"],
		ToURI:            s["ToUri"],
		Name:             s["Name"],
		HealthcheckUrl:   "http://" + host + s["HealthcheckUrl"],
		APIDocumentation: "http://" + host + s["APIDocumentation"],
		IsReachable:      true,
	}
	serv.MatchingURIRegex = sdUtils.GetMatchingURIRegex(serv.MatchingURI)
	serviceFound, err := servicediscovery.ValidateServiceExists(serv)
	if err != nil {
		serviceFound, err = serviceDiscovery.CreateService(serv)
	} else {
		serviceFound.Hosts = append(serviceFound.Hosts, host)
		serviceFound, err = serviceDiscovery.UpdateService(serviceFound)
	}
	if err != nil {
		return http.Error(c, err.Error(), 400, ServiceDiscoveryServiceName())
	}
	serv, _ = serviceDiscovery.FindService(serviceFound)
	s2, _ := json.Marshal(serv)
	http.Response(c, string(s2), 201, "AUTO_REGISTER", "application/json")
	return nil
}
func AutoDeRegisterHandler(c *routing.Context) error {
	serviceDiscovery := InternalServiceDiscovery()
	var s map[string]string
	json.Unmarshal(c.Request.Body(), &s)
	if s["MatchingUri"] == "" || s["Port"] == "" {
		return http.Error(c, "Missing body parameters.", 400, ServiceDiscoveryServiceName())
	}
	host := c.RemoteIP().String() + ":" + s["Port"]
	serv := service.Service{
		MatchingURI: s["MatchingUri"],
	}
	serv.MatchingURIRegex = sdUtils.GetMatchingURIRegex(serv.MatchingURI)
	serviceFound, err := servicediscovery.ValidateServiceExists(serv)
	var status int
	if err != nil {
		return ServiceNotFound(c)
	} else {
		serviceFound.Hosts = utils.RemoveStringFromArray(serviceFound.Hosts, host)
		serviceFound, err = serviceDiscovery.UpdateService(serviceFound)
	}
	serv, _ = serviceDiscovery.FindService(serviceFound)
	s2, _ := json.Marshal(serv)
	http.Response(c, string(s2), status, "AUTO_DEREGISTER", "application/json")
	return nil
}
func RegisterHandler(c *routing.Context) error {
	serviceDiscovery := ServiceDiscovery(c)
	serv, err := servicediscovery.ValidateServiceBody(c)
	if err != nil {
		return http.Error(c, err.Error(), 400, ServiceDiscoveryServiceName())
	}
	_, err = servicediscovery.ValidateServiceExists(serv)
	if err == nil {
		return http.Error(c, `Service already exists.`, 400, ServiceDiscoveryServiceName())
	}
	serv.MatchingURIRegex = sdUtils.GetMatchingURIRegex(serv.MatchingURI)
	serv, err = serviceDiscovery.CreateService(serv)
	if err != nil {
		return http.Error(c, err.Error(), 400, ServiceDiscoveryServiceName())
	}
	return http.Created(c, "Service registered successfuly", ServiceDiscoveryServiceName())
}