Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build go plugin with .env file #8

Open
louistwiice opened this issue Jan 12, 2023 · 0 comments
Open

Build go plugin with .env file #8

louistwiice opened this issue Jan 12, 2023 · 0 comments

Comments

@louistwiice
Copy link

Hello guyz !

I need to send global parameter to my plugin when running it, so for that, the solution I found is to create en .env file (Maybe there is an other solution. If yes I will take it). So I would like to know if it is possible to build go plugin and link it to an .env file.

I have a simple plugin

My go file

package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"os"

	"github.com/TykTechnologies/tyk/log"
	_ "github.com/lib/pq"
	"github.com/joho/godotenv"
)

type AuthDBConfig struct {
	DBHost 		string `json:"db_host"`
	DBPort 		int     `json:"db_port"`
	DBUser		string  `json:"db_user"`
	DBPassword	string  `json:"db_password"`
	DBName		string  `json:"db_name"`
}

var logger = log.Get()

func SimpleMiddleware(w http.ResponseWriter, req *http.Request) {
    err := godotenv.Load("local.env")
    if err != nil {
	logger.Error("some error occured. Err: %s", err)
	return
    }
    logger.Info("*** Authorizarion checking! ***")


    logger.Info(fmt.Sprintf("User %s authenticated succesfully", user_id))
    req.Header.Add("Foo", "Bar")
    req.Header.Add("UserID", user_id)
    req.Header.Add("FirstName", os.Getenv("FIRST_NAME"))
    req.Header.Add("LastName", os.Getenv("LAST_NAME"))
}

func main() {}

func init() {
	logger.Info("--- Go custom plugin v4 init successful! ---- ")
}

My local.env file

FIRST_NAME=something
LAST_NAME=link

building plugin
docker run --rm -v `pwd`:/plugin-source tykio/tyk-plugin-compiler:v4.1.0 plugin.so

But when I run Tyk with the plugin installed, it crashes.

Do you know how to do this please

Thanks a lot for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant