浏览代码

first commit

Yeuoly 1 年之前
当前提交
043e5251be

+ 3 - 0
.env.example

@@ -0,0 +1,3 @@
+DIFY_PLUGIN_HOST=127.0.0.1
+DIFY_PLUGIN_PORT=5002
+DIFY_PLUGIN_KEY=your_plugin_key

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+release/
+logs/
+.vscode/
+.env

+ 25 - 0
cmd/server/main.go

@@ -0,0 +1,25 @@
+package main
+
+import (
+	"github.com/joho/godotenv"
+	"github.com/kelseyhightower/envconfig"
+	"github.com/langgenius/dify-plugin-daemon/internal/daemon"
+	"github.com/langgenius/dify-plugin-daemon/internal/types/app"
+	"github.com/langgenius/dify-plugin-daemon/internal/utils/log"
+)
+
+func main() {
+	var config app.Config
+
+	err := godotenv.Load()
+	if err != nil {
+		log.Panic("Error loading .env file")
+	}
+
+	err = envconfig.Process("", &config)
+	if err != nil {
+		log.Panic("Error processing environment variables")
+	}
+
+	daemon.Run(&config)
+}

+ 8 - 0
go.mod

@@ -0,0 +1,8 @@
+module github.com/langgenius/dify-plugin-daemon
+
+go 1.20
+
+require (
+	github.com/joho/godotenv v1.5.1 // indirect
+	github.com/kelseyhightower/envconfig v1.4.0 // indirect
+)

+ 4 - 0
go.sum

@@ -0,0 +1,4 @@
+github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
+github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
+github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
+github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=

+ 1 - 0
internal/core/plugin_manager/init.go

@@ -0,0 +1 @@
+package plugin_manager

+ 1 - 0
internal/core/traffic_manager/init.go

@@ -0,0 +1 @@
+package traffic_manager

+ 9 - 0
internal/daemon/server.go

@@ -0,0 +1,9 @@
+package daemon
+
+import (
+	"github.com/langgenius/dify-plugin-daemon/internal/types/app"
+)
+
+func Run(config *app.Config) {
+
+}

+ 7 - 0
internal/types/app/config.go

@@ -0,0 +1,7 @@
+package app
+
+type Config struct {
+	DifyPluginHost string `envconfig:"DIFY_PLUGIN_HOST"`
+	DifyPluginPort int16  `envconfig:"DIFY_PLUGIN_PORT"`
+	DifyPluginKey  string `envconfig:"DIFY_PLUGIN_KEY"`
+}

+ 245 - 0
internal/utils/log/log.go

