generator: Can perform synchronous generation
This commit is contained in:
parent
ec98e521dc
commit
1769938205
13 changed files with 214 additions and 81 deletions
60
admin/generation/generation.go
Normal file
60
admin/generation/generation.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package generation
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
)
|
||||
|
||||
var GeneratorSocket string
|
||||
|
||||
func doGeneration(uri string, contenttype string, buf io.Reader) (*http.Response, error) {
|
||||
sockType := "unix"
|
||||
if strings.Contains(GeneratorSocket, ":") {
|
||||
sockType = "tcp"
|
||||
}
|
||||
|
||||
socket, err := net.Dial(sockType, GeneratorSocket)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer socket.Close()
|
||||
|
||||
httpClient := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Dial: func(network, addr string) (net.Conn, error) {
|
||||
return socket, nil
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return httpClient.Post("http://localhost"+uri, contenttype, buf)
|
||||
}
|
||||
|
||||
func EnqueueGeneration(gs fic.GenStruct) (*http.Response, error) {
|
||||
buf, err := json.Marshal(gs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Something is wrong with JSON encoder: %w", err)
|
||||
}
|
||||
|
||||
return doGeneration("/enqueue", "application/json", bytes.NewReader(buf))
|
||||
}
|
||||
|
||||
func PerformGeneration(gs fic.GenStruct) (*http.Response, error) {
|
||||
buf, err := json.Marshal(gs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Something is wrong with JSON encoder: %w", err)
|
||||
}
|
||||
|
||||
return doGeneration("/perform", "application/json", bytes.NewReader(buf))
|
||||
}
|
||||
|
||||
func FullGeneration() (*http.Response, error) {
|
||||
return doGeneration("/full", "application/json", nil)
|
||||
}
|
||||
Reference in a new issue