示例#1
0
/**
 * Retrieves all clients
 * @return json
 */
func (c *ConfigController) GetClients(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	clients := models.GetAllClients()

	// Write content-type, statuscode, payload
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(200)

	fmt.Fprintf(w, "%s", clients)
}
示例#2
0
func ServHttp(confChan chan client.Config, reg chan client.Client, rm chan string) {
	// init router
	router := httprouter.New()

	register := func(c client.Client) {
		reg <- c
	}

	delete := func(id string) {
		rm <- id
	}

	// Init controller
	controller := controllers.NewConfigController(*ConFile, register, delete)

	// Init bootstrap
	clients := client.GetAllClients()
	var conf client.Config
	json.Unmarshal(clients, &conf)
	confChan <- conf
	close(confChan)

	// Get clients list
	router.GET("/clients", controller.GetClients)

	// Add a new client
	router.POST("/clients/add", controller.AddClient)

	// Remove a client
	router.DELETE("/clients/delete/:id", controller.RemoveClient)

	log.Fatal(
		http.ListenAndServe(
			strings.Join([]string{"", *HttpPort}, ":"),
			router,
		),
	)

}