github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/docs/v2/framework/api.md (about) 1 --- 2 title: API Gateway 3 keywords: api 4 tags: [api] 5 sidebar: home_sidebar 6 permalink: /api 7 summary: The micro api is an api gateway 8 --- 9 10 Use the API gateway [pattern](http://microservices.io/patterns/apigateway.html) to provide a 11 single public entry point for your services. The micro api serves HTTP and dynamically routes using service discovery. 12 13 <p align="center"> 14 <img src="images/api.png" /> 15 </p> 16 17 ## Overview 18 19 The micro api is a HTTP api. Requests to the API are served over HTTP and routed via service discovery. It builds on 20 [go-micro](https://github.com/micro/go-micro), leveraging it for service discovery, load balancing, encoding and RPC based communication. 21 22 Because the micro api uses go-micro internally, this also makes it pluggable. See [go-plugins](https://github.com/micro/go-plugins) for 23 support for gRPC, kubernetes, etcd, nats, rabbitmq and more. Additionally it makes use of [go-api](https://github.com/micro/go-api) which 24 allows handlers to be configured too. 25 26 ## Install 27 28 ```shell 29 go get -u github.com/tickoalcantara12/micro/v2 30 ``` 31 32 ## Run 33 34 ```shell 35 # Default port 8080 36 micro api 37 ``` 38 39 ## Use ACME 40 41 Serve securely by default using ACME via **Let's Encrypt** 42 43 ``` 44 MICRO_ENABLE_ACME=true micro api 45 ``` 46 47 Optionally specify a host whitelist 48 49 ``` 50 MICRO_ENABLE_ACME=true \ 51 MICRO_ACME_HOSTS=example.com,api.example.com \ 52 micro api 53 ``` 54 55 ## Set TLS Certificate 56 57 The API supports serving securely with TLS certificates 58 59 ```shell 60 MICRO_ENABLE_TLS=true \ 61 MICRO_TLS_CERT_FILE=/path/to/cert \ 62 MICRO_TLS_KEY_FILE=/path/to/key \ 63 micro api 64 ``` 65 66 ## Set Namespace 67 68 The API makes use of namespaces to logically separate backend and public facing services. The namespace and http path 69 are used to resolve service name/method e.g `GET /foo HTTP/1.1` routes to service name `go.micro.api.foo`. 70 71 The default namespace is `go.micro.api` and can be changed like so 72 73 74 ```shell 75 MICRO_NAMESPACE=com.example.api micro api 76 ``` 77 78 To disable the namespace set it to be a blank space. This is a hack we'll look to fix. 79 80 ```shell 81 MICRO_NAMESPACE=' ' 82 ``` 83 84 ## Examples 85 86 Here we have an example of a 3 tier architecture 87 88 - `micro api`: (localhost:8080) - serving as the http entry point 89 - `api service`: (go.micro.api.greeter) - serving a public facing api 90 - `backend service`: (go.micro.srv.greeter) - internally scoped service 91 92 The full example is at [examples/greeter](https://github.com/micro/examples/tree/master/greeter) 93 94 ### Run Example 95 96 ```shell 97 # Download example 98 git clone https://github.com/micro/examples 99 100 # Start the service 101 go run examples/greeter/srv/main.go 102 103 # Start the API 104 go run examples/greeter/api/api.go 105 106 # Start the micro api 107 micro api 108 ``` 109 110 ### Query 111 112 Make a HTTP call via the micro api 113 114 ```shell 115 curl "http://localhost:8080/greeter/say/hello?name=John" 116 ``` 117 118 The HTTP path /greeter/say/hello maps to service go.micro.api.greeter method Say.Hello 119 120 Bypass the api service and call the backend directly via /rpc 121 122 ```shell 123 curl -d 'service=go.micro.srv.greeter' \ 124 -d 'method=Say.Hello' \ 125 -d 'request={"name": "John"}' \ 126 http://localhost:8080/rpc 127 ``` 128 129 Make the same call entirely as JSON 130 131 ```shell 132 curl -H 'Content-Type: application/json' \ 133 -d '{"service": "go.micro.srv.greeter", "method": "Say.Hello", "request": {"name": "John"}}' \ 134 http://localhost:8080/rpc 135 ``` 136 137 ## API 138 139 The micro api provides the following HTTP api 140 141 ``` 142 - /[service]/[method] # HTTP paths are dynamically mapped to services 143 - /rpc # Explicitly call a backend service by name and method 144 ``` 145 146 See below for examples 147 148 ## Handlers 149 150 Handlers are HTTP handlers which manage request routing. 151 152 The default handler uses endpoint metadata from the registry to determine service routes. If a route match is not found it will 153 fallback to the "rpc" handler. You can configure routes on registration using the [go-api](https://github.com/micro/go-api). 154 155 The API has the following configurable request handlers. 156 157 - [`api`](#api-handler) - Handles any HTTP request. Gives full control over the http request/response via RPC. 158 - [`rpc`](#rpc-handler) - Handles json and protobuf POST requests. Forwards as RPC. 159 - [`proxy`](#proxy-handler) - Handles HTTP and forwards as a reverse proxy. 160 - [`event`](#event-handler) - Handles any HTTP request and publishes to a message bus. 161 - [`web`](#web-handler) - HTTP reverse proxy which includes web sockets. 162 163 Optionally bypass the handlers with the [`/rpc`](#rpc-endpoint) endpoint 164 165 ### API Handler 166 167 The API handler serves any HTTP requests and forwards on as an RPC request with a specific format. 168 169 - Content-Type: Any 170 - Body: Any 171 - Forward Format: [api.Request](https://github.com/micro/go-micro/blob/master/api/proto/api.proto#L11)/[api.Response](https://github.com/micro/go-micro/blob/master/api/proto/api.proto#L21) 172 - Path: `/[service]/[method]` 173 - Resolver: Path is used to resolve service and method 174 - Configure: Flag `--handler=api` or env var `MICRO_API_HANDLER=api` 175 176 ### RPC Handler 177 178 The RPC handler serves json or protobuf HTTP POST requests and forwards as an RPC request. 179 180 - Content-Type: `application/json` or `application/protobuf` 181 - Body: JSON or Protobuf 182 - Forward Format: json-rpc or proto-rpc based on content 183 - Path: `/[service]/[method]` 184 - Resolver: Path is used to resolve service and method 185 - Configure: Flag `--handler=rpc` or env var `MICRO_API_HANDLER=rpc` 186 - The default handler when no handler is specified 187 188 ### Proxy Handler 189 190 The proxy handler is a http reserve proxy with built in service discovery. 191 192 - Content-Type: Any 193 - Body: Any 194 - Forward Format: HTTP Reverse proxy 195 - Path: `/[service]` 196 - Resolver: Path is used to resolve service name 197 - Configure: Flag `--handler=proxy` or env var `MICRO_API_HANDLER=proxy` 198 - REST can be implemented behind the API as microservices 199 200 ### Event Handler 201 202 The event handler serves HTTP and forwards the request as a message over a message bus using the go-micro broker. 203 204 - Content-Type: Any 205 - Body: Any 206 - Forward Format: Request is formatted as [go-api/proto.Event](https://github.com/micro/go-api/blob/master/proto/api.proto#L28L39) 207 - Path: `/[topic]/[event]` 208 - Resolver: Path is used to resolve topic and event name 209 - Configure: Flag `--handler=event` or env var `MICRO_API_HANDLER=event` 210 211 ### Web Handler 212 213 The web handler is a http reserve proxy with built in service discovery and web socket support. 214 215 - Content-Type: Any 216 - Body: Any 217 - Forward Format: HTTP Reverse proxy including web sockets 218 - Path: `/[service]` 219 - Resolver: Path is used to resolve service name 220 - Configure: Flag `--handler=web` or env var `MICRO_API_HANDLER=web` 221 222 ### RPC endpoint 223 224 The /rpc endpoint let's you bypass the main handler to speak to any service directly 225 226 - Request Params 227 * `service` - sets the service name 228 * `method` - sets the service method 229 * `request` - the request body 230 * `address` - optionally specify host address to target 231 232 Example call: 233 234 ``` 235 curl -d 'service=go.micro.srv.greeter' \ 236 -d 'method=Say.Hello' \ 237 -d 'request={"name": "Bob"}' \ 238 http://localhost:8080/rpc 239 ``` 240 241 Find working examples in [github.com/micro/examples/api](https://github.com/micro/examples/tree/master/api) 242 243 244 ## Resolver 245 246 Micro dynamically routes to services using a namespace value and the HTTP path. 247 248 The default namespace is `go.micro.api`. Set namespace via `--namespace` or `MICRO_NAMESPACE=`. 249 250 The resolvers used are explained below. 251 252 ### RPC Resolver 253 254 RPC services have a name (go.micro.api.greeter) and a method (Greeter.Hello). 255 256 URLs are resolved as follows: 257 258 Path | Service | Method 259 ---- | ---- | ---- 260 /foo/bar | go.micro.api.foo | Foo.Bar 261 /foo/bar/baz | go.micro.api.foo | Bar.Baz 262 /foo/bar/baz/cat | go.micro.api.foo.bar | Baz.Cat 263 264 Versioned API URLs can easily be mapped to service names: 265 266 Path | Service | Method 267 ---- | ---- | ---- 268 /foo/bar | go.micro.api.foo | Foo.Bar 269 /v1/foo/bar | go.micro.api.v1.foo | Foo.Bar 270 /v1/foo/bar/baz | go.micro.api.v1.foo | Bar.Baz 271 /v2/foo/bar | go.micro.api.v2.foo | Foo.Bar 272 /v2/foo/bar/baz | go.micro.api.v2.foo | Bar.Baz 273 274 ### Proxy Resolver 275 276 With the proxy handler we only need to deal with resolving the service name. So the resolution differs slightly to the RPC resolver. 277 278 URLS are resolved as follows: 279 280 Path | Service | Service Path 281 --- | --- | --- 282 /foo | go.micro.api.foo | /foo 283 /foo/bar | go.micro.api.foo | /foo/bar 284 /greeter | go.micro.api.greeter | /greeter 285 /greeter/:name | go.micro.api.greeter | /greeter/:name 286 287 288