Add http endpoints

This commit is contained in:
Barak Michener 2020-12-04 18:06:24 -08:00
parent 3b05f4316c
commit 56116aea2a
7 changed files with 136 additions and 1 deletions

58
http.go Normal file
View file

@ -0,0 +1,58 @@
package main
import (
"context"
"net/http"
"github.com/barakmich/tinkerbell/ray_rpc"
"github.com/gin-gonic/gin"
)
func runHttp(addr, rootPath string, raylet *Raylet) error {
r := gin.Default()
h := rayletHandler{raylet}
r.Static("/", rootPath)
api := r.Group("/api")
api.POST("/get", h.GetObject)
api.POST("/put", h.PutObject)
api.POST("/schedule", h.Schedule)
err := r.Run(addr)
return err
}
type rayletHandler struct {
raylet *Raylet
}
func (h *rayletHandler) GetObject(c *gin.Context) {
var req ray_rpc.GetRequest
c.BindJSON(&req)
res, err := h.raylet.GetObject(context.TODO(), &req)
if err != nil {
c.JSON(500, gin.H{"error": err})
return
}
c.JSON(http.StatusOK, res)
}
func (h *rayletHandler) PutObject(c *gin.Context) {
var req ray_rpc.PutRequest
c.BindJSON(&req)
res, err := h.raylet.PutObject(context.TODO(), &req)
if err != nil {
c.JSON(500, gin.H{"error": err})
return
}
c.JSON(http.StatusOK, res)
}
func (h *rayletHandler) Schedule(c *gin.Context) {
var req ray_rpc.ClientTask
c.BindJSON(&req)
res, err := h.raylet.Schedule(context.TODO(), &req)
if err != nil {
c.JSON(500, gin.H{"error": err})
return
}
c.JSON(http.StatusOK, res)
}