mirror of
https://github.com/criyle/go-judge.git
synced 2025-09-26 22:39:12 +08:00
refactor(rest_executor): rename handlers and clean up routing logic (#148)
Some checks failed
Build / Goreleaser (push) Has been cancelled
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (amd64_v3, darwin) (push) Has been cancelled
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (amd64_v3, linux) (push) Has been cancelled
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (amd64_v3, windows) (push) Has been cancelled
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (arm64_v8.0, darwin) (push) Has been cancelled
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (arm64_v8.0, linux) (push) Has been cancelled
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (arm64_v8.0, windows) (push) Has been cancelled
Some checks failed
Build / Goreleaser (push) Has been cancelled
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (amd64_v3, darwin) (push) Has been cancelled
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (amd64_v3, linux) (push) Has been cancelled
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (amd64_v3, windows) (push) Has been cancelled
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (arm64_v8.0, darwin) (push) Has been cancelled
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (arm64_v8.0, linux) (push) Has been cancelled
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (arm64_v8.0, windows) (push) Has been cancelled
* refactor: move `Register` interface to a new `register` package * refactor(rest_executor): separate route registration logic - rename struct: `handle` -> `cmdHandle` - remove member variable `fileHandle` from `CmdHandle` - rename function: `New` -> `NewCmdHandle` - create function: `NewFileHandle` * refactor(rest_executor): move `register.go` to `rest_executor/`
This commit is contained in:
parent
f52326c3ce
commit
40e47b2bfa
@ -317,8 +317,10 @@ func initHTTPMux(conf *config.Config, work worker.Worker, fs filestore.FileStore
|
||||
}
|
||||
|
||||
// Rest Handle
|
||||
restHandle := restexecutor.New(work, fs, conf.SrcPrefix, logger)
|
||||
restHandle.Register(r)
|
||||
cmdHandle := restexecutor.NewCmdHandle(work, conf.SrcPrefix, logger)
|
||||
cmdHandle.Register(r)
|
||||
fileHandle := restexecutor.NewFileHandle(fs)
|
||||
fileHandle.Register(r)
|
||||
|
||||
// WebSocket Handle
|
||||
wsHandle := wsexecutor.New(work, conf.SrcPrefix, logger)
|
||||
|
@ -5,88 +5,72 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/criyle/go-judge/cmd/go-judge/model"
|
||||
"github.com/criyle/go-judge/filestore"
|
||||
"github.com/criyle/go-judge/worker"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Register registers executor the handler
|
||||
//
|
||||
// POST /run, GET /file, POST /file, GET /file/:fid, DELETE /file/:fid
|
||||
type Register interface {
|
||||
Register(*gin.Engine)
|
||||
}
|
||||
|
||||
// New creates new REST API handler
|
||||
func New(worker worker.Worker, fs filestore.FileStore, srcPrefix []string, logger *zap.Logger) Register {
|
||||
return &handle{
|
||||
worker: worker,
|
||||
fileHandle: fileHandle{fs: fs},
|
||||
srcPrefix: srcPrefix,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
type handle struct {
|
||||
worker worker.Worker
|
||||
fileHandle
|
||||
type cmdHandle struct {
|
||||
worker worker.Worker
|
||||
srcPrefix []string
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func (h *handle) Register(r *gin.Engine) {
|
||||
// Run handle
|
||||
r.POST("/run", h.handleRun)
|
||||
|
||||
// File handle
|
||||
r.GET("/file", h.fileGet)
|
||||
r.POST("/file", h.filePost)
|
||||
r.GET("/file/:fid", h.fileIDGet)
|
||||
r.DELETE("/file/:fid", h.fileIDDelete)
|
||||
// NewCmdHandle creates a new command handle
|
||||
func NewCmdHandle(worker worker.Worker, srcPrefix []string, logger *zap.Logger) Register {
|
||||
return &cmdHandle{
|
||||
worker: worker,
|
||||
srcPrefix: srcPrefix,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *handle) handleRun(c *gin.Context) {
|
||||
func (c *cmdHandle) Register(r *gin.Engine) {
|
||||
// Run handle
|
||||
r.POST("/run", c.handleRun)
|
||||
}
|
||||
|
||||
func (c *cmdHandle) handleRun(ctx *gin.Context) {
|
||||
var req model.Request
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Error(err)
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error())
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
ctx.Error(err)
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if len(req.Cmd) == 0 {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, "no cmd provided")
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, "no cmd provided")
|
||||
return
|
||||
}
|
||||
r, err := model.ConvertRequest(&req, h.srcPrefix)
|
||||
r, err := model.ConvertRequest(&req, c.srcPrefix)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error())
|
||||
ctx.Error(err)
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
h.logger.Sugar().Debugf("request: %+v", r)
|
||||
rtCh, _ := h.worker.Submit(c.Request.Context(), r)
|
||||
c.logger.Sugar().Debugf("request: %+v", r)
|
||||
rtCh, _ := c.worker.Submit(ctx.Request.Context(), r)
|
||||
rt := <-rtCh
|
||||
h.logger.Sugar().Debugf("response: %+v", rt)
|
||||
c.logger.Sugar().Debugf("response: %+v", rt)
|
||||
if rt.Error != nil {
|
||||
c.Error(rt.Error)
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, rt.Error.Error())
|
||||
ctx.Error(rt.Error)
|
||||
ctx.AbortWithStatusJSON(http.StatusInternalServerError, rt.Error.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// encode json directly to avoid allocation
|
||||
c.Status(http.StatusOK)
|
||||
c.Header("Content-Type", "application/json; charset=utf-8")
|
||||
ctx.Status(http.StatusOK)
|
||||
ctx.Header("Content-Type", "application/json; charset=utf-8")
|
||||
|
||||
res, err := model.ConvertResponse(rt, true)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, err.Error())
|
||||
ctx.Error(err)
|
||||
ctx.AbortWithStatusJSON(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
defer res.Close()
|
||||
|
||||
if err := json.NewEncoder(c.Writer).Encode(res.Results); err != nil {
|
||||
c.Error(err)
|
||||
if err := json.NewEncoder(ctx.Writer).Encode(res.Results); err != nil {
|
||||
ctx.Error(err)
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,21 @@ type fileHandle struct {
|
||||
fs filestore.FileStore
|
||||
}
|
||||
|
||||
// NewFileHandle creates a new file handle
|
||||
func NewFileHandle(fs filestore.FileStore) Register {
|
||||
return &fileHandle{
|
||||
fs: fs,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *fileHandle) Register(r *gin.Engine) {
|
||||
// File handle
|
||||
r.GET("/file", f.fileGet)
|
||||
r.POST("/file", f.filePost)
|
||||
r.GET("/file/:fid", f.fileIDGet)
|
||||
r.DELETE("/file/:fid", f.fileIDDelete)
|
||||
}
|
||||
|
||||
func (f *fileHandle) fileGet(c *gin.Context) {
|
||||
ids := f.fs.List()
|
||||
c.JSON(http.StatusOK, ids)
|
||||
|
8
cmd/go-judge/rest_executor/register.go
Normal file
8
cmd/go-judge/rest_executor/register.go
Normal file
@ -0,0 +1,8 @@
|
||||
package restexecutor
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
// Register registers executor the handler
|
||||
type Register interface {
|
||||
Register(*gin.Engine)
|
||||
}
|
Loading…
Reference in New Issue
Block a user