Documentation
¶
Index ¶
- type TokenRequest
- type TokenService
- func (s *TokenService) GenChatToken(tokenRequest TokenRequest) (string, error)
- func (s *TokenService) GenRtcToken(tokenRequest TokenRequest) (string, error)
- func (s *TokenService) GenRtmToken(tokenRequest TokenRequest) (string, error)
- func (s *TokenService) GetToken(c *gin.Context)
- func (s *TokenService) HandleGetToken(tokenReq TokenRequest, w http.ResponseWriter)
- func (s *TokenService) RegisterRoutes(r *gin.Engine)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type TokenRequest ¶
type TokenRequest struct {
TokenType string `json:"tokenType"` // The token type: "rtc", "rtm", or "chat"
Channel string `json:"channel,omitempty"` // The channel name (used for RTC and RTM tokens)
RtcRole string `json:"role,omitempty"` // The role of the user for RTC tokens (publisher or subscriber)
Uid string `json:"uid,omitempty"` // The user ID or account (used for RTC, RTM, and some chat tokens)
ExpirationSeconds int `json:"expire,omitempty"` // The token expiration time in seconds (used for all token types)
}
TokenRequest is a struct representing the JSON payload structure for token generation requests. It contains fields necessary for generating different types of tokens (RTC, RTM, or chat) based on the "TokenType". The "Channel", "RtcRole", "Uid", and "ExpirationSeconds" fields are used for specific token types.
TokenType options: "rtc" for RTC token, "rtm" for RTM token, and "chat" for chat token.
type TokenService ¶
type TokenService struct {
Server *http.Server // The HTTP server for the application
Sigint chan os.Signal // Channel to handle OS signals, such as Ctrl+C
// contains filtered or unexported fields
}
TokenService represents the main application token service. It holds the necessary configurations and dependencies for managing tokens.
func NewTokenService ¶
func NewTokenService(appIDEnv string, appCertEnv string) *TokenService
NewTokenService initializes and returns a TokenService pointer with all configurations set. It loads environment variables, validates their presence, and initializes the TokenService struct.
Parameters:
- appIDEnv: string - The environment variable for the Agora app ID.
- appCertEnv: string - The environment variable for the Agora app certificate.
- corsAllowOrigin: string - The allowed origin for CORS.
Returns:
- *TokenService: The initialized TokenService struct.
Behavior:
- Initializes and returns a TokenService struct with the provided configurations.
Notes:
- The necessary environment variables should be set before initializing the TokenService.
func (*TokenService) GenChatToken ¶
func (s *TokenService) GenChatToken(tokenRequest TokenRequest) (string, error)
GenChatToken generates a chat token based on the provided TokenRequest and returns it.
Parameters:
- tokenRequest: TokenRequest - The TokenRequest struct containing the required information for chat token generation.
Returns:
- string: The generated chat token.
- error: An error if there are any issues during token generation or validation.
Behavior:
- Sets a default expiration time of 3600 seconds (1 hour) if not provided in the request.
- Determines whether to generate a chat app token or a chat user token based on the "UID" field in the request.
- Generates the chat token using the chatTokenBuilder package.
Notes:
- The chatTokenBuilder package is used for generating chat tokens.
- If the "UID" field is empty, a chat app token is generated; otherwise, a chat user token is generated.
Example usage:
// Generate a chat app token
tokenReq := TokenRequest{
TokenType: "chat",
ExpirationSeconds: 3600,
}
token, err := TokenService.GenChatToken(tokenReq)
// Generate a chat user token
tokenReq := TokenRequest{
TokenType: "chat",
Uid: "user123",
ExpirationSeconds: 3600,
}
token, err := TokenService.GenChatToken(tokenReq)
func (*TokenService) GenRtcToken ¶
func (s *TokenService) GenRtcToken(tokenRequest TokenRequest) (string, error)
GenRtcToken generates an RTC token based on the provided TokenRequest and returns it.
Parameters:
- tokenRequest: TokenRequest - The TokenRequest struct containing the required information for RTC token generation.
Returns:
- string: The generated RTC token.
- error: An error if there are any issues during token generation or validation.
Behavior:
- Validates the required fields in the TokenRequest (channel and UID).
- Sets a default expiration time of 3600 seconds (1 hour) if not provided in the request.
- Determines the user's role (publisher or subscriber) based on the "Role" field in the request.
- Generates the RTC token using the rtctokenbuilder2 package.
Notes:
- The rtctokenbuilder2 package is used for generating RTC tokens.
- The "Role" field can be "publisher" or "subscriber"; other values are considered invalid.
Example usage:
tokenReq := TokenRequest{
TokenType: "rtc",
Channel: "my_channel",
Uid: "user123",
RtcRole: "publisher",
ExpirationSeconds: 3600,
}
token, err := TokenService.GenRtcToken(tokenReq)
func (*TokenService) GenRtmToken ¶
func (s *TokenService) GenRtmToken(tokenRequest TokenRequest) (string, error)
GenRtmToken generates an RTM (Real-Time Messaging) token based on the provided TokenRequest and returns it.
Parameters:
- tokenRequest: TokenRequest - The TokenRequest struct containing the required information for RTM token generation.
Returns:
- string: The generated RTM token.
- error: An error if there are any issues during token generation or validation.
Behavior:
- Validates the required field in the TokenRequest (UID).
- Sets a default expiration time of 3600 seconds (1 hour) if not provided in the request.
- Generates the RTM token using the rtmtokenbuilder2 package.
Notes:
- The rtmtokenbuilder2 package is used for generating RTM tokens.
- The "UID" field in TokenRequest is mandatory for RTM token generation.
Example usage:
tokenReq := TokenRequest{
TokenType: "rtm",
Uid: "user123",
ExpirationSeconds: 3600,
}
token, err := TokenService.GenRtmToken(tokenReq)
func (*TokenService) GetToken ¶
func (s *TokenService) GetToken(c *gin.Context)
GetToken handles the HTTP request to generate a token based on the provided TokenRequest. It forwards the HTTP response writer and request from the provided *gin.Context to the HandleGetToken method for token generation and response sending.
Parameters:
- c: *gin.Context - The Gin context representing the HTTP request and response.
Behavior:
- Parses the request body into a TokenRequest struct.
- Forwards the HTTP response writer and request to the HandleGetToken method for processing.
Notes:
- This function acts as an intermediary to invoke the HandleGetToken method.
- It handles parsing the request body and passing it to the token generation logic.
Example usage:
router.POST("/getNew", TokenService.GetToken)
func (*TokenService) HandleGetToken ¶
func (s *TokenService) HandleGetToken(tokenReq TokenRequest, w http.ResponseWriter)
HandleGetToken handles the HTTP request to generate a token based on the provided tokenType. It checks the tokenType from the query parameters and calls the appropriate token generation method. The generated token is sent as a JSON response to the client.
Parameters:
- tokenReq: TokenRequest - The request object containing token type and other necessary fields.
- w: http.ResponseWriter - The HTTP response writer to send the response to the client.
Behavior:
- Retrieves the tokenType from the request. Error if invalid entry or not provided.
- Uses a switch statement to handle different tokenType cases: - "rtc": Calls the GenRtcToken method to generate the RTC token and sends it as a JSON response. - "rtm": Calls the GenRtmToken method to generate the RTM token and sends it as a JSON response. - "chat": Calls the GenChatToken method to generate the chat token and sends it as a JSON response. - Default: Returns an error response indicating an unsupported token type.
Notes:
- The actual token generation methods (GenRtcToken, GenRtmToken, and GenChatToken) are part of the TokenService struct.
- The generated token is sent as a JSON response with appropriate HTTP status codes.
Example usage:
router.POST("/getNew", TokenService.HandleGetToken)
func (*TokenService) RegisterRoutes ¶
func (s *TokenService) RegisterRoutes(r *gin.Engine)
RegisterRoutes registers the routes for the TokenService. It sets up the API endpoints and applies necessary middleware for request handling.
Parameters:
- r: *gin.Engine - The Gin engine instance to register the routes with.
Behavior:
- Creates an API group for token routes.
- Applies middleware for NoCache and CORS.
- Registers routes for getting a new token.
Notes:
- This function organizes the API routes and ensures that requests are handled with appropriate middleware.