Host your service
Now that we've defined some classes in Object orientation and a Chain in Chain: persistent mutable state, it's time to put our service online! You can do this using the same object-oriented API that you used to build the service. For security reasons, TinyChain requires that you load your Cluster configuration from a local file on the host machine; see the next section, Install TinyChain for instructions.
1
TC_PATH = "/path/to/tinychain/binary" # <-- edit this
2
3
# optionally, edit these also
4
CONFIG_PATH = "~/config/my_service.json"
5
WORKSPACE = "/tmp/tc/workspace"
6
DATA_DIR = "/tmp/tc/data"
7
8
# define the service
9
class AreaService(tc.Cluster):
10
__uri__ = tc.URI(LINK)
11
12
def _configure(self):
13
# make sure to include your app's classes here so your clients can find them!
14
self.Distance = Distance
15
self.Feet = Feet
16
self.Meters = Meters
17
18
@tc.post_method
19
def area(self, txn, length: Distance, width: Distance) -> tc.Number:
20
txn.length_m = length.to_meters()
21
txn.width_m = width.to_meters()
22
return txn.length_m * txn.width_m
23
24
if __name__ == "__main__":
25
# write the definition to disk
26
tc.write_cluster(CONFIG_PATH)
27
28
# start a new TinyChain host to serve AreaService
29
host = tc.host.Local(TC_PATH, WORKSPACE, DATA_DIR, [CONFIG_PATH], force_create=True)
30
31
# verify that it works as expected
32
service = tc.use(AreaService)
33
params = {"length": service.Meters(5), "width": service.Meters(2)}
34
assert self.host.post("/app/area/area", params) == 10
Copied!
You can see more in-depth examples in the tests directory.
Copy link