Skip to content

Commit

Permalink
Develop (#16)
Browse files Browse the repository at this point in the history
* Squashed commit of the following:

commit 4c3be07
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Mon Aug 3 11:57:25 2020 +0530

    Bump elliptic from 6.5.2 to 6.5.3 in /client (#6)

    Bumps [elliptic](https://github.com/indutny/elliptic) from 6.5.2 to 6.5.3.
    - [Release notes](https://github.com/indutny/elliptic/releases)
    - [Commits](indutny/elliptic@v6.5.2...v6.5.3)

    Signed-off-by: dependabot[bot] <[email protected]>

    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

commit 3e75da1
Author: sn123 <[email protected]>
Date:   Thu Jul 30 14:56:42 2020 +0530

    Update issue templates

commit 433830e
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Mon Jul 20 10:29:57 2020 +0530

    Bump lodash from 4.17.15 to 4.17.19 in /client (#5)

    Bumps [lodash](https://github.com/lodash/lodash) from 4.17.15 to 4.17.19.
    - [Release notes](https://github.com/lodash/lodash/releases)
    - [Commits](lodash/lodash@4.17.15...4.17.19)

    Signed-off-by: dependabot[bot] <[email protected]>

    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

commit 202271c
Merge: 8428d98 72c3cbe
Author: sn123 <[email protected]>
Date:   Fri Jun 12 11:45:49 2020 +0530

    Merge pull request #4 from goavega-software/develop

    Change log.Fatal to Println

commit 8428d98
Merge: 4cfaeb0 1297b79
Author: sn123 <[email protected]>
Date:   Thu Jun 11 15:21:22 2020 +0530

    Merge pull request #3 from goavega-software/dependabot/npm_and_yarn/client/websocket-extensions-0.1.4

    Bump websocket-extensions from 0.1.3 to 0.1.4 in /client

commit 1297b79
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Mon Jun 8 02:20:08 2020 +0000

    Bump websocket-extensions from 0.1.3 to 0.1.4 in /client

    Bumps [websocket-extensions](https://github.com/faye/websocket-extensions-node) from 0.1.3 to 0.1.4.
    - [Release notes](https://github.com/faye/websocket-extensions-node/releases)
    - [Changelog](https://github.com/faye/websocket-extensions-node/blob/master/CHANGELOG.md)
    - [Commits](faye/websocket-extensions-node@0.1.3...0.1.4)

    Signed-off-by: dependabot[bot] <[email protected]>

commit 4cfaeb0
Merge: 92a58b2 8cb8fa1
Author: sn123 <[email protected]>
Date:   Fri May 29 00:09:41 2020 +0530

    Merge pull request #2 from goavega-software/develop

    Add docker-compose support. Fixes #1

* Configurable jobs (#7)

* Change class name

* add youTube widget

* Dynamic layouting and state support (#11)

* Fix lint errors

* [Fix] Jobs don't fire on cron

* [add] multiple dashboard support

* Update README.md

* [Add] webhook
  • Loading branch information
sn123 authored Oct 23, 2020
1 parent 3b7915d commit dde3096
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ AWS_S3_BUCKET=AWS_S3_BUCKET
AWS_S3_PREFIX=AWS_S3_FILTER_PREFIX
KAFKA_URL=kafka-broker-url
KAFKA_TOPIC=topic-to-listen
JOB_SCHEDULE=[{"name": "flickrshow", "schedule": "@every 1d"}, {"name": "weather", "schedule": "@every 1h"}, ...]
JOB_SCHEDULE=[{"name": "flickrshow", "schedule": "@every 1d"}, {"name": "weather", "schedule": "@every 1h"}, ...]
API_KEY=<your-key-here>
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ Few widgets and jobs are already included in repository - feel free to use/abuse
* Weather widget - displays weather from OpenWeather and gets the backrground image from flickr for the weather condition
* List widget - cycles through a list of items (title, image and description) on schedule
#### Jobs
Jobs schedule is set using env variable ```JOB_SCHEDULE```. The variable holds an JSON array in below form:
Jobs schedule is set in ```config.json``` file in /config folder.:
```json
{"jobs": [{"name": "name-of-job", "schedule": "schedule-of-job"}, {"name": "weather", "schedule": "@every 1h"}]}
```
[{"name": "name-of-job", "schedule": "schedule-of-job"}, {"name": "weather", "schedule": "@every 1h"}]
```
This allows all jobs to have their job schedule configurable using env. Any job which is not present in JOB_SCHEDULE is disabled. (since 0.3)
This allows all jobs to have their job schedule configurable. Any job which is not present in config is disabled. (since 0.3)
* Blogroll - gets RSS feed from a site and converts the URL into a QR Code.
* Weather - gets the current weather from OpenWeather
Expand All @@ -70,7 +70,15 @@ This allows all jobs to have their job schedule configurable using env. Any job
data := mustardcore.EventData{Event: "clockWidget", Data: number{Trivia: string(text)}}
mustardcore.GetEventsManager().Notify(data)
```
### Pushing Data to Widgets
Mustard supports POST data to ```/api/webhook``` as a passthrough, the post body should be of JSON form ```{"event": "string", "data": object }```. This requires passing the ```API_KEY``` defined in env as base64 encoded value as ```X_API_KEY``` header.
```sh
$ curl --location --request POST '<mustard-url>/api/webhook' \
--header 'Content-Type: application/json' \
--header 'X_API_KEY: <base64encoded_api_key>' \
--data-raw '{"event": "slideshow", "data": ...}'
```
### Dashboard layout
Instead of hard-wiring the layout in vue file, the layout is retrieved from ```layout``` API. The API should return layout JSON data array in this form:
```typescript
Expand Down Expand Up @@ -161,8 +169,6 @@ $ docker run -p <local>:80 --env-file ./.env goavega/mustard
### TODO
- Create wiki
- Drag and drop support
- Better error handling - currently a job crash causes app to panic
- Support for multiple dashboards
- Data persistence
- Shared Chart components
Expand Down
5 changes: 1 addition & 4 deletions core/jobfactory.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package core

import (
"encoding/json"
"log"
)

Expand All @@ -19,9 +18,7 @@ var advertisedJobs map[string]func() = make(map[string]func())
/*
Process parses the json and only schedules enabled jobs
*/
func (jf JobFactory) Process(schedule string) {
schedules := make([]Schedule, 0)
json.Unmarshal([]byte(schedule), &schedules)
func (jf JobFactory) Process(schedules []Schedule) {
for _, item := range schedules {
value, ok := advertisedJobs[item.Name]
if ok {
Expand Down
7 changes: 7 additions & 0 deletions core/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ func GetEventsManager() EventsManager {
return eventsManager
}

/*
AnyClientConnected returns true if any SSE client is connected
*/
func (ev EventsManager) AnyClientConnected() bool {
return server.ClientCount() > 0
}

/*
Destroy is supposed to clean up
*/
Expand Down
23 changes: 19 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
b64 "encoding/base64"
"encoding/json"
"flag"
"fmt"
Expand Down Expand Up @@ -35,7 +36,8 @@ type dashboard struct {
}

type configuration struct {
Dashboards []dashboard `json:"dashboards"`
Dashboards []dashboard `json:"dashboards"`
Jobs []mustardcore.Schedule `json:"jobs"`
}

func parseArgs() (bool, string) {
Expand Down Expand Up @@ -83,33 +85,46 @@ func main() {
eventsManager.Init(e)
eventListener := events.EventListener{URL: os.Getenv("KAFKA_URL"), Topic: os.Getenv("KAFKA_TOPIC"), GroupID: "mustard"}
eventListener.Start()
mustardcore.GetFactory().Process(os.Getenv("JOB_SCHEDULE"))
// read config to get widgets
config := readConfig()
var result configuration
json.Unmarshal([]byte(config), &result)
mustardcore.GetFactory().Process(result.Jobs)

defer eventsManager.Destroy()
defer mustardcore.DestroyJobs()

e.POST("/api/nudge", func(c echo.Context) error {
mustardcore.FireImmediately()
return c.HTML(http.StatusOK, "Hello mustard")
return c.String(http.StatusOK, "Hello mustard")
})

e.POST("/api/layout", func(c echo.Context) (err error) {
request := new(requestPayload)
if err = c.Bind(request); err != nil {
return
}
log.Println(request.Path)
for _, f := range result.Dashboards {
if f.Id == request.Path {
return c.JSON(http.StatusOK, result.Dashboards[0].Widgets)
}
}
return
})

e.POST("/api/webhook", func(c echo.Context) (err error) {
apiKey := os.Getenv("API_KEY")
headerKey := c.Request().Header.Get("X_API_KEY")
if apiKey == "" || headerKey != b64.StdEncoding.EncodeToString([]byte(apiKey)) {
return c.String(http.StatusUnauthorized, "Invalid key")
}
eventMessage := new(mustardcore.EventData)
if err = c.Bind(eventMessage); err != nil || eventMessage.Data == nil {
return c.String(http.StatusUnprocessableEntity, "Invalid request body")
}
mustardcore.GetEventsManager().Notify(*eventMessage)
return c.String(http.StatusAccepted, "Ok")
})
port := os.Getenv("PORT")
if port == "" {
port = "8090"
Expand Down

0 comments on commit dde3096

Please sign in to comment.