Skip to content

Instantly share code, notes, and snippets.

@thewraven
Created December 5, 2017 17:13
Show Gist options
  • Save thewraven/75d229422397d4ac50626f7f8b77e561 to your computer and use it in GitHub Desktop.
Save thewraven/75d229422397d4ac50626f7f8b77e561 to your computer and use it in GitHub Desktop.
package agent
import (
"moon"
"net/http"
"github.com/gin-gonic/gin"
)
type Worker struct {
manager moon.Manager
platform moon.Platform
}
func NewWorker(mgr moon.Manager, p moon.Platform) Worker {
return Worker{
manager: mgr,
platform: p,
}
}
func (w Worker) UnwrapHandler() http.Handler {
router := gin.Default()
router.GET("/status", w.GetStatus)
router.POST("/update", w.UpdateConfig)
return router
}
func (w Worker) GetStatus(c *gin.Context) {
status, err := w.manager.GetStatus()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, status)
}
func (w Worker) UpdateConfig(c *gin.Context) {
var configs []moon.Configuration
err := c.BindJSON(&configs)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
err = w.manager.ApplyConfig(configs)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.Status(http.StatusOK)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment