service

package
v1.4.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 11, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Service

type Service struct {
	// Server is the HTTP server for the application.
	Server *http.Server

	// Sigint is a channel to handle OS signals, such as Ctrl+C.
	Sigint chan os.Signal
	// contains filtered or unexported fields
}

Service represents the main application service.

func NewService

func NewService() *Service

NewService returns a Service pointer with all configurations set

func (*Service) GenChatToken added in v1.4.3

func (s *Service) 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:

  1. Sets a default expiration time of 3600 seconds (1 hour) if not provided in the request.
  2. Determines whether to generate a chat app token or a chat user token based on the "UID" field in the request.
  3. 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 := service.GenChatToken(tokenReq)

// Generate a chat user token
tokenReq := TokenRequest{
    TokenType:  "chat",
    Uid:        "user123",
    ExpirationSeconds: 3600,
}
token, err := service.GenChatToken(tokenReq)

func (*Service) GenRtcToken added in v1.4.3

func (s *Service) 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:

  1. Validates the required fields in the TokenRequest (channel and UID).
  2. Sets a default expiration time of 3600 seconds (1 hour) if not provided in the request.
  3. Determines the user's role (publisher or subscriber) based on the "Role" field in the request.
  4. 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",
    Role:       "publisher",
    ExpirationSeconds: 3600,
}
token, err := service.GenRtcToken(tokenReq)

func (*Service) GenRtmToken added in v1.4.3

func (s *Service) 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:

  1. Validates the required field in the TokenRequest (UID).
  2. Sets a default expiration time of 3600 seconds (1 hour) if not provided in the request.
  3. 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 := service.GenRtmToken(tokenReq)

func (*Service) GetToken added in v1.4.3

func (s *Service) GetToken(w http.ResponseWriter, r *http.Request)

getToken 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:

  • w: http.ResponseWriter - The HTTP response writer to send the response to the client.
  • r: *http.Request - The HTTP request received from the client.

Behavior:

  1. Retrieves the tokenType from the query parameters. Error if invalid entry or not provided.
  2. Uses a switch statement to handle different tokenType cases: - "rtm": Calls the RtmToken method to generate the RTM token and sends it as a JSON response. - "chat": Calls the ChatToken method to generate the chat token and sends it as a JSON response. - Default: Calls the RtcToken method to generate the RTC token and sends it as a JSON response.

Notes:

  • The actual token generation methods (RtmToken, ChatToken, and RtcToken) are part of the Service struct.
  • The generated token is sent as a JSON response with appropriate HTTP status codes.

Example usage:

router.GET("/getToken", service.GetToken)

func (*Service) Start

func (s *Service) Start()

Start runs the service by listening to the specified port

func (*Service) Stop

func (s *Service) Stop()

Stop service safely, closing additional connections if needed.

type TokenRequest added in v1.4.1

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL