AOSharedServiceLibrary
service.h
1 /*
2 MIT License Block
3 
4 Copyright (c) 2016 Alex Barry
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 */
24 
25 #include <string.h>
26 #include <string>
27 #include <vector>
28 #include "aossl/consul/include/consul_interface.h"
29 
30 #ifndef AOSSL_CONSUL_INCLUDE_SERVICE_H_
31 #define AOSSL_CONSUL_INCLUDE_SERVICE_H_
32 
33 namespace AOSSL {
34 
36 
39 class Service: public ServiceInterface {
40  std::string id;
41  std::string name;
42  std::vector<std::string> tags;
43  std::string address;
44  std::string port;
45  HealthCheck check;
46 
47  public:
49  Service() : id(""), name(""), address(""), port("") {check.url = "";check.ttl = "";}
50 
52  Service(std::string new_id, std::string new_name) : id(new_id), \
53  name(new_name), address(""), port("") {check.url = "";check.ttl = "";}
54 
56  Service(std::string new_id, std::string new_name, std::string new_address, \
57  std::string new_port) : id(new_id), name(new_name), address(new_address), \
58  port(new_port) {check.url = "";check.ttl = "";}
59 
61  Service(std::string new_id, std::string new_name, std::string new_address, \
62  std::string new_port, std::vector<std::string> new_tags) : id(new_id), \
63  name(new_name), address(new_address), port(new_port) \
64  {tags = new_tags;check.url = "";check.ttl = "";}
65 
67 
70  std::string to_json() const;
71 
73  std::string get_id() const {return id;}
74 
76  std::string get_name() const {return name;}
77 
79  std::string get_address() const {return address;}
80 
82  std::string get_port() const {return port;}
83 
85  void set_id(std::string new_id) {id = new_id;}
86 
88  void set_name(std::string new_name) {name = new_name;}
89 
91  void set_address(std::string new_address) {address = new_address;}
92 
94  void set_port(std::string new_port) {port = new_port;}
95 
97  std::vector<std::string> get_tags() {return tags;}
99  void add_tag(std::string new_tag) {tags.push_back(new_tag);}
101  void clear_tags() {tags.clear();}
103  int num_tags() const {return tags.size();}
104 
106  HealthCheck get_check() {return check;}
108  inline void set_check(std::string http_url, int interval_seconds) {
109  check.url = http_url;
110  check.interval = std::to_string(interval_seconds) + "s";
111  check.ttl = std::to_string(interval_seconds + 5) + "s";
112  }
114  inline void set_check(std::string http_url, int interv_seconds, int interv_ttl) {
115  check.url = http_url;
116  check.interval = std::to_string(interv_seconds) + "s";
117  check.ttl = std::to_string(interv_ttl) + "s";
118  }
119 };
120 
121 } // namespace AOSSL
122 
123 #endif // AOSSL_CONSUL_INCLUDE_SERVICE_H_
void clear_tags()
Clear the tags.
Definition: service.h:101
std::string to_json() const
Convert the Service into a JSON Message.
std::string ttl
The TTL for the health check.
Definition: consul_interface.h:48
HealthCheck get_check()
Get the health checks.
Definition: service.h:106
std::string interval
The interval for the health check.
Definition: consul_interface.h:45
A struct to hold health check information which can be added to a service.
Definition: consul_interface.h:40
int num_tags() const
How many tags are there?
Definition: service.h:103
std::string get_id() const
Get the Service ID.
Definition: service.h:73
Service()
Construct a Service.
Definition: service.h:49
std::string url
The HTTP URL to hit with a GET request for the healthcheck.
Definition: consul_interface.h:42
void set_port(std::string new_port)
Set the Service Port.
Definition: service.h:94
A Service class which can be registered with Consul for each app instance.
Definition: service.h:39
A Service class which can be registered with Consul for each app instance.
Definition: consul_interface.h:57
Service(std::string new_id, std::string new_name, std::string new_address, std::string new_port, std::vector< std::string > new_tags)
Construct a Service.
Definition: service.h:61
void set_check(std::string http_url, int interval_seconds)
Add a check.
Definition: service.h:108
Service(std::string new_id, std::string new_name, std::string new_address, std::string new_port)
Construct a Service.
Definition: service.h:56
std::string get_address() const
Get the Service Address.
Definition: service.h:79
void set_id(std::string new_id)
Set the Service ID.
Definition: service.h:85
void set_check(std::string http_url, int interv_seconds, int interv_ttl)
Add a check.
Definition: service.h:114
void set_name(std::string new_name)
Set the Service Name.
Definition: service.h:88
Service(std::string new_id, std::string new_name)
Construct a Service.
Definition: service.h:52
std::vector< std::string > get_tags()
Get the tags.
Definition: service.h:97
void set_address(std::string new_address)
Set the Service Address.
Definition: service.h:91
Definition: cli.h:35
void add_tag(std::string new_tag)
Add a tag.
Definition: service.h:99
std::string get_port() const
Get the Service Port.
Definition: service.h:82
std::string get_name() const
Get the Service Name.
Definition: service.h:76