Skip to content

Commit

Permalink
Perf code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
slhmy committed Sep 13, 2023
1 parent 6d380d7 commit d132b9f
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 54 deletions.
54 changes: 54 additions & 0 deletions packages/agent/judger/judge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package judger

import (
"bytes"
"encoding/json"
"net/http"
"net/url"

"github.com/OJ-lab/oj-lab-services/packages/application"
)

const JUDGER_HOST_PROP = "judger.host"
const JUDGER_JUDGE_PATH = "/api/v1/judge"

var judgerHost string

func init() {
judgerHost = application.AppConfig.GetString(JUDGER_HOST_PROP)
}

type JudgeRequest struct {
Src string `json:"src"`
SrcLanguage string `json:"src_language"`
}

func PostJudgeSync(packageSlug string, judgeRequest JudgeRequest) ([]map[string]interface{}, error) {
url, err := url.JoinPath(judgerHost, JUDGER_JUDGE_PATH, packageSlug)
if err != nil {
return nil, err
}
payloadBytes, err := json.Marshal(judgeRequest)
if err != nil {
return nil, err
}
client := &http.Client{}
innerRequest, err := http.NewRequest("POST", url, bytes.NewReader(payloadBytes))
if err != nil {
return nil, err
}
innerRequest.Header.Set("Content-Type", "application/json")
innerRequest.Header.Set("Accept", "application/json")
innerResponse, err := client.Do(innerRequest)
if err != nil {
return nil, err
}
defer innerResponse.Body.Close()

innerBody := []map[string]interface{}{}
if err := json.NewDecoder(innerResponse.Body).Decode(&innerBody); err != nil {
return nil, err
}

return innerBody, nil
}
6 changes: 3 additions & 3 deletions service/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"github.com/OJ-lab/oj-lab-services/packages/application"
"github.com/OJ-lab/oj-lab-services/service/router"
"github.com/OJ-lab/oj-lab-services/service/handler"
"github.com/gin-gonic/gin"
)

Expand All @@ -25,8 +25,8 @@ func main() {
r := gin.Default()
r.Use(application.HandleError)
gin.SetMode(serviceMode)
router.SetupUserRouter(r)
router.SetupProblemRoute(r)
handler.SetupUserRouter(r)
handler.SetupProblemRoute(r)

err := r.Run(servicePort)
if err != nil {
Expand Down
23 changes: 23 additions & 0 deletions service/handler/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package handler

import (
"github.com/OJ-lab/oj-lab-services/packages/mapper"
"github.com/gin-gonic/gin"
)

func GetProblemInfo(ctx *gin.Context) {
slug := ctx.Param("slug")

problem, err := mapper.GetProblem(slug)
if err != nil {
ctx.Error(err)
return
}

ctx.JSON(200, gin.H{
"slug": problem.Slug,
"title": problem.Title,
"description": problem.Description,
"tags": mapper.GetTagsList(problem),
})
}
4 changes: 2 additions & 2 deletions service/router/setup.go → service/handler/routes.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package router
package handler

import (
"net/http"
Expand All @@ -8,7 +8,7 @@ import (
)

func SetupUserRouter(r *gin.Engine) {
g := r.Group("/api/user")
g := r.Group("/api/v1/user")
{
g.GET("/health", func(c *gin.Context) {
c.String(http.StatusOK, "Hello, this is user service")
Expand Down
54 changes: 5 additions & 49 deletions service/problem/judge.go
Original file line number Diff line number Diff line change
@@ -1,68 +1,24 @@
package problem

import (
"bytes"
"encoding/json"
"net/http"
"net/url"

"github.com/OJ-lab/oj-lab-services/packages/application"
"github.com/gin-gonic/gin"
)

const JUDGER_HOST_PROP = "judger.host"
const JUDGER_JUDGE_PATH = "/api/v1/judge"

var judgerHost string

func init() {
judgerHost = application.AppConfig.GetString(JUDGER_HOST_PROP)
}

type JudgeRequest struct {
Src string `json:"src"`
SrcLanguage string `json:"src_language"`
}
"github.com/OJ-lab/oj-lab-services/packages/agent/judger"
)

func Judge(ctx *gin.Context) {
packageSlug := ctx.Param("slug")

judgeRequest := JudgeRequest{}
judgeRequest := judger.JudgeRequest{}
if err := ctx.ShouldBindJSON(&judgeRequest); err != nil {
ctx.Error(err)
return
}

url, err := url.JoinPath(judgerHost, JUDGER_JUDGE_PATH, packageSlug)
if err != nil {
ctx.Error(err)
return
}
payloadBytes, err := json.Marshal(judgeRequest)
body, err := judger.PostJudgeSync(packageSlug, judgeRequest)
if err != nil {
ctx.Error(err)
return
}
client := &http.Client{}
innerRequest, err := http.NewRequest("POST", url, bytes.NewReader(payloadBytes))
if err != nil {
ctx.Error(err)
return
}
innerRequest.Header.Set("Content-Type", "application/json")
innerRequest.Header.Set("Accept", "application/json")
innerResponse, err := client.Do(innerRequest)
if err != nil {
ctx.Error(err)
return
}
defer innerResponse.Body.Close()

innerBody := []map[string]interface{}{}
if err := json.NewDecoder(innerResponse.Body).Decode(&innerBody); err != nil {
ctx.Error(err)
return
}

ctx.JSON(innerResponse.StatusCode, innerBody)
ctx.JSON(200, body)
}

0 comments on commit d132b9f

Please sign in to comment.