The Food API Gateway was created

We will start this article by writing the Food API Gateway

CD to FoodGuides directory. Create an API folder

mkdir -p foodmanage/api && cd foodmanage/api
Copy the code

Create the food. API file

goctl api -o food.api
Copy the code

Defining API services

info( title: // FoodApi desc: Type SearchRequest struct {key string 'json:"key"'} type SearchResponse struct {FoodReply} type AddFoodRequest struct { FoodId string `json:"foodId"` } type DeleteFoodRequest struct { FoodId string `json:"foodId"` } type FoodReply { Id string `json:"id"` Name string `json:"name"` Protein string `json:"protein"` Fat string `json:"fat"`  Carbohydrate string `json:"carbohydrate"` Calorie string `json:"calorie"` Minerals string `json:"minerals"` Calcium string `json:"calcium"` Phosphorus string `json:"phosphorus"` Iron string `json:"iron"` Purine string `json:"purine"` } Type FoodResponse struct {} service food-api {@handler Search // food/ Search (SearchRequest) returns(SearchResponse) } @server( jwt: Auth) service food-api {@handler AddFood // AddFood/AddFood (AddFoodRequest) returns(FoodResponse) @handler Post /food/ DeleteFood (DeleteFoodRequest) returns(FoodResponse) @handler FoodList // My food post /food/foodlist() returns(FoodResponse) }Copy the code

We defined four food-apis: Search AddFood DeleteFood FoodList

The Search AddFood DeleteFood FoodList request needs to be authenticated

Generate the Food-API service

goctl api go -api food.api -dir .
Copy the code

Check out the API directory

➜ API git tree. (master) ✗ ├ ─ ─ etc │ └ ─ ─ the food - API. Yaml ├ ─ ─ the food. The API ├ ─ ─ food. Go └ ─ ─ internal ├ ─ ─ the config │ └ ─ ─ config. Go ├ ─ ─ handler │ ├ ─ ─ addfoodhandler. Go │ ├ ─ ─ deletefoodhandler. Go │ ├ ─ ─ foodlisthandler. Go │ ├ ─ ─ routes. Go │ └ ─ ─ Searchhandler. Go ├ ─ ─ logic │ ├ ─ ─ addfoodlogic. Go │ ├ ─ ─ deletefoodlogic. Go │ ├ ─ ─ foodlistlogic. Go │ └ ─ ─ searchlogic. Go ├── ─ class ├── go org.go org.go, 15 files ➜ API git:(master) qualifyCopy the code

API GatewayincreaseJwtauthentication

Edit the food-api.yaml file in API /etc and add the following

Auth:
  AccessSecret: ad879037-d3fd-tghj-112d-6bfc35d54b7d
  AccessExpire: 86400
Copy the code

Start the service

Start the service. Note that before starting the service, you need to make sure that the ningxi-compose used in the previous article is up and running.

go run food.go -f etc/food-api.yaml
Copy the code

So the Food API Gateway is basically complete.

Go-zero Tutorial – User Management rpc-UserInfo – Food Rpc-Search