同级目录建立view文件夹。
package main
import (
"bytes"
"embed"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"html/template"
"io/ioutil"
"net/http"
)
const (
gptUrl = "https://api.zhishuyun.com/chatgpt"
gptToken = "1111"
gptImageUrl = "https://api.zhishuyun.com/midjourney/imagine"
gptImageToken = "2222"
)
type gptRes struct {
Answer string `json:"answer"`
ConversationId string `json:"conversation_id"`
}
type gptImgRes struct {
ImageUrl string `json:"image_url"`
}
// 这句话是设置打包
//go:embed view/*
var htmlFS embed.FS
func main() {
router := gin.Default()
router.Use(Cors())
// 设置Gin框架的HTML模板和静态文件路由
router.Any("/view/*filepath", func(c *gin.Context) {
staticServer := http.FileServer(http.FS(htmlFS))
staticServer.ServeHTTP(c.Writer, c.Request)
})
// 引入html
router.SetHTMLTemplate(template.Must(template.New("").ParseFS(htmlFS, "view/*")))
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{})
})
//问答
router.POST("/ask", func(c *gin.Context) {
msg := c.PostForm("msg")
song := make(map[string]interface{})
song["stateful"] = true
song["question"] = msg
song["conversation_id"] = ""
song["timeout"] = 3
song["retry_times"] = 3
content := Post(gptUrl, gptToken, song)
var res gptRes
var answer string
err := json.Unmarshal([]byte(content), &res)
if err != nil {
answer = err.Error()
}
answer = res.Answer
c.JSON(http.StatusOK, gin.H{
"answer": answer,
})
})
//图片
router.POST("/img", func(c *gin.Context) {
msg := c.PostForm("msg")
song := make(map[string]interface{})
song["action"] = "generate"
song["prompt"] = msg
song["timeout"] = 300
song["callback_url"] = ""
content := Post(gptImageUrl, gptImageToken, song)
var res gptImgRes
var answer string
err := json.Unmarshal([]byte(content), &res)
if err != nil {
answer = err.Error()
}
answer = res.ImageUrl
c.JSON(http.StatusOK, gin.H{
"imageUrl": answer,
})
})
router.Run(":6789")
}
// 发送post请求
func Post(url, secret string, data map[string]interface{}) string {
client := &http.Client{}
// 设置请求体,json格式
bytesData, _ := json.Marshal(data)
req, err := http.NewRequest("POST", url+"?token="+secret,
bytes.NewBuffer([]byte(bytesData)))
if err != nil {
fmt.Println("Fatal error ", err.Error())
}
//设置请求头
req.Header.Set("content-type", "application/json")
req.Header.Set("content-type", "application/json")
resp, err := client.Do(req)
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err.Error()
}
return string(content)
}
func Cors() gin.HandlerFunc {
return func(context *gin.Context) {
method := context.Request.Method
context.Header("Access-Control-Allow-Origin", "*")
context.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token, x-token")
context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PATCH, PUT")
context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
context.Header("Access-Control-Allow-Credentials", "true")
if method == "OPTIONS" {
context.AbortWithStatus(http.StatusNoContent)
}
}
}
标签:string,err,song,二进制,answer,json,Go,gin,embed
From: https://www.cnblogs.com/qcy-blog/p/18019427