修复无法后台运行

This commit is contained in:
ben Gutier 2023-09-10 23:04:14 +08:00
parent 088c15e54b
commit d274cf819a
3 changed files with 27 additions and 113 deletions

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"github.com/nsf/termbox-go"
"sync" "sync"
) )
@ -20,14 +19,6 @@ type Config struct {
Verification string `json:"verification"` Verification string `json:"verification"`
} }
// InfoItem 表示要显示的内容项
type InfoItem struct {
Title string
Value string
TitleColor termbox.Attribute // 标题颜色
ValueColor termbox.Attribute // 内容颜色
}
var ( var (
//初始化需要返回给客户端的响应体 //初始化需要返回给客户端的响应体
responseData map[string]interface{} responseData map[string]interface{}

View File

@ -1,50 +0,0 @@
package main
import (
"github.com/nsf/termbox-go"
"time"
)
func DisplayInfo(dataGetter func() []InfoItem, exitChan chan bool) {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
termbox.SetInputMode(termbox.InputEsc)
for {
err := termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
if err != nil {
return
}
y := 0
data := dataGetter()
for _, item := range data {
drawString(0, y, item.Title, item.TitleColor, termbox.ColorDefault)
drawString(len(item.Title)+1, y, item.Value, item.ValueColor, termbox.ColorDefault)
y++
}
err = termbox.Flush()
if err != nil {
return
}
select {
case <-exitChan:
return // 通过退出通道退出循环
default:
}
time.Sleep(1 * time.Second)
}
}
func drawString(x, y int, text string, fg, bg termbox.Attribute) {
for _, char := range text {
termbox.SetCell(x, y, char, fg, bg)
x++
}
}

View File

@ -4,9 +4,9 @@ import (
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/fatih/color"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
"github.com/nsf/termbox-go"
"io" "io"
"log" "log"
"math/rand" "math/rand"
@ -63,8 +63,6 @@ func initServer(engine *gin.Engine) {
}() }()
} }
func showMsg() { func showMsg() {
exitChan := make(chan bool)
dataGetter := func() []InfoItem {
var url = "" var url = ""
if configFile.Server.Port == 80 { if configFile.Server.Port == 80 {
url = "http://" + configFile.Server.Domain url = "http://" + configFile.Server.Domain
@ -73,58 +71,33 @@ func showMsg() {
} else { } else {
url = "http://" + configFile.Server.Domain + ":" + strconv.Itoa(configFile.Server.Port) url = "http://" + configFile.Server.Domain + ":" + strconv.Itoa(configFile.Server.Port)
} }
var jetStr = color.WhiteString("[Jetbrains]")
var vsStr = color.WhiteString("[Vscode]")
fmt.Println(jetStr + ": " + url + color.HiBlueString("/copilot_internal/v2/token"))
fmt.Println(vsStr + ": " + color.HiBlueString(url))
fmt.Println(color.WhiteString("-----------------------------------------------------------------------"))
for {
requestCountMutex.Lock() requestCountMutex.Lock()
sCount := successCount sCount := successCount
tCount := requestCount
gCount := githubApiCount gCount := githubApiCount
requestCountMutex.Unlock() requestCountMutex.Unlock()
currentTime := time.Now().Format("2006-01-02 15:04:05") currentTime := time.Now().Format("2006-01-02 15:04:05")
if "00:00:00" == currentTime { if "00:00:00" == currentTime {
resetRequestCount() resetRequestCount()
} }
return []InfoItem{ var s2 = color.WhiteString("[Succeed]")
{ var s3 = color.WhiteString("[Failed]")
Title: "[Jetbrains]", var s4 = color.WhiteString("[GithubApi]")
Value: url + "/copilot_internal/v2/token", // 打印文本
TitleColor: termbox.ColorGreen, fmt.Printf("\033[G%s - %s: %s %s: %s %s: %s ",
ValueColor: termbox.ColorYellow, color.HiYellowString(currentTime),
}, s2, color.GreenString(strconv.Itoa(sCount)),
{ s3, color.RedString(strconv.Itoa(tCount-sCount)),
Title: "[Vscode]", s4, color.CyanString(strconv.Itoa(gCount)))
Value: url, time.Sleep(1 * time.Second) //
TitleColor: termbox.ColorGreen,
ValueColor: termbox.ColorYellow,
},
{
Title: "[User Request]:",
Value: strconv.Itoa(sCount),
TitleColor: termbox.ColorGreen,
ValueColor: termbox.ColorYellow,
},
{
Title: "[GithubApi request]:",
Value: strconv.Itoa(gCount),
TitleColor: termbox.ColorGreen,
ValueColor: termbox.ColorYellow,
},
{
Title: "Time:",
Value: currentTime,
TitleColor: termbox.ColorGreen,
ValueColor: termbox.ColorYellow,
},
} }
}
exitChan = make(chan bool)
// 调用显示函数,传递数据获取器函数
go DisplayInfo(dataGetter, exitChan)
for {
ev := termbox.PollEvent()
if ev.Type == termbox.EventKey && ev.Key == termbox.KeyEsc {
exitChan <- true // 发送退出信号
break
}
}
} }
func getToken() gin.HandlerFunc { func getToken() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {