mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-12-27 02:25:58 +08:00
完成前后端框架搭建,完成聊天页面布局
This commit is contained in:
11
server/chat_handler.go
Normal file
11
server/chat_handler.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (s *Server) Chat(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 0, "message": fmt.Sprintf("HELLO, ChatGPT !!!")})
|
||||
}
|
||||
65
server/client.go
Normal file
65
server/client.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gorilla/websocket"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var ErrConClosed = errors.New("connection closed")
|
||||
|
||||
type Client interface {
|
||||
Close()
|
||||
}
|
||||
|
||||
// WsClient websocket client
|
||||
type WsClient struct {
|
||||
NodeId string
|
||||
SessionId string
|
||||
Conn *websocket.Conn
|
||||
lock sync.Mutex
|
||||
mt int
|
||||
closed bool
|
||||
}
|
||||
|
||||
func NewWsClient(nodeId string, sessionId string, conn *websocket.Conn) *WsClient {
|
||||
return &WsClient{
|
||||
NodeId: nodeId,
|
||||
SessionId: sessionId,
|
||||
Conn: conn,
|
||||
lock: sync.Mutex{},
|
||||
mt: 2, // fixed bug for 'Invalid UTF-8 in text frame'
|
||||
closed: false,
|
||||
}
|
||||
}
|
||||
|
||||
func (wc *WsClient) Send(message []byte) error {
|
||||
wc.lock.Lock()
|
||||
defer wc.lock.Unlock()
|
||||
|
||||
if wc.closed {
|
||||
return ErrConClosed
|
||||
}
|
||||
|
||||
return wc.Conn.WriteMessage(wc.mt, message)
|
||||
}
|
||||
|
||||
func (wc *WsClient) Receive() (int, []byte, error) {
|
||||
if wc.closed {
|
||||
return 0, nil, ErrConClosed
|
||||
}
|
||||
|
||||
return wc.Conn.ReadMessage()
|
||||
}
|
||||
|
||||
func (wc *WsClient) Close() {
|
||||
wc.lock.Lock()
|
||||
defer wc.lock.Unlock()
|
||||
|
||||
if wc.closed {
|
||||
return
|
||||
}
|
||||
|
||||
_ = wc.Conn.Close()
|
||||
wc.closed = true
|
||||
}
|
||||
137
server/server.go
Normal file
137
server/server.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-contrib/sessions/cookie"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net/http"
|
||||
logger2 "openai/logger"
|
||||
"openai/types"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var logger = logger2.GetLogger()
|
||||
|
||||
type StaticFile struct {
|
||||
embedFS embed.FS
|
||||
path string
|
||||
}
|
||||
|
||||
func (s StaticFile) Open(name string) (fs.File, error) {
|
||||
filename := filepath.Join(s.path, strings.TrimLeft(name, "/"))
|
||||
file, err := s.embedFS.Open(filename)
|
||||
return file, err
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
Config *types.Config
|
||||
}
|
||||
|
||||
func NewServer(config *types.Config) *Server {
|
||||
return &Server{Config: config}
|
||||
}
|
||||
|
||||
func (s *Server) Run(webRoot embed.FS, path string) {
|
||||
gin.SetMode(gin.DebugMode)
|
||||
engine := gin.Default()
|
||||
engine.Use(sessionMiddleware(s.Config))
|
||||
engine.Use(corsMiddleware())
|
||||
engine.Use(AuthorizeMiddleware())
|
||||
|
||||
engine.GET("/hello", Hello)
|
||||
engine.Any("/api/chat", s.Chat)
|
||||
|
||||
// process front-end web static files
|
||||
engine.StaticFS("/chat", http.FS(StaticFile{
|
||||
embedFS: webRoot,
|
||||
path: path,
|
||||
}))
|
||||
|
||||
logger.Infof("http://%s", s.Config.Listen)
|
||||
err := engine.Run(s.Config.Listen)
|
||||
|
||||
if err != nil {
|
||||
logger.Error("Fail to start server:", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func sessionMiddleware(config *types.Config) gin.HandlerFunc {
|
||||
// encrypt the cookie
|
||||
store := cookie.NewStore([]byte(config.Session.SecretKey))
|
||||
store.Options(sessions.Options{
|
||||
Path: config.Session.Path,
|
||||
Domain: config.Session.Domain,
|
||||
MaxAge: config.Session.MaxAge,
|
||||
Secure: config.Session.Secure,
|
||||
HttpOnly: config.Session.HttpOnly,
|
||||
SameSite: config.Session.SameSite,
|
||||
})
|
||||
return sessions.Sessions(config.Session.Name, store)
|
||||
}
|
||||
|
||||
// 跨域中间件设置
|
||||
func corsMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
method := c.Request.Method
|
||||
origin := c.Request.Header.Get("Origin")
|
||||
if origin != "" {
|
||||
// 设置允许的请求源
|
||||
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
|
||||
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
|
||||
//允许跨域设置可以返回其他子段,可以自定义字段
|
||||
c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, Content-Type, Session-Name, Session")
|
||||
// 允许浏览器(客户端)可以解析的头部 (重要)
|
||||
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers")
|
||||
//设置缓存时间
|
||||
c.Header("Access-Control-Max-Age", "172800")
|
||||
//允许客户端传递校验信息比如 cookie (重要)
|
||||
c.Header("Access-Control-Allow-Credentials", "true")
|
||||
}
|
||||
|
||||
if method == http.MethodOptions {
|
||||
c.JSON(http.StatusOK, "ok!")
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
log.Printf("Panic info is: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// AuthorizeMiddleware 用户授权验证
|
||||
func AuthorizeMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.Next()
|
||||
//if c.Request.URL.Path == "/login" {
|
||||
// c.Next()
|
||||
// return
|
||||
//}
|
||||
|
||||
//sessionName := c.GetHeader("Session-Name")
|
||||
//session, err := c.Cookie(sessionName)
|
||||
//if err == nil {
|
||||
// c.Request.Header.Set(utils.SessionKey, session)
|
||||
// c.Next()
|
||||
//} else {
|
||||
// logger.Fatal(err)
|
||||
// c.Abort()
|
||||
// c.JSON(http.StatusUnauthorized, "No session data found")
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
func Hello(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"code": 0, "message": fmt.Sprintf("HELLO, XWEBSSH !!!")})
|
||||
}
|
||||
Reference in New Issue
Block a user