add cloud logging and refactor token logic
This commit is contained in:
@@ -1,10 +1,13 @@
|
|||||||
package coffee
|
package coffee
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"cloud.google.com/go/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
type reply struct {
|
type reply struct {
|
||||||
@@ -27,30 +30,69 @@ type message struct {
|
|||||||
User_name string `json:"user_name"`
|
User_name string `json:"user_name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Coffee() replies to mattermost webhooks with the correct token value
|
func contains(haystack []string, needle string) bool {
|
||||||
func Coffee(writer http.ResponseWriter, request *http.Request) {
|
for _, shiny := range haystack {
|
||||||
mattermost_token := os.Getenv("MATTERMOST_TOKEN")
|
if shiny == needle {
|
||||||
test_token := os.Getenv("TEST_TOKEN")
|
return true
|
||||||
|
}
|
||||||
if mattermost_token == "" || test_token == "" {
|
}
|
||||||
log.Fatal("Failed to load mattermost token")
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
incoming_msg := message{}
|
// Coffee() replies to mattermost webhooks with the correct token value
|
||||||
err := json.NewDecoder(request.Body).Decode(&incoming_msg)
|
func Coffee(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
project_id := os.Getenv("PROJECT_ID")
|
||||||
|
|
||||||
|
if project_id == "" {
|
||||||
|
log.Fatal("Environment variable PROJECT_ID is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create cloud logging client
|
||||||
|
ctx := context.Background()
|
||||||
|
client, err := logging.NewClient(ctx, project_id)
|
||||||
|
|
||||||
|
logger := client.Logger("Coffee-log")
|
||||||
|
var stdlog *log.Logger
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("json.NewDecoder: %v\n", err)
|
defer client.Close()
|
||||||
|
stdlog = logger.StandardLogger(logging.Debug)
|
||||||
|
} else {
|
||||||
|
log.Printf("Failed to create logging client: %v", err)
|
||||||
|
stdlog = log.New(os.Stderr, "", log.LstdFlags)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load valid tokens to reply to
|
||||||
|
tokens := make([]string, 0)
|
||||||
|
|
||||||
|
for _, envvar := range []string{"MATTERMOST_TOKEN", "TEST_TOKEN"} {
|
||||||
|
token := os.Getenv(envvar)
|
||||||
|
if token != "" {
|
||||||
|
tokens = append(tokens, token)
|
||||||
|
} else {
|
||||||
|
stdlog.Printf("Environment variable %v is empty\n", token)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmarshal message and reply
|
||||||
|
incoming := message{}
|
||||||
|
err = json.NewDecoder(request.Body).Decode(&incoming)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
stdlog.Printf("Failed to decode message: %v\n", err)
|
||||||
http.Error(writer, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
http.Error(writer, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Coffee requested by user id: %+v", incoming_msg.User_id)
|
if !contains(tokens, incoming.Token) {
|
||||||
log.Println(incoming_msg)
|
stdlog.Printf("Invalid request received with token %+v", incoming.Token)
|
||||||
|
http.Error(writer, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if incoming_msg.Token == mattermost_token || incoming_msg.Token == test_token {
|
err = json.NewEncoder(writer).Encode(&reply{Response_type: "comment", Text: "@omorud"})
|
||||||
json.NewEncoder(writer).Encode(&reply{Response_type: "comment", Text: "@omorud"})
|
|
||||||
} else {
|
if err != nil {
|
||||||
log.Printf("Invalid request received with token: %v", incoming_msg.Token)
|
stdlog.Printf("Failed to encode response: %v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,3 +1,5 @@
|
|||||||
module example.com/cloudfunction
|
module example.com/cloudfunction
|
||||||
|
|
||||||
go 1.13
|
go 1.13
|
||||||
|
|
||||||
|
require cloud.google.com/go/logging v1.6.1
|
||||||
|
|||||||
Reference in New Issue
Block a user