Created
December 5, 2017 17:13
-
-
Save thewraven/75d229422397d4ac50626f7f8b77e561 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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