2018-08-21 18:40:42 +00:00
|
|
|
package motd
|
|
|
|
|
|
|
|
import (
|
2019-06-02 06:16:43 +00:00
|
|
|
"encoding/json"
|
2018-08-21 18:40:42 +00:00
|
|
|
"net/http"
|
2019-06-02 06:16:43 +00:00
|
|
|
"strings"
|
2018-08-21 18:40:42 +00:00
|
|
|
|
2019-07-25 22:38:07 +00:00
|
|
|
"github.com/portainer/libcrypto"
|
2018-09-10 10:01:38 +00:00
|
|
|
"github.com/portainer/libhttp/response"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api"
|
|
|
|
"github.com/portainer/portainer/api/http/client"
|
2018-08-21 18:40:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type motdResponse struct {
|
2019-06-02 06:16:43 +00:00
|
|
|
Title string `json:"Title"`
|
|
|
|
Message string `json:"Message"`
|
|
|
|
ContentLayout map[string]string `json:"ContentLayout"`
|
|
|
|
Style string `json:"Style"`
|
|
|
|
Hash []byte `json:"Hash"`
|
2018-08-21 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 06:16:43 +00:00
|
|
|
type motdData struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
Message []string `json:"message"`
|
|
|
|
ContentLayout map[string]string `json:"contentLayout"`
|
|
|
|
Style string `json:"style"`
|
|
|
|
}
|
2018-08-21 18:40:42 +00:00
|
|
|
|
2019-06-02 06:16:43 +00:00
|
|
|
func (handler *Handler) motd(w http.ResponseWriter, r *http.Request) {
|
2018-09-24 00:09:12 +00:00
|
|
|
motd, err := client.Get(portainer.MessageOfTheDayURL, 0)
|
2018-08-21 18:40:42 +00:00
|
|
|
if err != nil {
|
2018-11-13 23:20:33 +00:00
|
|
|
response.JSON(w, &motdResponse{Message: ""})
|
2018-08-21 18:40:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-02 06:16:43 +00:00
|
|
|
var data motdData
|
|
|
|
err = json.Unmarshal(motd, &data)
|
2019-03-05 03:05:15 +00:00
|
|
|
if err != nil {
|
|
|
|
response.JSON(w, &motdResponse{Message: ""})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-02 06:16:43 +00:00
|
|
|
message := strings.Join(data.Message, "\n")
|
|
|
|
|
2019-07-25 22:38:07 +00:00
|
|
|
hash := libcrypto.HashFromBytes([]byte(message))
|
2019-06-02 06:16:43 +00:00
|
|
|
resp := motdResponse{
|
|
|
|
Title: data.Title,
|
|
|
|
Message: message,
|
|
|
|
Hash: hash,
|
|
|
|
ContentLayout: data.ContentLayout,
|
|
|
|
Style: data.Style,
|
|
|
|
}
|
|
|
|
|
|
|
|
response.JSON(w, &resp)
|
2018-08-21 18:40:42 +00:00
|
|
|
}
|