sermoni

"Service monitor" / cronjob status service
Log | Files | Refs

commit 8395f865cd4f0f1ac77aa6e15fef8f74b3b5f0d8
parent d36567cdb98f151b0aa292d6e7985085f0dd994f
Author: Vetle Haflan <vetle@haflan.dev>
Date:   Tue, 14 Apr 2020 01:04:42 +0200

Almost finish service management

Deletion remaining, and obviously CSS

Diffstat:
Minternal/events/events_test.go | 3++-
Minternal/http/services.go | 9++++++++-
Minternal/services/services.go | 6+++---
Minternal/services/services_test.go | 13+++++++------
Mui/src/Services.vue | 43++++++++++++++++++++++++++++++++++++++++---
Mui/src/requests.js | 29+++++++++--------------------
6 files changed, 69 insertions(+), 34 deletions(-)

diff --git a/internal/events/events_test.go b/internal/events/events_test.go @@ -87,7 +87,8 @@ func TestMain(m *testing.M) { os.Remove(testDB) database.Open(testDB) config.InitConfig() - err := services.Add(serviceToken, &services.Service{ + err := services.Add(&services.Service{ + Token: serviceToken, Name: "test @ dev-laptop", Description: "Service used for testing only", }) diff --git a/internal/http/services.go b/internal/http/services.go @@ -2,6 +2,7 @@ package http import ( "encoding/json" + "io/ioutil" "net/http" "sermoni/internal/services" "strconv" @@ -16,7 +17,13 @@ func getServices(w http.ResponseWriter, r *http.Request) { w.Write(data) } func postService(w http.ResponseWriter, r *http.Request) { - + content, err := ioutil.ReadAll(r.Body) + check(err) + service := new(services.Service) + // TODO: Handle json parse error + err = json.Unmarshal(content, service) + check(err) + services.Add(service) } func deleteService(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) diff --git a/internal/services/services.go b/internal/services/services.go @@ -109,7 +109,7 @@ func Delete(intID uint64) error { // Add adds a new service to monitor // Returns error if the token is unavailable and if the transaction fails in any way -func Add(token string, service *Service) error { +func Add(service *Service) error { db := database.GetDB() return db.Update(func(tx *bbolt.Tx) error { var err error @@ -127,7 +127,7 @@ func Add(token string, service *Service) error { } // Check if the service token is available, return error otherwise - serviceToken := []byte(token) + serviceToken := []byte(service.Token) if serviceID = stb.Get(serviceToken); serviceID != nil { return errors.New("a service has already been registered for the given token") } @@ -152,7 +152,7 @@ func Add(token string, service *Service) error { } // Put an entry in the service-tokens bucket to map the token to the service - return stb.Put([]byte(token), serviceID) + return stb.Put([]byte(service.Token), serviceID) }) } diff --git a/internal/services/services_test.go b/internal/services/services_test.go @@ -41,28 +41,29 @@ var ( var testServices = []*Service{ { + Token: token1, Name: "tester @ dev-computer", Description: "This describes the service in more detail", ExpectationPeriod: 282342, }, - {Name: "tester2", ExpectationPeriod: 300003}, - {Name: "third @ tester"}, + {Token: token2, Name: "tester2", ExpectationPeriod: 300003}, + {Token: token3, Name: "third @ tester"}, } func TestAddService(t *testing.T) { - err := Add(token1, testServices[0]) + err := Add(testServices[0]) if err != nil { fmt.Println(err) t.Fatal("unexpected error when adding service") } - if err = Add(token2, testServices[1]); err != nil { + if err = Add(testServices[1]); err != nil { fmt.Println(err) t.Fatal("unexpected error when adding second service") } - if err = Add(token2, testServices[1]); err == nil { + if err = Add(testServices[1]); err == nil { t.Fatal("no error returned when trying to re-use a service token") } - err = Add(token3, testServices[2]) + err = Add(testServices[2]) if err != nil { t.Fatal("unexpected error when adding third service") } diff --git a/ui/src/Services.vue b/ui/src/Services.vue @@ -1,10 +1,24 @@ <template> <div class="services-wrapper"> <div v-for="service in services"> - {{ service.name }} : - <input type="text" service.description }} : + {{ service.id }} : <input type="text" :value="service.token"> + <input type="text" :value="service.name"> + <input type="text" :value="service.description"> + <input type="text" :value="service.period"> + <input type="text" :value="service.maxevents"> </div> + + <input type="text" v-model="newService.token" placeholder="Token"> + <input type="text" v-model="newService.name" placeholder="Name"> + <input type="text" v-model="newService.description" + placeholder="Description"> + <input type="text" v-model="newService.period" + placeholder="Expectation Period"> + <input type="text" v-model="newService.maxevents" + placeholder="Max number of events"> + + <button @click="addService">Add service</button> </div> </template> @@ -14,7 +28,30 @@ name: "Services", data() { return { - services: [] + services: [], + newService: { + token: "", + name: "", + description: "", + period: 0, + maxevents: 0 + } + } + }, + methods: { + addService() { + api.postService(this.newService, + success => { + this.services.push(this.newService); + this.newService = { + token: "", name: "", description: "", + period: 0, maxevents: 0 + }; + }, + error => { + this.$emit("error"); + } + ); } }, mounted() { diff --git a/ui/src/requests.js b/ui/src/requests.js @@ -57,8 +57,15 @@ function getServices(successHandler, errorHandler) { }); } -function postService(service) { - +// TODO: Expected status 201 (and set 201 server-side) +function postService(service, successHandler, errorHandler) { + request({ + url: "/services", + method: "POST", + data: service, + success: successHandler, + error: errorHandler + }); } function deleteService(id) { @@ -90,24 +97,6 @@ function deleteEvent(id, successHandler, errorHandler) { /***** Request utils *****/ -/** - * Create URL parameters from a JSON object - */ -function params(parameterObject) { - const parameters = new URLSearchParams(); - for (let key in parameterObject) { - let value = parameterObject[key]; - if (Array.isArray(value)) { - for (let i = 0; i < value.length; i++) { - parameters.append(key, value[i]); - } - } else { - parameters.set(key, value); - } - } - return parameters.toString(); -} - // Send a request to server. Takes request description, jsonRequest, // on the following format // {