Skip to content
Snippets Groups Projects
Commit 89d30367 authored by Nils Petter Skålerud's avatar Nils Petter Skålerud
Browse files

Initial work on notification endpoint / webhooks.

Still not functionalt, just small tests.
Made a Postman collection of useful requests to run.

Signed-off-by: default avatarNils Petter Skålerud <np_skalerud@hotmail.com>
parent e23a7a8d
No related branches found
No related tags found
No related merge requests found
.vs .vs
.idea/
.idea
\ No newline at end of file
{
"info": {
"_postman_id": "c996617f-925e-4130-93bc-297a04916729",
"name": "Requests",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Webhook Registration 01",
"request": {
"method": "POST",
"header": [
{
"key": "content-type",
"value": "application/json",
"type": "default"
}
],
"body": {
"mode": "raw",
"raw": "{\r\n \"url\": \"https://localhost:8080/client/\",\r\n \"country\": \"NOR\",\r\n \"calls\": 5\r\n}\r\n",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:8080/energy/v1/notifications/",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"energy",
"v1",
"notifications",
""
]
}
},
"response": []
}
]
}
\ No newline at end of file
...@@ -8,24 +8,9 @@ import ( ...@@ -8,24 +8,9 @@ import (
"os" "os"
) )
/*
type ServerState struct {
startTime time.Time
_useMocking bool
}
func (state ServerState) UseMocking() bool {
return state._useMocking
}
func (state ServerState) UptimeInSeconds() float64 {
return time.Since(state.startTime).Seconds()
}
*/
func main() { func main() {
// serverState := ServerState{} // Currently unused serverState := utils.ServerState{}
// Extract PORT variable from the environment variables // Extract PORT variable from the environment variables
port := os.Getenv("PORT") port := os.Getenv("PORT")
...@@ -39,6 +24,13 @@ func main() { ...@@ -39,6 +24,13 @@ func main() {
http.HandleFunc(utils.DEFAULT_PATH, handlers.DefaultHandler) http.HandleFunc(utils.DEFAULT_PATH, handlers.DefaultHandler)
http.HandleFunc(utils.CURRENT_PATH, handlers.HandleGetRequestForCurrentPercentage) http.HandleFunc(utils.CURRENT_PATH, handlers.HandleGetRequestForCurrentPercentage)
// Add the
http.HandleFunc(
utils.NOTIFICATIONS_PATH,
func(w http.ResponseWriter, r *http.Request) {
handlers.WebhookRegistrationHandler(&serverState, w, r)
})
log.Println("Starting server on port " + port + " ...") log.Println("Starting server on port " + port + " ...")
log.Fatal(http.ListenAndServe(":"+port, nil)) log.Fatal(http.ListenAndServe(":"+port, nil))
} }
......
package handlers
import (
"Assignment02/utils"
"encoding/json"
"net/http"
)
type WebhookRegistrationSource struct {
Url string `json:"url"`
CountryCode string `json:"country"`
Calls int `json:"calls"`
}
type WebhookRegistrationOutput struct {
Webhook_id int `json:"webhook_id"`
}
func WebhookRegistrationHandler(serverState *utils.ServerState, w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
return
}
if r.Header.Get("content-type") != "application/json" {
return
}
var command WebhookRegistrationSource
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&command)
if err != nil {
return
}
registration := utils.WebhookRegistration{
Url: command.Url,
Event: command.CountryCode,
}
registrationId := serverState.InsertWebhook(registration)
output := WebhookRegistrationOutput{
Webhook_id: registrationId,
}
encoder := json.NewEncoder(w)
err = encoder.Encode(output)
if err != nil {
return
}
}
...@@ -4,4 +4,5 @@ package utils ...@@ -4,4 +4,5 @@ package utils
const DEFAULT_PATH = "/" const DEFAULT_PATH = "/"
const CURRENT_PATH = "/energy/v1/renewables/current/" const CURRENT_PATH = "/energy/v1/renewables/current/"
const HISTORY_PATH = "/energy/v1/renewables/history/" const HISTORY_PATH = "/energy/v1/renewables/history/"
const NOTIFICATIONS_PATH = "/energy/v1/notifications/"
const STATUS_PATH = "/energy/v1/status/" const STATUS_PATH = "/energy/v1/status/"
package utils package utils
import "time"
type ServerState struct {
startTime time.Time
_useMocking bool
webhook_id_tracker int
webHooks []WebhookRegistration
}
func (state ServerState) UseMocking() bool {
return state._useMocking
}
func (state ServerState) UptimeInSeconds() float64 {
return time.Since(state.startTime).Seconds()
}
func (state *ServerState) InsertWebhook(registration WebhookRegistration) int {
state.webHooks = append(state.webHooks, registration)
output := state.webhook_id_tracker
state.webhook_id_tracker++
return output
}
type CountryRenewableOutput struct { type CountryRenewableOutput struct {
CountryName string `json:"name"` CountryName string `json:"name"`
IsoCode string `json:"isoCode"` IsoCode string `json:"isoCode"`
...@@ -18,3 +43,8 @@ type CountryItem struct { ...@@ -18,3 +43,8 @@ type CountryItem struct {
Cca2 string `json:"cca2"` Cca2 string `json:"cca2"`
MapsInternal map[string]string `json:"maps"` MapsInternal map[string]string `json:"maps"`
} }
type WebhookRegistration struct {
Url string `json:"url"`
Event string `json:"event"`
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment