Skip to main content

How To Code Go Part 5: HTTP And GRPC Format

Introduction

This document provides a design guide for HTTP and gRPC API. By following these conventions, we can make our API definitions more readable, and easier to maintain and detect errors.

that suggested for dev

RuleDescription
Path Naming ConventionUse underscore_separated_names for path names.
Field Naming ConventionUniformly use underscore_separated_names or MixCaps for field names.
HTTP Status CodesFollow these definitions for HTTP response status codes:
200The request has been processed, but the success or failure is indicated by the response body.
400Bad request parameter (e.g., missing parameter or parameter type mismatch).
401Unauthorized (e.g., no permission, invalid cookie).
422Unprocessable entity (Request is syntactically correct, but semantically invalid).
429Too many requests (rate limit exceeded).
500Server exception (e.g., DB error, internal server error).
503The server is temporarily unable to handle the request.
504Gateway timeout.
Standard JSON Response StructureUse the following structure for all responses:
Response Body Examplejson { "code": 0, "msg": "success", "data": { /* business data */ } }
Logical Error ResponseUse code != 0 and HTTP status code 200 or 422 for logical errors.
Logical Error Examplejson { "code": 10000, "msg": "exceed user’s backpack size limit", "data": {} }
API Consistency with YAPIThe API must match the definitions in YAPI, including path, field name, type, optionality, and comments.
Field Type - Integer vs. FloatDo not use float type fields, use integer instead.
RESTful API MethodsUse the correct HTTP methods for RESTful APIs: GET, POST, PUT, DELETE, etc.
Empty List vs. Null ListIf the response contains a list, return an empty list [] instead of a null list.
Error Code RangeTo make error detection easier, define fixed ranges of error codes for each business module:
10000~20000Access errors
20001~30000System errors
30001~40000Chance module errors
40001~50000LDC module errors
Restful API for Simple LogicUse RESTful style APIs for resource-based services with simple logic.
RESTful ExampleGET /redeem/api/v1/stores/:store_id (query a store)
. POST /redeem/api/v1/stores/:store_id (create a store)
Custom API for Complex LogicFor complex scenarios, use JSON API with custom names, matching RPC request protocol for future ease of access.
JSON API ExampleGET /pet/api/v1/game/round?round_num={round_num} (query a game round detail)
. POST /pet/api/v1/game/begin_round (begin a game round)
Integer PrecisionTo avoid precision issues in JavaScript, integer fields with more than 52 bits should use string type (user_id, shop_id, timestamp will not exceed 52 bits).

gRPC API

RuleDescription
File and Package NamingMUST: File names and package names should be named lower_snake_case. Example: package gameplatform_chance;
Message NamingMUST: Use CamelCase for message names. Example: message SongServerRequest
Field NamingMUST: Use underscore_separated_name for field names. `
gRPC Service and Method NamingMUST: Use CamelCase for both the service name and any gRPC method names. Example:
. protobuf <br />. service FooService { <br />. rpc GetSomething(GetSomethingRequest) returns (GetSomethingResponse); <br />. rpc ListSomething(ListSomethingRequest) returns (ListSomethingResponse); <br />. } <br />.
Field Types for Unique IDsMUST: Use uint64/int64 for fields representing unique identifiers (e.g., user_id, shop_id, order_id, item_id).
Enumeration DefinitionsMUST: If there is an enumeration, it should be defined in the .proto file. Example:
. protobuf <br />. enum MsgSource { <br />. IOS = 3000; // apple msg source <br />. ANDROID = 4000; // android msg source <br />. } <br />. message SendChatRequest { <br />. optional MsgSource msg_source = 1; // refer to MsgSource <br />. ... <br />. } <br />.
Field NumberingMUST: DO NOT change the field numbers for any existing fields.
Type ConversionMUST: DO NOT do incompatible type conversions (e.g., int <-> string <-> float).
Response Message with ListRECOMMENDED: If a gRPC proto response message contains a field with type list (repeated), return nil or null list instead of an empty list. Note: This is different from HTTP response behavior.
Repeated Field NamingRECOMMENDED: Use pluralized names for repeated fields. Example: accounts for a list of accounts, keys for a list of keys, values for a list of values.
Enum NamingRECOMMENDED: Use CamelCase for enum type names and CAPITALS_WITH_UNDERSCORES for enum values. Example:
. protobuf <br />. enum FooBar { <br />. FOO_BAR_UNSPECIFIED = 0; <br />. FOO_BAR_FIRST_VALUE = 1; <br />. FOO_BAR_SECOND_VALUE = 2; <br />. } <br />.
Keyword UsageRECOMMENDED: DO NOT use require keyword, as it may cause compatibility issues.
Field DeletionRECOMMENDED: DO NOT delete any existing fields; instead, use // deprecated since v1.x.y comments.
existing fields; instead, use // deprecated since v1.x.y comments.