go-micro.dev/v5@v5.12.0/cmd/micro/README.md (about)

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