Skip to content

Instantly share code, notes, and snippets.

View digitallysavvy's full-sized avatar
:octocat:

Hermes digitallysavvy

:octocat:
View GitHub Profile
@digitallysavvy
digitallysavvy / main.go
Last active August 31, 2020 16:06
example of main.go that retrieves the Agora credentials from the local environment variables.
package main
import (
"log"
"os"
"github.com/gin-gonic/gin"
)
var appID, appCertificate string
@digitallysavvy
digitallysavvy / getBothTokens+snippet.go
Created August 10, 2020 17:51
A function that takes the context, extracts the parameters, uses them to generate two tokens, and uses those tokens in the context's JSON response.
func getBothTokens(c *gin.Context) {
log.Printf("dual token\n")
// get rtc param values
channelName, tokentype, uidStr, role, expireTimestamp, rtcParamErr := parseRtcParams(c)
if rtcParamErr != nil {
c.Error(rtcParamErr)
c.AbortWithStatusJSON(400, gin.H{
"message": "Error Generating RTC token: " + rtcParamErr.Error(),
"status": 400,
@digitallysavvy
digitallysavvy / parseRtmParams+snippet.go
Created August 10, 2020 17:30
A function that takes the context, extracts and returns the parameters.
func parseRtmParams(c *gin.Context) (uidStr string, expireTimestamp uint32, err error) {
// get param values
uidStr = c.Param("uid")
expireTime := c.DefaultQuery("expiry", "3600")
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)
}
@digitallysavvy
digitallysavvy / getRtmToken+snippet.go
Last active August 10, 2020 17:36
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 getRtmToken(c *gin.Context) {
log.Printf("rtm token\n")
// get param values
uidStr, expireTimestamp, err := parseRtmParams(c)
if err != nil {
c.Error(err)
c.AbortWithStatusJSON(400, gin.H{
"message": "Error Generating RTC token: " + err.Error(),
"status": 400,
@digitallysavvy
digitallysavvy / generateRtcToken+snippet.go
Last active August 10, 2020 16:36
a function that generates an RTC token from the given inputs.
func generateRtcToken(channelName, uidStr, tokentype string, role rtctokenbuilder.Role, expireTimestamp uint32) (rtcToken string, err error) {
if tokentype == "userAccount" {
log.Printf("Building Token with userAccount: %s\n", uidStr)
rtcToken, err = rtctokenbuilder.BuildTokenWithUserAccount(appID, appCertificate, channelName, uidStr, role, expireTimestamp)
return rtcToken, err
} else if tokentype == "uid" {
uid64, parseErr := strconv.ParseUint(uidStr, 10, 64)
// check if conversion fails
@digitallysavvy
digitallysavvy / parseRtcParams+snippet.go
Last active August 10, 2020 16:29
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
@digitallysavvy
digitallysavvy / getRtcToken+snippet.go
Last active August 10, 2020 16:34
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,
@digitallysavvy
digitallysavvy / main.go
Last active August 13, 2020 19:22
A template shell for an Agora token server using Golang
package main
import (
"log"
"os"
"github.com/AgoraIO-Community/go-tokenbuilder/rtctokenbuilder"
"github.com/gin-gonic/gin"
)
@digitallysavvy
digitallysavvy / main.go
Last active August 7, 2020 20:42
a very basic web service using GoLang and the Gin framework
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
api := gin.Default()
@digitallysavvy
digitallysavvy / getQueryParams.js
Created July 16, 2020 18:51
a snippet that gets the query params for an Agora token server.
// get uid
let uid = req.query.uid;
if(!uid || uid == '') {
uid = 0;
}
// get role
let role = RtcRole.SUBSCRIBER;
if (req.query.role == 'publisher') {
role = RtcRole.PUBLISHER;
}