haven notify :)
This commit is contained in:
12
haven-notify/Dockerfile
Normal file
12
haven-notify/Dockerfile
Normal file
@@ -0,0 +1,12 @@
|
||||
# Start from the official Golang image for building
|
||||
FROM golang:1.25 AS builder
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
RUN go build -o haven-notify main.go
|
||||
|
||||
# Use a minimal image for running
|
||||
FROM busybox:latest
|
||||
WORKDIR /app
|
||||
COPY --from=builder /app/haven-notify .
|
||||
EXPOSE 8080
|
||||
ENTRYPOINT ["/app/haven-notify"]
|
54
haven-notify/deploy/haven-notify.yaml
Normal file
54
haven-notify/deploy/haven-notify.yaml
Normal file
@@ -0,0 +1,54 @@
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: haven-notify
|
||||
labels:
|
||||
app: haven-notify
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: haven-notify
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: haven-notify
|
||||
spec:
|
||||
containers:
|
||||
- name: haven-notify
|
||||
image: git.ivanch.me/ivanch/haven-notify:latest
|
||||
imagePullPolicy: Always
|
||||
ports:
|
||||
- containerPort: 8080
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: haven-notify
|
||||
spec:
|
||||
selector:
|
||||
app: haven-notify
|
||||
ports:
|
||||
- protocol: TCP
|
||||
port: 8080
|
||||
targetPort: 8080
|
||||
---
|
||||
apiVersion: networking.k8s.io/v1
|
||||
kind: Ingress
|
||||
metadata:
|
||||
name: haven-notify
|
||||
namespace: default
|
||||
annotations:
|
||||
traefik.ingress.kubernetes.io/router.entrypoints: web
|
||||
spec:
|
||||
rules:
|
||||
- host: notify.haven
|
||||
http:
|
||||
paths:
|
||||
- path: /
|
||||
pathType: Prefix
|
||||
backend:
|
||||
service:
|
||||
name: haven-notify
|
||||
port:
|
||||
number: 8080
|
76
haven-notify/main.go
Normal file
76
haven-notify/main.go
Normal file
@@ -0,0 +1,76 @@
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Notification payload
|
||||
type Notification struct {
|
||||
Title string `json:"title"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/notify", notifyHandler)
|
||||
log.Println("Starting server on :8080...")
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
|
||||
func notifyHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
w.Write([]byte("Method not allowed"))
|
||||
return
|
||||
}
|
||||
|
||||
var notif Notification
|
||||
if err := json.NewDecoder(r.Body).Decode(¬if); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte("Invalid payload"))
|
||||
return
|
||||
}
|
||||
|
||||
// Call Discord notification function
|
||||
if err := sendDiscordNotification(notif.Title, notif.Message); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("Failed to send Discord notification"))
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("Notification sent"))
|
||||
}
|
||||
|
||||
func sendDiscordNotification(title, message string) error {
|
||||
const webhookURL = ""
|
||||
|
||||
// Discord webhook payload
|
||||
type discordPayload struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
content := "**" + title + "**\n" + message
|
||||
payload := discordPayload{Content: content}
|
||||
|
||||
jsonData, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return fmt.Errorf("Discord webhook returned status: %s", resp.Status)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user