@@ -0,0 +1,245 @@
+package log
+
+/*
+	log module is used to write log info to log file
+	open a log file when log was created, and close it when log was destroyed
+*/
+
+import (
+	"fmt"
+	go_log "log"
+	"os"
+	"time"
+)
+
+type Log struct {
+	Level int
+	//File of log
+	File *os.File
+	path string
+}
+
+const (
+	LOG_LEVEL_DEBUG = 0
+	LOG_LEVEL_INFO  = 1
+	LOG_LEVEL_WARN  = 2
+	LOG_LEVEL_ERROR = 3
+)
+
+func (l *Log) Debug(format string, stdout bool, v ...interface{}) {
+	if l.Level <= LOG_LEVEL_DEBUG {
+		l.writeLog("DEBUG", format, stdout, v...)
+	}
+}
+
+func (l *Log) Info(format string, stdout bool, v ...interface{}) {
+	if l.Level <= LOG_LEVEL_INFO {
+		l.writeLog("INFO", format, stdout, v...)
+	}
+}
+
+func (l *Log) Warn(format string, stdout bool, v ...interface{}) {
+	if l.Level <= LOG_LEVEL_WARN {
+		l.writeLog("WARN", format, stdout, v...)
+	}
+}
+
+func (l *Log) Error(format string, stdout bool, v ...interface{}) {
+	if l.Level <= LOG_LEVEL_ERROR {
+		l.writeLog("ERROR", format, stdout, v...)
+	}
+}
+
+func (l *Log) Panic(format string, stdout bool, v ...interface{}) {
+	l.writeLog("PANIC", format, stdout, v...)
+	panic("")
+}
+
+func (l *Log) writeLog(level string, format string, stdout bool, v ...interface{}) {
+	//if the next day is coming, reopen file
+	if time.Now().Format("/2006-01-02.log") != l.File.Name() {
+		l.File.Close()
+		l.OpenFile()
+	}
+	//test if file is closed
+	if l.File == nil {
+		//open file
+		err := l.OpenFile()
+		if err != nil {
+			panic(err)
+		}
+	}
+	//write log
+	format = fmt.Sprintf("["+level+"]"+format, v...)
+
+	if show_log && stdout {
+		logger.Output(4, format)
+	}
+
+	_, err := l.File.Write([]byte(format + "\n"))
+	if err != nil {
+		//reopen file
+		l.File.Close()
+		l.OpenFile()
+	}
+}
+
+func (l *Log) SetLogLevel(level int) {
+	l.Level = level
+}
+
+func (l *Log) OpenFile() error {
+	//test if file is closed
+	if l.File == nil {
+		//open file
+		file, err := os.OpenFile(l.path+time.Now().Format("/2006-01-02.log"), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
+		if err != nil {
+			return err
+		}
+		l.File = file
+	}
+	//test if file is writable
+	_, err := l.File.Write([]byte(" "))
+	if err != nil {
+		//reopen file
+		l.File.Close()
+		file, err := os.OpenFile(l.path+time.Now().Format("/2006-01-02.log"), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
+		if err != nil {
+			return err
+		}
+		l.File = file
+	}
+	return nil
+}
+
+func NewLog(path string) (*Log, error) {
+	if path == "" {
+		path = "log"
+	}
+	//test if path is exist
+	_, err := os.Stat(path)
+	if err != nil {
+		//create path
+		err = os.MkdirAll(path, 0777)
+		if err != nil {
+			return nil, err
+		}
+	}
+	// test if path is a directory
+	fileInfo, err := os.Stat(path)
+	if err != nil {
+		return nil, err
+	}
+	if !fileInfo.IsDir() {
+		return nil, fmt.Errorf("log file path %s is not a directory", path)
+	}
+
+	log := &Log{
+		Level: LOG_LEVEL_DEBUG,
+		path:  path,
+	}
+	//open file
+	err = log.OpenFile()
+	if err != nil {
+		return nil, err
+	}
+	return log, nil
+}
+
+func init() {
+	// why logger will cause panic when call
+	initlog()
+}
+
+func initlog() {
+	var err error
+	main_log, err = NewLog("./logs")
+	if err != nil {
+		panic(err)
+	}
+}
+
+var main_log *Log // wapper of go_log
+var show_log bool = true
+var logger = go_log.New(os.Stdout, "", go_log.Ldate|go_log.Ltime|go_log.Lshortfile)
+
+func SetShowLog(show bool) {
+	show_log = show
+}
+
+func SetLogLevel(level int) {
+	if main_log == nil {
+		initlog()
+	}
+	main_log.SetLogLevel(level)
+}
+
+func Debug(format string, v ...interface{}) {
+	if main_log == nil {
+		initlog()
+	}
+	main_log.Debug(format, true, v...)
+}
+
+func Info(format string, v ...interface{}) {
+	if main_log == nil {
+		initlog()
+	}
+	main_log.Info(format, true, v...)
+}
+
+func Warn(format string, v ...interface{}) {
+	if main_log == nil {
+		initlog()
+	}
+	main_log.Warn(format, true, v...)
+}
+
+func Error(format string, v ...interface{}) {
+	if main_log == nil {
+		initlog()
+	}
+	main_log.Error(format, true, v...)
+}
+
+func Panic(format string, v ...interface{}) {
+	if main_log == nil {
+		initlog()
+	}
+	main_log.Panic(format, true, v...)
+}
+
+func SlientDebug(format string, v ...interface{}) {
+	if main_log == nil {
+		initlog()
+	}
+	main_log.Debug(format, false, v...)
+}
+
+func SlientInfo(format string, v ...interface{}) {
+	if main_log == nil {
+		initlog()
+	}
+	main_log.Info(format, false, v...)
+}
+
+func SlientWarn(format string, v ...interface{}) {
+	if main_log == nil {
+		initlog()
+	}
+	main_log.Warn(format, false, v...)
+}
+
+func SlientError(format string, v ...interface{}) {
+	if main_log == nil {
+		initlog()
+	}
+	main_log.Error(format, false, v...)
+}
+
+func SlientPanic(format string, v ...interface{}) {
+	if main_log == nil {
+		initlog()
+	}
+	main_log.Panic(format, false, v...)
+}