Skip to content

Instantly share code, notes, and snippets.

View LLe27's full-sized avatar
🥑

Long Le LLe27

🥑
View GitHub Profile
@LLe27
LLe27 / CustomGoPlugin.go
Created September 25, 2025 13:30
Function to debug custom domains and request values
package main
import (
"encoding/json"
"net/http"
"strings"
"github.com/TykTechnologies/tyk/log"
)
@LLe27
LLe27 / README.md
Last active July 30, 2025 12:38
Using URL Rewrite + Custom Go Plugin for Dynamic Routing

NOTE: It's important to note that this is NOT PRODUCTION READY code and is only used as an example build from. It is EXPECTED that you test and prove this out on your own based on your use-case and understand what the Tyk Gateway has to offer / expected to do.

This is an example using custom go plugins to dynamically set the target_url for a given Tyk Classic API Definition. This workaround solution revolves around the idea that you can utilize the URL Rewrite middleware as a way to trigger the Gateway to rewrite the target_url in a custom go plugin satisfying the following code: https://github.com/TykTechnologies/tyk/blob/master/gateway/reverse_proxy.go#L271C6-L271C67.

Basic testing has been done and it should preserve the METHOD and relative PATH.

The minimum requirement here is that you need to configure your Tyk Classic API Definition such that the Tyk Gateway is aware of the URL Rewrite and to satisfy the condition: if spec.URLRewriteEnabled && req.Context().Value(ctx.RetainHost) == true

@LLe27
LLe27 / header-and-tyk-session-example.go
Last active March 27, 2025 13:15
Custom Go Plugin - Extract Request Header from Request and Response Middleware
package main
import (
"net/http"
"github.com/TykTechnologies/tyk/ctx"
"github.com/TykTechnologies/tyk/log"
)
var logger = log.Get()
@LLe27
LLe27 / api-def-example.json
Last active March 27, 2025 13:15
Custom Go Plugin - Utilizing Tyk OOTB Authentication Token and Middleware
{
"custom_middleware": {
"pre": [],
"post": [],
"post_key_auth": [],
"auth_check": {
"disabled": false,
"name": "Authenticate",
"path": "/opt/tyk-gateway/middleware/CustomGoPlugin.so",
"require_session": false,
@LLe27
LLe27 / README.md
Last active March 27, 2025 13:16
Custom Go Plugin -- Pump Analytics Data

// dump command for tyk-pump ./tyk-pump --demo=6538df7641102200887bfe99 --demo-track-path --demo-days=90 --demo-records-per-hour=100 --conf=./pump.conf

@LLe27
LLe27 / aws-lambda-func.go
Last active March 27, 2025 13:17
Custom Go Plugin - Run AWS Lambda Function
func AddFooBarHeader(rw http.ResponseWriter, r *http.Request) {
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("us-east-1"),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(
"KEYID",
"SECRET",
"AWS_TOKEN")),
)
if err != nil {
fmt.Println("Couldn't load default configuration. Have you set up your AWS account?")
@LLe27
LLe27 / detect-mtls.go
Last active March 27, 2025 13:17
Custom Go Plugin - Detect incoming TLS and certificates for an API request
var logger = log.Get()
// Main method to be run by Tyk
func DetectMTLS(w http.ResponseWriter, r *http.Request) {
logger.Info("-----------")
logger.Info("Detecting if TLS is used for this request!")
if r.TLS == nil {
logger.Warn("TLS IS NOT being used for this request!")
return
@LLe27
LLe27 / api-def-plugin-example.json
Last active March 27, 2025 13:18
Custom Go Plugin - Validate and print Cookie
"custom_middleware": {
"pre": [
{
"name": "AddFooBarHeader",
"path": "CustomGoPlugin.so",
"require_session": false,
"raw_body_only": false
}
],
"driver": "goplugin",
@LLe27
LLe27 / api-def-example.json
Last active March 27, 2025 13:19
Custom Go Plugin - Stdout websocket analytics
{
// Custom Plugin Config
"custom_middleware": {
"pre": [],
"post": [
{
"disabled": false,
"name": "WebSocketAnalytics",
"path": "/opt/tyk-gateway/middleware/CustomGoPlugin.so",
"require_session": false,
@LLe27
LLe27 / http-context-data-plugin-example.go
Last active March 27, 2025 13:20
Custom Go Plugin - Write to HTTP context
package main
import (
"context"
"net/http"
"github.com/TykTechnologies/tyk/log"
)
// HTTP Request Contexts & Go Reference: