func (p PostgresDb) GetServices() ([]core.Service, error) { // read from services table rows, err := p.pg.Query("SELECT id, host, interface, port, type, scheduler, persistence, netmask FROM services") if err != nil { return nil, fmt.Errorf("Failed to select from services table - %v", err) } defer rows.Close() services := make([]core.Service, 0, 0) // get data for rows.Next() { svc := core.Service{} err = rows.Scan(&svc.Id, &svc.Host, &svc.Interface, &svc.Port, &svc.Type, &svc.Scheduler, &svc.Persistence, &svc.Netmask) if err != nil { return nil, fmt.Errorf("Failed to save results into service - %v", err) } // get service's servers servers, err := p.GetServers(svc.Id) if err != nil { return nil, err } svc.Servers = servers services = append(services, svc) } // check for errors if err = rows.Err(); err != nil { return services, fmt.Errorf("Error with results - %v", err) } return services, nil }
// takes a lvs.Service and converts it to a core.Service func lToSvc(service lvs.Service) core.Service { srvs := []core.Server{} for i, srv := range service.Servers { srvs = append(srvs, lToSrv(srv)) srvs[i].GenId() } svc := core.Service{Host: service.Host, Port: service.Port, Type: service.Type, Scheduler: service.Scheduler, Persistence: service.Persistence, Netmask: service.Netmask, Servers: srvs} svc.GenId() return svc }
func parseReqService(req *http.Request) (*core.Service, error) { b, err := ioutil.ReadAll(req.Body) if err != nil { config.Log.Error(err.Error()) return nil, BodyReadFail } var svc core.Service if err := json.Unmarshal(b, &svc); err != nil { return nil, BadJson } if svc.Interface != "" { err = svc.GenHost() if err != nil { return nil, err } } svc.GenId() if svc.Id == "--0" { return nil, NoServiceError } err = checkPort(svc) if err != nil { return nil, err } for i := range svc.Servers { svc.Servers[i].GenId() // localhost doesn't work properly, use service.Host if svc.Servers[i].Host == "127.0.0.1" { svc.Servers[i].GenHost(svc.Id) } } config.Log.Trace("SERVICE: %+v", svc) return &svc, nil }
func marshalSvc(service []byte) (*core.Service, error) { var svc core.Service if err := json.Unmarshal(service, &svc); err != nil { return nil, BadJson } svc.GenId() if svc.Id == "--0" { return nil, NoServiceError } for i := range svc.Servers { svc.Servers[i].GenId() if svc.Servers[i].Id == "-0" { return nil, NoServerError } } return &svc, nil }