修复无法后台运行

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
import (
"github.com/nsf/termbox-go"
"sync"
)
@ -20,14 +19,6 @@ type Config struct {
Verification string `json:"verification"`
}
// InfoItem 表示要显示的内容项
type InfoItem struct {
Title string
Value string
TitleColor termbox.Attribute // 标题颜色
ValueColor termbox.Attribute // 内容颜色
}
var (
//初始化需要返回给客户端的响应体
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"
"encoding/json"
"fmt"
"github.com/fatih/color"
"github.com/gin-gonic/gin"
"github.com/go-resty/resty/v2"
"github.com/nsf/termbox-go"
"io"
"log"
"math/rand"
@ -63,68 +63,41 @@ func initServer(engine *gin.Engine) {
}()
}
func showMsg() {
exitChan := make(chan bool)
dataGetter := func() []InfoItem {
var url = ""
if configFile.Server.Port == 80 {
url = "http://" + configFile.Server.Domain
} else if configFile.Server.Port == 443 {
url = "https://" + configFile.Server.Domain
} else {
url = "http://" + configFile.Server.Domain + ":" + strconv.Itoa(configFile.Server.Port)
}
var url = ""
if configFile.Server.Port == 80 {
url = "http://" + configFile.Server.Domain
} else if configFile.Server.Port == 443 {
url = "https://" + configFile.Server.Domain
} else {
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()
sCount := successCount
tCount := requestCount
gCount := githubApiCount
requestCountMutex.Unlock()
currentTime := time.Now().Format("2006-01-02 15:04:05")
if "00:00:00" == currentTime {
resetRequestCount()
}
return []InfoItem{
{
Title: "[Jetbrains]",
Value: url + "/copilot_internal/v2/token",
TitleColor: termbox.ColorGreen,
ValueColor: termbox.ColorYellow,
},
{
Title: "[Vscode]",
Value: url,
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,
},
}
var s2 = color.WhiteString("[Succeed]")
var s3 = color.WhiteString("[Failed]")
var s4 = color.WhiteString("[GithubApi]")
// 打印文本
fmt.Printf("\033[G%s - %s: %s %s: %s %s: %s ",
color.HiYellowString(currentTime),
s2, color.GreenString(strconv.Itoa(sCount)),
s3, color.RedString(strconv.Itoa(tCount-sCount)),
s4, color.CyanString(strconv.Itoa(gCount)))
time.Sleep(1 * time.Second) //
}
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 {
return func(c *gin.Context) {