Skip to content

Instantly share code, notes, and snippets.

@digitallysavvy
Last active August 10, 2020 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitallysavvy/f5466530c73de45bf49a2b5ba535364c to your computer and use it in GitHub Desktop.
Save digitallysavvy/f5466530c73de45bf49a2b5ba535364c to your computer and use it in GitHub Desktop.
A function that takes the context, extracts and returns the parameters.
func parseRtcParams(c *gin.Context) (channelName, tokentype, uidStr string, role rtctokenbuilder.Role, expireTimestamp uint32, err error) {
// get param values
channelName = c.Param("channelName")
roleStr := c.Param("role")
tokentype = c.Param("tokentype")
uidStr = c.Param("uid")
expireTime := c.DefaultQuery("expiry", "3600")
if roleStr == "publisher" {
role = rtctokenbuilder.RolePublisher
} else {
role = rtctokenbuilder.RoleSubscriber
}
expireTime64, parseErr := strconv.ParseUint(expireTime, 10, 64)
if parseErr != nil {
// if string conversion fails return an error
err = fmt.Errorf("failed to parse expireTime: %s, causing error: %s", expireTime, parseErr)
}
// set timestamps
expireTimeInSeconds := uint32(expireTime64)
currentTimestamp := uint32(time.Now().UTC().Unix())
expireTimestamp = currentTimestamp + expireTimeInSeconds
return channelName, tokentype, uidStr, role, expireTimestamp, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment