Skip to content

Instantly share code, notes, and snippets.

@digitallysavvy
Last active August 10, 2020 16:34
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/b02dfe239ef55423270c5ec9f705e196 to your computer and use it in GitHub Desktop.
Save digitallysavvy/b02dfe239ef55423270c5ec9f705e196 to your computer and use it in GitHub Desktop.
A function that takes the context, extracts the parameters, uses them to generate a token, and uses that token in building the JSON response.
func getRtcToken(c *gin.Context) {
log.Printf("rtc token\n")
// get param values
channelName, tokentype, uidStr, role, expireTimestamp, err := parseRtcParams(c)
if err != nil {
c.Error(err)
c.AbortWithStatusJSON(400, gin.H{
"message": "Error Generating RTC token: " + err.Error(),
"status": 400,
})
return
}
rtcToken, tokenErr := generateRtcToken(channelName, uidStr, tokentype, role, expireTimestamp)
if tokenErr != nil {
log.Println(tokenErr) // token failed to generate
c.Error(tokenErr)
errMsg := "Error Generating RTC token - " + tokenErr.Error()
c.AbortWithStatusJSON(400, gin.H{
"status": 400,
"error": errMsg,
})
} else {
log.Println("RTC Token generated")
c.JSON(200, gin.H{
"rtcToken": rtcToken,
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment