|
| 1 | +# Micro |
| 2 | + |
| 3 | +Go Micro Command Line |
| 4 | + |
| 5 | +## Install the CLI |
| 6 | + |
| 7 | +Install `micro` via `go install` |
| 8 | + |
| 9 | +``` |
| 10 | +go install go-micro.dev/v5/cmd/micro@latest |
| 11 | +``` |
| 12 | + |
| 13 | +Or via install script |
| 14 | + |
| 15 | + |
| 16 | +## Create a service |
| 17 | + |
| 18 | +Create your service (all setup is now automatic!): |
| 19 | + |
| 20 | +``` |
| 21 | +micro new helloworld |
| 22 | +``` |
| 23 | + |
| 24 | +This will: |
| 25 | +- Create a new service in the `helloworld` directory |
| 26 | +- Automatically run `go mod tidy` and `make proto` for you |
| 27 | +- Show the updated project tree including generated files |
| 28 | +- Warn you if `protoc` is not installed, with install instructions |
| 29 | + |
| 30 | +## Run the service |
| 31 | + |
| 32 | +Run the service |
| 33 | + |
| 34 | +``` |
| 35 | +micro run |
| 36 | +``` |
| 37 | + |
| 38 | +List services to see it's running and registered itself |
| 39 | + |
| 40 | +``` |
| 41 | +micro services |
| 42 | +``` |
| 43 | + |
| 44 | +## Describe the service |
| 45 | + |
| 46 | +Describe the service to see available endpoints |
| 47 | + |
| 48 | +``` |
| 49 | +micro describe helloworld |
| 50 | +``` |
| 51 | + |
| 52 | +Output |
| 53 | + |
| 54 | +``` |
| 55 | +{ |
| 56 | + "name": "helloworld", |
| 57 | + "version": "latest", |
| 58 | + "metadata": null, |
| 59 | + "endpoints": [ |
| 60 | + { |
| 61 | + "request": { |
| 62 | + "name": "Request", |
| 63 | + "type": "Request", |
| 64 | + "values": [ |
| 65 | + { |
| 66 | + "name": "name", |
| 67 | + "type": "string", |
| 68 | + "values": null |
| 69 | + } |
| 70 | + ] |
| 71 | + }, |
| 72 | + "response": { |
| 73 | + "name": "Response", |
| 74 | + "type": "Response", |
| 75 | + "values": [ |
| 76 | + { |
| 77 | + "name": "msg", |
| 78 | + "type": "string", |
| 79 | + "values": null |
| 80 | + } |
| 81 | + ] |
| 82 | + }, |
| 83 | + "metadata": {}, |
| 84 | + "name": "Helloworld.Call" |
| 85 | + }, |
| 86 | + { |
| 87 | + "request": { |
| 88 | + "name": "Context", |
| 89 | + "type": "Context", |
| 90 | + "values": null |
| 91 | + }, |
| 92 | + "response": { |
| 93 | + "name": "Stream", |
| 94 | + "type": "Stream", |
| 95 | + "values": null |
| 96 | + }, |
| 97 | + "metadata": { |
| 98 | + "stream": "true" |
| 99 | + }, |
| 100 | + "name": "Helloworld.Stream" |
| 101 | + } |
| 102 | + ], |
| 103 | + "nodes": [ |
| 104 | + { |
| 105 | + "metadata": { |
| 106 | + "broker": "http", |
| 107 | + "protocol": "mucp", |
| 108 | + "registry": "mdns", |
| 109 | + "server": "mucp", |
| 110 | + "transport": "http" |
| 111 | + }, |
| 112 | + "id": "helloworld-31e55be7-ac83-4810-89c8-a6192fb3ae83", |
| 113 | + "address": "127.0.0.1:39963" |
| 114 | + } |
| 115 | + ] |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +## Call the service |
| 120 | + |
| 121 | +Call via RPC endpoint |
| 122 | + |
| 123 | +``` |
| 124 | +micro call helloworld Helloworld.Call '{"name": "Asim"}' |
| 125 | +``` |
| 126 | + |
| 127 | +## Create a client |
| 128 | + |
| 129 | +Create a client to call the service |
| 130 | + |
| 131 | +```go |
| 132 | +package main |
| 133 | + |
| 134 | +import ( |
| 135 | + "context" |
| 136 | + "fmt" |
| 137 | + |
| 138 | + "go-micro.dev/v5" |
| 139 | +) |
| 140 | + |
| 141 | +type Request struct { |
| 142 | + Name string |
| 143 | +} |
| 144 | + |
| 145 | +type Response struct { |
| 146 | + Message string |
| 147 | +} |
| 148 | + |
| 149 | +func main() { |
| 150 | + client := micro.New("helloworld").Client() |
| 151 | + |
| 152 | + req := client.NewRequest("helloworld", "Helloworld.Call", &Request{Name: "John"}) |
| 153 | + |
| 154 | + var rsp Response |
| 155 | + |
| 156 | + err := client.Call(context.TODO(), req, &rsp) |
| 157 | + if err != nil { |
| 158 | + fmt.Println(err) |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + fmt.Println(rsp.Message) |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | +## Protobuf |
| 167 | + |
| 168 | +Use protobuf for code generation with [protoc-gen-micro](https://github.com/micro/go-micro/tree/master/cmd/protoc-gen-micro) |
| 169 | + |
| 170 | +## Server |
| 171 | + |
| 172 | +The micro server is an api and web dashboard that provide a fixed entrypoint for seeing and querying services. |
| 173 | + |
| 174 | +Run it like so |
| 175 | + |
| 176 | +``` |
| 177 | +micro server |
| 178 | +``` |
| 179 | + |
| 180 | +Then browse to [localhost:8080](http://localhost:8080) |
| 181 | + |
| 182 | +### API Endpoints |
| 183 | + |
| 184 | +The API provides a fixed HTTP entrypoint for calling services |
| 185 | + |
| 186 | +``` |
| 187 | +curl http://localhost:8080/api/helloworld/Helloworld/Call -d '{"name": "John"}' |
| 188 | +``` |
| 189 | +See /api for more details and documentation for each service |
| 190 | + |
| 191 | +### Web Dashboard |
| 192 | + |
| 193 | +The web dashboard provides a modern, secure UI for managing and exploring your Micro services. Major features include: |
| 194 | + |
| 195 | +- **Dynamic Service & Endpoint Forms**: Browse all registered services and endpoints. For each endpoint, a dynamic form is generated for easy testing and exploration. |
| 196 | +- **API Documentation**: The `/api` page lists all available services and endpoints, with request/response schemas and a sidebar for quick navigation. A documentation banner explains authentication requirements. |
| 197 | +- **JWT Authentication**: All login and token management uses a custom JWT utility. Passwords are securely stored with bcrypt. All `/api/x` endpoints and authenticated pages require an `Authorization: Bearer <token>` header (or `micro_token` cookie as fallback). |
| 198 | +- **Token Management**: The `/auth/tokens` page allows you to generate, view (obfuscated), and copy JWT tokens. Tokens are stored and can be revoked. When a user is deleted, all their tokens are revoked immediately. |
| 199 | +- **User Management**: The `/auth/users` page allows you to create, list, and delete users. Passwords are never shown or stored in plaintext. |
| 200 | +- **Token Revocation**: JWT tokens are stored and checked for revocation on every request. Revoked or deleted tokens are immediately invalidated. |
| 201 | +- **Security**: All protected endpoints use consistent authentication logic. Unauthorized or revoked tokens receive a 401 error. All sensitive actions require authentication. |
| 202 | +- **Logs & Status**: View service logs and status (PID, uptime, etc) directly from the dashboard. |
| 203 | + |
| 204 | +To get started, run: |
| 205 | + |
| 206 | +``` |
| 207 | +micro server |
| 208 | +``` |
| 209 | + |
| 210 | +Then browse to [localhost:8080](http://localhost:8080) and log in with the default admin account (`admin`/`micro`). |
| 211 | + |
| 212 | +> **Note:** See the `/api` page for details on API authentication and how to generate tokens for use with the HTTP API |
0 commit comments