Split backend service into checker and generator

Both are linked through a unix socket.
This commit is contained in:
nemunaire 2023-07-10 09:17:02 +02:00
commit ed091e761c
34 changed files with 660 additions and 208 deletions

View file

@ -1,13 +1,16 @@
package api
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"path"
"strconv"
"strings"
"time"
"srs.epita.fr/fic-server/libfic"
@ -15,6 +18,8 @@ import (
"github.com/gin-gonic/gin"
)
var GeneratorSocket string
func declareClaimsRoutes(router *gin.RouterGroup) {
// Tasks
router.GET("/claims", getClaims)
@ -287,12 +292,44 @@ func clearClaims(c *gin.Context) {
}
func generateTeamIssuesFile(team fic.Team) error {
if my, err := team.MyIssueFile(); err != nil {
return fmt.Errorf("Unable to generate issue FILE (tid=%d): %w", team.Id, err)
} else if j, err := json.Marshal(my); err != nil {
return fmt.Errorf("Unable to encode issues' file JSON: %w", err)
} else if err = ioutil.WriteFile(path.Join(TeamsDir, fmt.Sprintf("%d", team.Id), "issues.json"), j, 0644); err != nil {
return fmt.Errorf("Unable to write issues' file: %w", err)
if GeneratorSocket == "" {
if my, err := team.MyIssueFile(); err != nil {
return fmt.Errorf("Unable to generate issue FILE (tid=%d): %w", team.Id, err)
} else if j, err := json.Marshal(my); err != nil {
return fmt.Errorf("Unable to encode issues' file JSON: %w", err)
} else if err = ioutil.WriteFile(path.Join(TeamsDir, fmt.Sprintf("%d", team.Id), "issues.json"), j, 0644); err != nil {
return fmt.Errorf("Unable to write issues' file: %w", err)
}
} else {
buf, err := json.Marshal(fic.GenStruct{Type: fic.GenTeamIssues, TeamId: team.Id})
if err != nil {
return fmt.Errorf("Something is wrong with JSON encoder: %w", err)
}
sockType := "unix"
if strings.Contains(GeneratorSocket, ":") {
sockType = "tcp"
}
socket, err := net.Dial(sockType, GeneratorSocket)
if err != nil {
return err
}
defer socket.Close()
httpClient := &http.Client{
Transport: &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
return socket, nil
},
},
}
resp, err := httpClient.Post("http://localhost/enqueue", "application/json", bytes.NewReader(buf))
if err != nil {
return fmt.Errorf("Unable to enqueue new generation event: %w", err)
}
resp.Body.Close()
}
return nil
}