From 50f547a6e7b58bc50195afbe3bc1cdbed38474bd Mon Sep 17 00:00:00 2001 From: Anthony Lapenna Date: Sun, 2 Jun 2019 18:16:43 +1200 Subject: [PATCH] feat(motd): add the ability to use custom style (#2918) * feat(motd): rework motd display mechanism for more flexibility on motd content * feat(api): enhance MOTD * refactor(api): refactor MOTD related codebase * feat(motd): hash on message --- api/http/handler/motd/motd.go | 35 +++++++++++++++---- api/portainer.go | 4 +-- .../components/motd-panel/motd-panel.js | 8 +++++ .../components/motd-panel/motdPanel.html | 26 ++++++++++++++ app/portainer/models/motd.js | 2 ++ app/portainer/views/home/home.html | 9 ++--- 6 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 app/portainer/components/motd-panel/motd-panel.js create mode 100644 app/portainer/components/motd-panel/motdPanel.html diff --git a/api/http/handler/motd/motd.go b/api/http/handler/motd/motd.go index 253be31cc..ba768becb 100644 --- a/api/http/handler/motd/motd.go +++ b/api/http/handler/motd/motd.go @@ -1,7 +1,9 @@ package motd import ( + "encoding/json" "net/http" + "strings" "github.com/portainer/libhttp/response" "github.com/portainer/portainer/api" @@ -10,25 +12,44 @@ import ( ) type motdResponse struct { - Title string `json:"Title"` - Message string `json:"Message"` - Hash []byte `json:"Hash"` + Title string `json:"Title"` + Message string `json:"Message"` + ContentLayout map[string]string `json:"ContentLayout"` + Style string `json:"Style"` + Hash []byte `json:"Hash"` +} + +type motdData struct { + Title string `json:"title"` + Message []string `json:"message"` + ContentLayout map[string]string `json:"contentLayout"` + Style string `json:"style"` } func (handler *Handler) motd(w http.ResponseWriter, r *http.Request) { - motd, err := client.Get(portainer.MessageOfTheDayURL, 0) if err != nil { response.JSON(w, &motdResponse{Message: ""}) return } - title, err := client.Get(portainer.MessageOfTheDayTitleURL, 0) + var data motdData + err = json.Unmarshal(motd, &data) if err != nil { response.JSON(w, &motdResponse{Message: ""}) return } - hash := crypto.HashFromBytes(motd) - response.JSON(w, &motdResponse{Title: string(title), Message: string(motd), Hash: hash}) + message := strings.Join(data.Message, "\n") + + hash := crypto.HashFromBytes([]byte(message)) + resp := motdResponse{ + Title: data.Title, + Message: message, + Hash: hash, + ContentLayout: data.ContentLayout, + Style: data.Style, + } + + response.JSON(w, &resp) } diff --git a/api/portainer.go b/api/portainer.go index ae13fdedf..bc50a5b50 100644 --- a/api/portainer.go +++ b/api/portainer.go @@ -860,9 +860,7 @@ const ( // AssetsServerURL represents the URL of the Portainer asset server AssetsServerURL = "https://portainer-io-assets.sfo2.digitaloceanspaces.com" // MessageOfTheDayURL represents the URL where Portainer MOTD message can be retrieved - MessageOfTheDayURL = AssetsServerURL + "/motd.html" - // MessageOfTheDayTitleURL represents the URL where Portainer MOTD title can be retrieved - MessageOfTheDayTitleURL = AssetsServerURL + "/motd-title.txt" + MessageOfTheDayURL = AssetsServerURL + "/motd.json" // ExtensionDefinitionsURL represents the URL where Portainer extension definitions can be retrieved ExtensionDefinitionsURL = AssetsServerURL + "/extensions-1.20.3.json" // PortainerAgentHeader represents the name of the header available in any agent response diff --git a/app/portainer/components/motd-panel/motd-panel.js b/app/portainer/components/motd-panel/motd-panel.js new file mode 100644 index 000000000..4548beefd --- /dev/null +++ b/app/portainer/components/motd-panel/motd-panel.js @@ -0,0 +1,8 @@ +angular.module('portainer.app').component('motdPanel', { + templateUrl: './motdPanel.html', + bindings: { + motd: '<', + dismissAction: '&?' + }, + transclude: true +}); diff --git a/app/portainer/components/motd-panel/motdPanel.html b/app/portainer/components/motd-panel/motdPanel.html new file mode 100644 index 000000000..49727805f --- /dev/null +++ b/app/portainer/components/motd-panel/motdPanel.html @@ -0,0 +1,26 @@ +
+
+ + + +
+
+ + {{ $ctrl.motd.Title }} + + + dismiss + +
+
+ +

+
+
+
+
+
+
+
\ No newline at end of file diff --git a/app/portainer/models/motd.js b/app/portainer/models/motd.js index 1e6ac1b1f..872b2dfe8 100644 --- a/app/portainer/models/motd.js +++ b/app/portainer/models/motd.js @@ -2,4 +2,6 @@ export function MotdViewModel(data) { this.Title = data.Title; this.Message = data.Message; this.Hash = data.Hash; + this.Style = data.Style; + this.ContentLayout = data.ContentLayout; } diff --git a/app/portainer/views/home/home.html b/app/portainer/views/home/home.html index 06eecd3e4..2fb8a4afc 100644 --- a/app/portainer/views/home/home.html +++ b/app/portainer/views/home/home.html @@ -7,14 +7,11 @@ Endpoints - - -

-
-
+