Snippets of Go code etc
Last active
October 31, 2023 21:40
-
-
Save benc-uk/1f79c53e1497e828e9df57abb4d33d5f to your computer and use it in GitHub Desktop.
Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "net/http" | |
func main() { | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, `<script src="https://unpkg.com/htmx.org/dist/htmx.min.js"></script> | |
<button hx-put="/count" hx-target="#counter" hx-include="body" hx-swap="outerHTML">Increment</button> | |
<input id="counter" name="counter" value="0" readonly/>`)}) | |
http.HandleFunc("/count", func(w http.ResponseWriter, r *http.Request) { | |
var c int | |
fmt.Sscanf(r.FormValue("counter"), "%d", &c) | |
fmt.Fprintf(w, `<input id="counter" name="counter" value="%d" readonly>`, c+1) | |
}) | |
http.ListenAndServe(":8080", nil) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"github.com/dgrijalva/jwt-go" | |
"github.com/lestrrat-go/jwx/jwk" | |
) | |
// Put your token here | |
const token = `BBBBBBBBBLLLLLLLLLLAAAAAAAAAAAGGGGGGGHHHHHHHHHHHHH` | |
// Your endpoint might be different | |
const jwksURL = `https://login.microsoftonline.com/common/discovery/v2.0/keys` | |
// | |
// Get key for given token (from it's kid header) | |
// | |
func getKeyFromJWKS(token *jwt.Token) (interface{}, error) { | |
// TODO: cache response so we don't have to make a request every time we want to verify a JWT | |
set, err := jwk.FetchHTTP(jwksURL) | |
if err != nil { | |
return nil, err | |
} | |
keyID, ok := token.Header["kid"].(string) | |
if !ok { | |
return nil, errors.New("expecting JWT header to have string kid") | |
} | |
if key := set.LookupKeyID(keyID); len(key) == 1 { | |
return key[0].Materialize() | |
} | |
return nil, fmt.Errorf("unable to find key %q", keyID) | |
} | |
// | |
// Parse token using our getKey function | |
// | |
func main() { | |
token, err := jwt.Parse(token, getKeyFromJWKS) | |
if err != nil { | |
panic(err) | |
} | |
// Now validate the claims as you wish, e.g. azp, scp, iss | |
claims := token.Claims.(jwt.MapClaims) | |
for key, value := range claims { | |
fmt.Printf("%s\t%v\n", key, value) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"log" | |
dapr "github.com/dapr/go-sdk/client" | |
) | |
// Component binding YAML example | |
/* | |
apiVersion: dapr.io/v1alpha1 | |
kind: Component | |
metadata: | |
name: sendgrid-test | |
spec: | |
type: bindings.twilio.sendgrid | |
version: v1 | |
metadata: | |
- name: emailFrom | |
value: "[email protected]" | |
- name: apiKey | |
value: "!!CHANGE_ME!!" | |
*/ | |
func main() { | |
client, err := dapr.NewClient() | |
if err != nil { | |
panic(err) | |
} | |
emailMetadata := map[string]string{ | |
"emailTo": "[email protected]", | |
"subject": "Dapr Test", | |
} | |
request := &dapr.InvokeBindingRequest{ | |
Name: "sendgrid-test", | |
Operation: "create", | |
Metadata: emailMetadata, | |
Data: []byte("<h1>Dapr is pretty cool, right?</h1>"), | |
} | |
err = client.InvokeOutputBinding(context.Background(), request) | |
if err != nil { | |
log.Println(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment