Skip to content

Web API

GOST可以通过开启Web API服务使用RESTful API和GOST进程进行交互。

gost -L http://:8080 -api :18080

开启认证并设置选项

gost -L http://:8080 -api "user:pass@:18080?pathPrefix=/api&accesslog=true"
services:
- name: service-0
  addr: ":8080"
  handler:
    type: http
  listener:
    type: tcp
api:
  addr: :18080
  pathPrefix: /api
  accesslog: true
  auth:
    username: user
    password: pass
  auther: auther-0

也可以只开启API服务,后续通过API来动态配置服务和其他组件。

gost -api :18080
api:
  addr: :18080
  pathPrefix: /api
  accesslog: true
  auth:
    username: user
    password: pass
  auther: auther-0

路径前缀

通过pathPrefix参数可以设置URL路径前缀。

例如默认路径为http://localhost:18080/config,当设置pathPrefix/api后变为http://localhost:18080/api/config。

访问日志

通过accesslog参数开启接口访问日志,默认不输出访问日志。

身份认证

身份认证采用HTTP Basic Auth方式。

配置文件中通过authauther选项可以设置身份认证信息,如果设置了auther选项,auth选项则会被忽略。

gost -api user:pass@:18080
api:
  addr: :18080
  auth:
    username: user
    password: pass
  auther: auther-0

在线测试

你可以使用线上环境进行测试,或在下面的swaggerUI中直接尝试。接口说明请参考在线API文档

GOST程序已经内置了swagger API文档,如果本地开启了Web API服务,也可以通过https://gost.run/swagger-ui/?url=http://localhost:18080/docs/swagger.yaml来尝试配置本地服务(这里假设本地Web API服务运行在18080端口)。

Scheme切换

SwaggerUI中默认的scheme为HTTPS,如果要测试本地服务则需要手动切换到HTTP。

Comments