zhangwei пре 3 година
родитељ
комит
f711b2ba56

+ 12 - 1
chfoot/ChFootMain.go

@@ -2,6 +2,8 @@ package main
 
 import (
 	"chathub/chfoot/core"
+	"chathub/chfoot/http"
+	"flag"
 	"fmt"
 	"github.com/etlibgo/et"
 	"time"
@@ -12,7 +14,16 @@ func main() {
 
 	fmt.Println(et.StrPadLeft("et pad left", "0", 20))
 
-	core.AppPrepare()
+	pStrCfgPath := flag.String("cfgpath", "foot-cfg/foot_main.cfg", "see foot-cfg")
+	flag.Parse()
+
+	strCfgContent := et.FileReadStr(*pStrCfgPath)
+	pJsonCfg := et.ExJsonFromStr(strCfgContent)
+	core.AppInit(pJsonCfg)
+
+	http.FootHttpEntry(core.AppGetHttpServer())
+
+	fmt.Println("cfg get name:" + core.CfgGetStr("name"))
 	core.AppStart()
 
 	for core.AppIsRunning() {

+ 12 - 9
chfoot/core/App.go

@@ -1,20 +1,27 @@
 package core
 
 import (
-	"chathub/chfoot/http"
 	"fmt"
+	"github.com/etlibgo/et"
 
 	"github.com/gin-gonic/gin"
 )
 
 var sApp = struct {
-	Running bool
+	HttpServer *gin.Engine
+	Running    bool
 }{
 	Running: false,
 }
 
-func AppPrepare() {
+func AppInit(jsonCfg *et.ExJsonVal) {
+	CfgInit(jsonCfg)
 
+	sApp.HttpServer = gin.Default()
+}
+
+func AppGetHttpServer() *gin.Engine {
+	return sApp.HttpServer
 }
 
 func AppStart() {
@@ -25,18 +32,14 @@ func AppStart() {
 
 	sApp.Running = true
 
-	eg := gin.Default()
-
-	http.FootHttpEntry(eg)
-
-	eg.GET("/ping", func(c *gin.Context) { // /ping是路由
+	sApp.HttpServer.GET("/ping", func(c *gin.Context) { // /ping是路由
 		//输出json结果给调用方
 		c.JSON(200, gin.H{
 			"message": "pong",
 		})
 	})
 
-	go eg.Run(":8081") // 可以从本地的127.0.0.1:8081访问,不填的话,默认是8080端口
+	go sApp.HttpServer.Run(":8081") // 可以从本地的127.0.0.1:8081访问,不填的话,默认是8080端口
 
 	fmt.Println("foot AppStart started")
 }

+ 34 - 0
chfoot/core/Cfg.go

@@ -0,0 +1,34 @@
+package core
+
+import "github.com/etlibgo/et"
+
+var sCfg = struct {
+	JsonRoot *et.ExJsonVal
+	IsInited bool
+}{
+	JsonRoot: nil,
+	IsInited: false,
+}
+
+func CfgInit(jsonCfg *et.ExJsonVal) {
+	sCfg.JsonRoot = jsonCfg
+	if sCfg.JsonRoot != nil {
+		sCfg.IsInited = true
+	}
+}
+
+func CfgGetStr(strKey string) string {
+	if !sCfg.IsInited {
+		return ""
+	}
+
+	return et.ExJsonGetStr(sCfg.JsonRoot, strKey)
+}
+
+func CfgGetStrPath(arrPath []string) string {
+	if !sCfg.IsInited {
+		return ""
+	}
+
+	return et.ExJsonPathGetStr(sCfg.JsonRoot, arrPath)
+}

+ 3 - 0
chfoot/http/HttpEntry.go

@@ -1,6 +1,7 @@
 package http
 
 import (
+	"chathub/chfoot/http/app"
 	"chathub/chfoot/http/chat"
 	"chathub/chfoot/http/test"
 	"github.com/gin-gonic/gin"
@@ -12,4 +13,6 @@ func FootHttpEntry(eg *gin.Engine) {
 
 	eg.GET("/chat_ask", chat.FootApiChatAsk)
 
+	eg.GET("/app_stop", app.FootApiAppStop)
+
 }

+ 19 - 0
chfoot/http/app/AppHome.go

@@ -0,0 +1,19 @@
+package app
+
+import (
+	"chathub/chfoot/core"
+	"github.com/gin-gonic/gin"
+	"time"
+)
+
+func FootApiAppStop(c *gin.Context) { // /ping是路由
+	//输出json结果给调用方
+	c.JSON(200, gin.H{
+		"message": "stopped",
+	})
+
+	go func() {
+		time.Sleep(1000)
+		core.AppStop()
+	}()
+}

+ 43 - 2
chfoot/http/chat/ChatHome.go

@@ -1,10 +1,51 @@
 package chat
 
-import "github.com/gin-gonic/gin"
+import (
+	"github.com/gin-gonic/gin"
+	"net/http"
+	"time"
+)
+
+import chatgpt "github.com/chatgp/chatgpt-go"
 
 func FootApiChatAsk(c *gin.Context) { // /ping是路由
 	//输出json结果给调用方
+
+	token := ""
+	cfValue := ""
+
+	cookies := []*http.Cookie{
+		{
+			Name:  "__Secure-next-auth.session-token",
+			Value: token,
+		},
+		{
+			Name:  "cf_clearance",
+			Value: cfValue,
+		},
+	}
+
+	chatClient := chatgpt.NewClient(
+		chatgpt.WithDebug(true),
+		chatgpt.WithTimeout(60*time.Second),
+		chatgpt.WithCookies(cookies),
+	)
+
+	question := "hello"
+	stream, err := chatClient.GetChatStream(question)
+	if err != nil {
+		c.JSON(200, gin.H{
+			"message": err.Error(),
+		})
+		return
+	}
+
+	var answer string
+	for text := range stream.Stream {
+		answer += text.Content
+	}
+
 	c.JSON(200, gin.H{
-		"message": "asking",
+		"message": answer,
 	})
 }

BIN
chfoot/out/chfoot.exe


+ 1 - 0
cmd/foot_build.bat

@@ -0,0 +1 @@
+go build -o ./out/chfoot.exe ./chfoot 

+ 3 - 0
foot-cfg/foot_main.cfg

@@ -0,0 +1,3 @@
+{
+"name":"chfoot"
+}

+ 6 - 0
go.mod

@@ -6,6 +6,7 @@ require github.com/etlibgo/et v1.0.0
 
 require (
 	github.com/bytedance/sonic v1.8.7 // indirect
+	github.com/chatgp/chatgpt-go v1.4.0 // indirect
 	github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
 	github.com/gin-contrib/sse v0.1.0 // indirect
 	github.com/gin-gonic/gin v1.9.0 // indirect
@@ -13,13 +14,18 @@ require (
 	github.com/go-playground/universal-translator v0.18.1 // indirect
 	github.com/go-playground/validator/v10 v10.12.0 // indirect
 	github.com/goccy/go-json v0.10.2 // indirect
+	github.com/google/uuid v1.3.0 // indirect
 	github.com/json-iterator/go v1.1.12 // indirect
 	github.com/klauspost/cpuid/v2 v2.2.4 // indirect
+	github.com/launchdarkly/eventsource v1.7.1 // indirect
 	github.com/leodido/go-urn v1.2.3 // indirect
 	github.com/mattn/go-isatty v0.0.18 // indirect
 	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
 	github.com/modern-go/reflect2 v1.0.2 // indirect
 	github.com/pelletier/go-toml/v2 v2.0.7 // indirect
+	github.com/tidwall/gjson v1.14.4 // indirect
+	github.com/tidwall/match v1.1.1 // indirect
+	github.com/tidwall/pretty v1.2.0 // indirect
 	github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
 	github.com/ugorji/go/codec v1.2.11 // indirect
 	golang.org/x/arch v0.3.0 // indirect

+ 17 - 0
go.sum

@@ -1,6 +1,8 @@
 github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
 github.com/bytedance/sonic v1.8.7 h1:d3sry5vGgVq/OpgozRUNP6xBsSo0mtNdwliApw+SAMQ=
 github.com/bytedance/sonic v1.8.7/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
+github.com/chatgp/chatgpt-go v1.4.0 h1:7ghhy6N8EklCc3CYlWuWjTcEP8+L475MGPdspD24NGM=
+github.com/chatgp/chatgpt-go v1.4.0/go.mod h1:8/Gyvxkyun0uyYR0KT58X86smJxEcx1EkkwmGaohn0Q=
 github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
 github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
 github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
@@ -21,11 +23,16 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
 github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
 github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
 github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
+github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
+github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
 github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
 github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
 github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
 github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
+github.com/launchdarkly/eventsource v1.7.1 h1:StoRQeiPyrcQIXjlQ7b5jWMzHW4p+GGczN2r2oBhujg=
+github.com/launchdarkly/eventsource v1.7.1/go.mod h1:LHxSeb4OnqznNZxCSXbFghxS/CjIQfzHovNoAqbO/Wk=
+github.com/launchdarkly/go-test-helpers/v2 v2.2.0/go.mod h1:L7+th5govYp5oKU9iN7To5PgznBuIjBPn+ejqKR0avw=
 github.com/leodido/go-urn v1.2.3 h1:6BE2vPT0lqoz3fmOesHZiaiFh7889ssCo2GMvLCfiuA=
 github.com/leodido/go-urn v1.2.3/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
 github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
@@ -42,11 +49,19 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
 github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
 github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
+github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
+github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
 github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
 github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
+github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
+github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
+github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
+github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
+github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
+github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
 github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
 github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
 github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
@@ -69,6 +84,8 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
 google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
 google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=