Skip to content

Commit

Permalink
gh-1 initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ryancurrah committed May 26, 2019
0 parents commit 4166617
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# ---- Build container
FROM golang:alpine AS builder
WORKDIR /synology-notifications
COPY . .
RUN apk add --no-cache git
RUN go build -v ./...

# ---- App container
FROM alpine:latest as synology-notifications
EXPOSE 8080
ENV API_KEY=
ENV SLACK_WEBHOOK=
ENV LISTEN_PORT=8080
RUN apk --no-cache add ca-certificates
COPY --from=builder synology-notifications/synology-notifications /
ENTRYPOINT ./synology-notifications
LABEL Name=synology-notifications Version=0.0.1
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/ryancurrah/synology-notifications

go 1.12

require (
github.com/caarlos0/env v3.5.0+incompatible
github.com/gorilla/websocket v1.4.0 // indirect
github.com/nlopes/slack v0.5.0
github.com/pkg/errors v0.8.1 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/caarlos0/env v3.5.0+incompatible h1:Yy0UN8o9Wtr/jGHZDpCBLpNrzcFLLM2yixi/rBrKyJs=
github.com/caarlos0/env v3.5.0+incompatible/go.mod h1:tdCsowwCzMLdkqRYDlHpZCp2UooDD3MspDBjZ2AD02Y=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/nlopes/slack v0.5.0 h1:NbIae8Kd0NpqaEI3iUrsuS0KbcEDhzhc939jLW5fNm0=
github.com/nlopes/slack v0.5.0/go.mod h1:jVI4BBK3lSktibKahxBF74txcK2vyvkza1z/+rRnVAM=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
88 changes: 88 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"

"github.com/caarlos0/env"
"github.com/nlopes/slack"
)

var appConfig = AppConfig{}
var slackConfig = SlackConfig{}

type SynologyMessage struct {
Message string `json:"message"`
}

type AppConfig struct {
ListenPort string `env:"LISTEN_PORT" envDefault:"8080"`
ApiKey string `env:"API_KEY,required"`
}

type SlackConfig struct {
Webhook string `env:"SLACK_WEBHOOK,required"`
}

// PostHandler send notifcations from synology to slack
func PostHandler(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("api_key") != appConfig.ApiKey {
http.Error(w, "invalid api key", http.StatusUnauthorized)
log.Printf("invalid api key")
return
}

if r.Method == "POST" {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "error reading request body", http.StatusInternalServerError)
log.Printf("error reading request body: %s", err)
return
}

synologyMessage := SynologyMessage{}
err = json.Unmarshal(body, &synologyMessage)
if err != nil {
http.Error(w, "error reading request body", http.StatusInternalServerError)
log.Printf("error reading request body: %s", err)
return
}

msg := slack.WebhookMessage{Attachments: []slack.Attachment{{Color: "warning", Text: fmt.Sprintf("%s", synologyMessage.Message)}}}

err = slack.PostWebhook(slackConfig.Webhook, &msg)
if err != nil {
http.Error(w, "error sendming slack message", http.StatusInternalServerError)
log.Printf("error sendming slack message: %s", err)
return
}
} else {
http.Error(w, "invalid request method", http.StatusMethodNotAllowed)
return
}
}

func main() {
err := env.Parse(&appConfig)
if err != nil {
panic(err)
}

err = env.Parse(&slackConfig)
if err != nil {
panic(err)
}

if len(appConfig.ApiKey) < 32 {
panic(fmt.Errorf("api key not long enough it should be 32 characters long not %d", len(appConfig.ApiKey)))
}

mux := http.NewServeMux()
mux.HandleFunc("/", PostHandler)

log.Printf("listening on port %s", appConfig.ListenPort)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", appConfig.ListenPort), mux))
}

0 comments on commit 4166617

Please sign in to comment.