Storyscript 微服务轻度学习

/ 0评 / 0

主要是涉猎一些课外知识.

说来这个东西,语法官方花了一页全部说完.

###

Meet Storyscript

###



# Pull data from a microservice

output = service action key:value

output = team/service action key:value

# Discover and create services in the Storyscript Hub



# Call a function

output = function_name(key:value)

# A Storyscript function

# or another programming language



# Call type methods

output = variable.mutation(key:value)



# Event streaming microservice

when service action event key:value as output

    ... # run this block for every event



# Types

string = "Hello"

integer = 1

number = 1.3

bool = true

list = ["a", "b", "c"]

map = {"apple": "red", "banana": "yellow"}

regexp = /^foobar/

empty = null

time = 1d35m



# Destructuring

{ apple, banana } = map

# apple = "red", banana = "yellow"



# Conditions

if one > 1

    # ...

else if one == 1

    # ...

else

    # ...



# Loops

foreach list as item

    # ...

while true

    # ...



# Functions

function name input:int returns int

    # ...

    return input

name(input:1)

# >>> 1

由于官方给的上手Hello World实在太简单了,基本就是输出,实际上就http服务而言,都有很多功能,比如下面代码获取某个get参数.

http server as server

    when server listen method: "get" path: "/" as r

        r write content: r.query_params["src"]

运行结果大致如下.

做一个简单计算器,涉及了类型转换,创建了一个函数.

function add a:int b:int returns int

    return a + b



http server as server

    when server listen method: "get" path: "/" as r

        result = add(a:r.query_params["srcA"] as int b:r.query_params["srcB"] as int)

        r write content: result as string

运行结果:

当然我们可以依赖别人的库,也能自己按OMG写,我们可以依赖数据库运行某些东西.

比如依赖这个编码的库:https://hub.storyscript.io/service/base64/guide#encode

如果你要封装自己的功能,只要符合OMG格式,也是可以来套用的,主要是一个脚本.

http server as server

    when server listen method: "get" path: "/" as r

        query_content = r.query_params["src"]

        encoded = base64 encode content: query_content as string

        decoded = base64 decode content: encoded

        r write content: decoded

要自己做服务,请参考:

https://microservice.guide/introduction/overview/

https://github.com/omg-services/uuid

当然你也可以当serverless用,只不过这是两码事.