mirror of
https://github.com/coaidev/coai.git
synced 2025-05-19 21:10:18 +09:00
update code struct
This commit is contained in:
parent
29d161ba9c
commit
821fba5756
@ -1,6 +1,8 @@
|
|||||||
package main
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"chat/connection"
|
||||||
|
"chat/utils"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -14,13 +16,8 @@ type AnonymousRequestBody struct {
|
|||||||
Message string `json:"message" required:"true"`
|
Message string `json:"message" required:"true"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChatGPTMessage struct {
|
|
||||||
Role string `json:"role"`
|
|
||||||
Content string `json:"content"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAnonymousResponse(message string) (string, error) {
|
func GetAnonymousResponse(message string) (string, error) {
|
||||||
res, err := Post(viper.GetString("openai.anonymous_endpoint")+"/chat/completions", map[string]string{
|
res, err := utils.Post(viper.GetString("openai.anonymous_endpoint")+"/chat/completions", map[string]string{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Authorization": "Bearer " + viper.GetString("openai.anonymous"),
|
"Authorization": "Bearer " + viper.GetString("openai.anonymous"),
|
||||||
}, map[string]interface{}{
|
}, map[string]interface{}{
|
||||||
@ -41,13 +38,13 @@ func GetAnonymousResponse(message string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetAnonymousResponseWithCache(c context.Context, message string) (string, error) {
|
func GetAnonymousResponseWithCache(c context.Context, message string) (string, error) {
|
||||||
res, err := Cache.Get(c, fmt.Sprintf(":chatgpt:%s", message)).Result()
|
res, err := connection.Cache.Get(c, fmt.Sprintf(":chatgpt:%s", message)).Result()
|
||||||
if err != nil || len(res) == 0 {
|
if err != nil || len(res) == 0 {
|
||||||
res, err := GetAnonymousResponse(message)
|
res, err := GetAnonymousResponse(message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "There was something wrong...", err
|
return "There was something wrong...", err
|
||||||
}
|
}
|
||||||
Cache.Set(c, fmt.Sprintf(":chatgpt:%s", message), res, time.Hour*6)
|
connection.Cache.Set(c, fmt.Sprintf(":chatgpt:%s", message), res, time.Hour*6)
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
6
api/types.go
Normal file
6
api/types.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
type ChatGPTMessage struct {
|
||||||
|
Role string `json:"role"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package main
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"chat/utils"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
@ -12,12 +13,12 @@ type ValidateUserResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Validate(token string) *ValidateUserResponse {
|
func Validate(token string) *ValidateUserResponse {
|
||||||
res, err := Post("https://api.deeptrain.net/app/validate", map[string]string{
|
res, err := utils.Post("https://api.deeptrain.net/app/validate", map[string]string{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
}, map[string]interface{}{
|
}, map[string]interface{}{
|
||||||
"password": viper.GetString("auth.access"),
|
"password": viper.GetString("auth.access"),
|
||||||
"token": token,
|
"token": token,
|
||||||
"hash": Sha2Encrypt(token + viper.GetString("auth.salt")),
|
"hash": utils.Sha2Encrypt(token + viper.GetString("auth.salt")),
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil || res == nil || res.(map[string]interface{})["status"] == false {
|
if err != nil || res == nil || res.(map[string]interface{})["status"] == false {
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package connection
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
6
main.go
6
main.go
@ -1,6 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"chat/api"
|
||||||
|
"chat/connection"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
@ -10,11 +12,11 @@ func main() {
|
|||||||
if err := viper.ReadInConfig(); err != nil {
|
if err := viper.ReadInConfig(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
ConnectRedis()
|
connection.ConnectRedis()
|
||||||
|
|
||||||
app := gin.Default()
|
app := gin.Default()
|
||||||
{
|
{
|
||||||
app.POST("/api/anonymous", AnonymousAPI)
|
app.POST("/api/anonymous", api.AnonymousAPI)
|
||||||
}
|
}
|
||||||
if err := app.Run(":" + viper.GetString("server.port")); err != nil {
|
if err := app.Run(":" + viper.GetString("server.port")); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
34
middleware/cors.go
Normal file
34
middleware/cors.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"chat/utils"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
var allowedOrigins = []string{
|
||||||
|
"https://fystart.cn",
|
||||||
|
"https://www.fystart.cn",
|
||||||
|
"https://deeptrain.net",
|
||||||
|
"https://www.deeptrain.net",
|
||||||
|
"http://localhost",
|
||||||
|
}
|
||||||
|
|
||||||
|
func CORSMiddleware() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
origin := c.Request.Header.Get("Origin")
|
||||||
|
if utils.Contains(origin, allowedOrigins) {
|
||||||
|
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
|
||||||
|
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
||||||
|
c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
|
||||||
|
|
||||||
|
if c.Request.Method == "OPTIONS" {
|
||||||
|
c.Writer.Header().Set("Access-Control-Max-Age", "3600")
|
||||||
|
c.AbortWithStatus(http.StatusOK)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
155
utils.go
155
utils.go
@ -1,155 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"crypto/aes"
|
|
||||||
"crypto/cipher"
|
|
||||||
crand "crypto/rand"
|
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/hex"
|
|
||||||
"encoding/json"
|
|
||||||
"io"
|
|
||||||
"math/rand"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Sha2Encrypt(raw string) string {
|
|
||||||
hash := sha256.Sum256([]byte(raw))
|
|
||||||
return hex.EncodeToString(hash[:])
|
|
||||||
}
|
|
||||||
|
|
||||||
func AES256Encrypt(key string, data string) (string, error) {
|
|
||||||
text := []byte(data)
|
|
||||||
block, err := aes.NewCipher([]byte(key))
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
iv := make([]byte, aes.BlockSize)
|
|
||||||
if _, err := io.ReadFull(crand.Reader, iv); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
encryptor := cipher.NewCFBEncrypter(block, iv)
|
|
||||||
|
|
||||||
ciphertext := make([]byte, len(text))
|
|
||||||
encryptor.XORKeyStream(ciphertext, text)
|
|
||||||
return hex.EncodeToString(ciphertext), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func AES256Decrypt(key string, data string) (string, error) {
|
|
||||||
ciphertext, err := hex.DecodeString(data)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
block, err := aes.NewCipher([]byte(key))
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
iv := ciphertext[:aes.BlockSize]
|
|
||||||
ciphertext = ciphertext[aes.BlockSize:]
|
|
||||||
|
|
||||||
decryptor := cipher.NewCFBDecrypter(block, iv)
|
|
||||||
plaintext := make([]byte, len(ciphertext))
|
|
||||||
decryptor.XORKeyStream(plaintext, ciphertext)
|
|
||||||
|
|
||||||
return string(plaintext), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func Http(uri string, method string, ptr interface{}, headers map[string]string, body io.Reader) (err error) {
|
|
||||||
req, err := http.NewRequest(method, uri, body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for key, value := range headers {
|
|
||||||
req.Header.Set(key, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
client := &http.Client{}
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
if err = json.NewDecoder(resp.Body).Decode(ptr); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func Get(uri string, headers map[string]string) (data interface{}, err error) {
|
|
||||||
err = Http(uri, http.MethodGet, &data, headers, nil)
|
|
||||||
return data, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func Post(uri string, headers map[string]string, body interface{}) (data interface{}, err error) {
|
|
||||||
var form io.Reader
|
|
||||||
if buffer, err := json.Marshal(body); err == nil {
|
|
||||||
form = bytes.NewBuffer(buffer)
|
|
||||||
}
|
|
||||||
err = Http(uri, http.MethodPost, &data, headers, form)
|
|
||||||
return data, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func PostForm(uri string, body map[string]interface{}) (data map[string]interface{}, err error) {
|
|
||||||
client := &http.Client{}
|
|
||||||
form := make(url.Values)
|
|
||||||
for key, value := range body {
|
|
||||||
form[key] = []string{value.(string)}
|
|
||||||
}
|
|
||||||
res, err := client.PostForm(uri, form)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
content, err := io.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = json.Unmarshal(content, &data); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return data, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GenerateCode(length int) string {
|
|
||||||
var code string
|
|
||||||
for i := 0; i < length; i++ {
|
|
||||||
code += strconv.Itoa(rand.Intn(10))
|
|
||||||
}
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
||||||
func GenerateChar(length int) string {
|
|
||||||
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
||||||
result := make([]byte, length)
|
|
||||||
for i := 0; i < length; i++ {
|
|
||||||
result[i] = charset[rand.Intn(len(charset))]
|
|
||||||
}
|
|
||||||
return string(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ConvertTime(t []uint8) *time.Time {
|
|
||||||
val, err := time.Parse("2006-01-02 15:04:05", string(t))
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return &val
|
|
||||||
}
|
|
||||||
|
|
||||||
func Contains[T comparable](value T, slice []T) bool {
|
|
||||||
for _, item := range slice {
|
|
||||||
if item == value {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
10
utils/base.go
Normal file
10
utils/base.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
func Contains[T comparable](value T, slice []T) bool {
|
||||||
|
for _, item := range slice {
|
||||||
|
if item == value {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
32
utils/char.go
Normal file
32
utils/char.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GenerateCode(length int) string {
|
||||||
|
var code string
|
||||||
|
for i := 0; i < length; i++ {
|
||||||
|
code += strconv.Itoa(rand.Intn(10))
|
||||||
|
}
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateChar(length int) string {
|
||||||
|
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||||
|
result := make([]byte, length)
|
||||||
|
for i := 0; i < length; i++ {
|
||||||
|
result[i] = charset[rand.Intn(len(charset))]
|
||||||
|
}
|
||||||
|
return string(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ConvertTime(t []uint8) *time.Time {
|
||||||
|
val, err := time.Parse("2006-01-02 15:04:05", string(t))
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &val
|
||||||
|
}
|
55
utils/encrypt.go
Normal file
55
utils/encrypt.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
crand "crypto/rand"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Sha2Encrypt(raw string) string {
|
||||||
|
hash := sha256.Sum256([]byte(raw))
|
||||||
|
return hex.EncodeToString(hash[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func AES256Encrypt(key string, data string) (string, error) {
|
||||||
|
text := []byte(data)
|
||||||
|
block, err := aes.NewCipher([]byte(key))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
iv := make([]byte, aes.BlockSize)
|
||||||
|
if _, err := io.ReadFull(crand.Reader, iv); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
encryptor := cipher.NewCFBEncrypter(block, iv)
|
||||||
|
|
||||||
|
ciphertext := make([]byte, len(text))
|
||||||
|
encryptor.XORKeyStream(ciphertext, text)
|
||||||
|
return hex.EncodeToString(ciphertext), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func AES256Decrypt(key string, data string) (string, error) {
|
||||||
|
ciphertext, err := hex.DecodeString(data)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
block, err := aes.NewCipher([]byte(key))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
iv := ciphertext[:aes.BlockSize]
|
||||||
|
ciphertext = ciphertext[aes.BlockSize:]
|
||||||
|
|
||||||
|
decryptor := cipher.NewCFBDecrypter(block, iv)
|
||||||
|
plaintext := make([]byte, len(ciphertext))
|
||||||
|
decryptor.XORKeyStream(plaintext, ciphertext)
|
||||||
|
|
||||||
|
return string(plaintext), nil
|
||||||
|
}
|
68
utils/net.go
Normal file
68
utils/net.go
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Http(uri string, method string, ptr interface{}, headers map[string]string, body io.Reader) (err error) {
|
||||||
|
req, err := http.NewRequest(method, uri, body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for key, value := range headers {
|
||||||
|
req.Header.Set(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if err = json.NewDecoder(resp.Body).Decode(ptr); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Get(uri string, headers map[string]string) (data interface{}, err error) {
|
||||||
|
err = Http(uri, http.MethodGet, &data, headers, nil)
|
||||||
|
return data, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func Post(uri string, headers map[string]string, body interface{}) (data interface{}, err error) {
|
||||||
|
var form io.Reader
|
||||||
|
if buffer, err := json.Marshal(body); err == nil {
|
||||||
|
form = bytes.NewBuffer(buffer)
|
||||||
|
}
|
||||||
|
err = Http(uri, http.MethodPost, &data, headers, form)
|
||||||
|
return data, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func PostForm(uri string, body map[string]interface{}) (data map[string]interface{}, err error) {
|
||||||
|
client := &http.Client{}
|
||||||
|
form := make(url.Values)
|
||||||
|
for key, value := range body {
|
||||||
|
form[key] = []string{value.(string)}
|
||||||
|
}
|
||||||
|
res, err := client.PostForm(uri, form)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
content, err := io.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = json.Unmarshal(content, &data); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return data, nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user