Skip to content

Instantly share code, notes, and snippets.

View sle-c's full-sized avatar
🏠
Working from home

Si Le sle-c

🏠
Working from home
View GitHub Profile
@sle-c
sle-c / active_record_json_cheatsheet.rb
Last active March 15, 2024 23:48 — forked from mabenson00/cheatsheet.rb
Rails ActiveRecord JSON cheatsheet
# Basic key operators to query the JSON objects :
# #> : Get the JSON object at that path (if you need to do something fancy)
# -> : Get the JSON object at that path (if you don't)
# ->> : Get the JSON object at that path as text
# {obj, n} : Get the nth item in that object
# https://www.postgresql.org/docs/9.4/functions-json.html#FUNCTIONS-JSONB-OP-TABLE
# Date
# date before today
@sle-c
sle-c / object_pool_part1.go
Last active December 14, 2019 23:28
Defining Factory function and Object interface
type Object interface {
Close()
}
type Factory func() (Object, error)
@sle-c
sle-c / middleware.go
Created May 31, 2019 20:35
JWT auth middleware example
package goliauth
import (
"fmt"
"net/http"
"strings"
)
// AuthenticateJWTMiddleware wraps AuthenticateJWTToken to provide middleware
// this is just an example to show how it can be used as a middleware
@sle-c
sle-c / encode_jwt.go
Created May 31, 2019 20:22
Encode things to JWT Token
package goliauth
import (
"fmt"
jwt "github.com/dgrijalva/jwt-go"
)
// EncodeJWT serialize data into a jwt token using a secret
// This secret must match with the client's secret who's generating the token
@sle-c
sle-c / parse_jwt.go
Last active May 31, 2019 19:34
Parse JWT token
package goliauth
import (
"fmt"
jwt "github.com/dgrijalva/jwt-go"
)
// Claims is an alias for MapClaims
type Claims = jwt.MapClaims
@sle-c
sle-c / get.go
Created April 16, 2019 04:51
A function to get an App object given a public key
package app
import (
"encoding/hex"
"fmt"
"github.com/omnisyle/goliauth"
)
func GetApp(publicKey, dbURL string) *App {
@sle-c
sle-c / create.go
Created April 16, 2019 04:40
Service to create an App object in database
package app
import (
"fmt"
"github.com/omnisyle/goliauth"
)
func CreateApp(name, dbURL string) *App {
publicKey := goliauth.NewRandomKey()
@sle-c
sle-c / handlers.go
Created April 16, 2019 04:24
Handlers for app command to create and get app
func createApp(cmd *cobra.Command, args []string) {
appName := args[0]
keyPair := app.CreateApp(appName, db)
printResult(keyPair)
}
func getApp(cmd *cobra.Command, args []string) {
publicKey := args[0]
keyPair := app.GetApp(publicKey, db)
printResult(keyPair)
@sle-c
sle-c / key.go
Created April 16, 2019 02:55
Command to generate a random 32 bits key and print to the screen
package cmd
import (
"fmt"
"github.com/omnisyle/goliauth"
"github.com/spf13/cobra"
)
func keyCmd() *cobra.Command {
@sle-c
sle-c / root.go
Last active April 15, 2019 04:57
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{