Init commit, base struct and redis connection

This commit is contained in:
Schrody
2026-01-29 17:55:14 +01:00
parent 58164d4b10
commit 9ae5696aec
10 changed files with 206 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/go
{
"name": "Go",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/go:1-1.22-bookworm",
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
"settings": {},
"extensions": [
"streetsidesoftware.code-spell-checker"
]
}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [9000],
// Use 'portsAttributes' to set default properties for specific forwarded ports.
// More info: https://containers.dev/implementors/json_reference/#port-attributes
"portsAttributes": {
"9000": {
"label": "Hello Remote World",
"onAutoForward": "notify"
}
}
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "go version",
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}

17
Dockerfile Normal file
View File

@@ -0,0 +1,17 @@
FROM golang:1.23.4-alpine AS builder
WORKDIR /app
COPY go.mod ./
RUN go mod download && go mod verify
COPY . .
RUN go build -o main cmd/main.go
FROM scratch
COPY --from=builder /app/main /main
CMD ["./main"]

View File

@@ -1,2 +1,3 @@
# UeBucch
[Redis Viewer](https://github.com/ekvedaras/redis-gui)

36
cmd/main.go Normal file
View File

@@ -0,0 +1,36 @@
package main
import (
"fmt"
"net/http"
"io"
"context"
"uebucch/manager"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "UeBucch!")
})
http.HandleFunc("/webhook", func(w http.ResponseWriter, r *http.Request) {
webHookName := r.URL.Query().Get("name")
redisClient := manager.NewRedisClient()
body, _ := io.ReadAll(r.Body)
res := redisClient.LPush(context.Background(), webHookName, body)
if res.Err() != nil {
fmt.Fprintf(w, "Error: %s", res.Err())
return
}
fmt.Fprintf(w, "Webhook created!")
})
http.HandleFunc("/webhooks", func(w http.ResponseWriter, r *http.Request) {
webHooks, err := manager.GetWebhooks(context.Background(), manager.NewRedisClient())
if err != nil {
fmt.Fprintf(w, "Error: %s", err)
return
}
for _, webHook := range webHooks {
fmt.Fprintf(w, "Webhook: %s", webHook)
}
})
http.ListenAndServe(":8080", nil)
}

22
docker-compose.yaml Normal file
View File

@@ -0,0 +1,22 @@
services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
- 8080:8080
depends_on:
- redis
environment:
- REDIS_ADDR=redis:6379
redis:
image: redis:7.4.1-alpine
ports:
- 6379:6379
volumes:
- redis-data:/data
volumes:
redis-data:

10
go.mod Normal file
View File

@@ -0,0 +1,10 @@
module uebucch
go 1.22.2
require github.com/redis/go-redis/v9 v9.17.3
require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
)

10
go.sum Normal file
View File

@@ -0,0 +1,10 @@
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/redis/go-redis/v9 v9.17.3 h1:fN29NdNrE17KttK5Ndf20buqfDZwGNgoUr9qjl1DQx4=
github.com/redis/go-redis/v9 v9.17.3/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370=

28
handlers/worker.go Normal file
View File

@@ -0,0 +1,28 @@
package handlers
import (
"bytes"
"log"
"net/http"
"time"
"uebucch/structs"
)
func worker(id int, jobs <-chan structs.WebhookJob) {
client := &http.Client{Timeout: 10 * time.Second}
for job := range jobs {
req, _ := http.NewRequest(job.Method, job.TargetURL, bytes.NewBuffer(job.Payload))
for k, v := range job.Headers {
req.Header[k] = v
}
resp, err := client.Do(req)
if err != nil {
log.Printf("Worker %d: Errore nell'inoltro: %v", id, err)
continue
}
resp.Body.Close()
log.Printf("Worker %d: Inoltrato con successo a %s", id, job.TargetURL)
}
}

35
manager/redis.go Normal file
View File

@@ -0,0 +1,35 @@
package manager
import (
"context"
"os"
"github.com/redis/go-redis/v9"
)
func NewRedisClient() *redis.Client {
addr := os.Getenv("REDIS_ADDR")
if addr == "" {
addr = "redis:6379"
}
password := os.Getenv("REDIS_PASSWORD")
return redis.NewClient(&redis.Options{
Addr: addr,
Password: password,
DB: 0,
})
}
func GetWebhooks(ctx context.Context, client *redis.Client) ([]string, error) {
return client.LRange(ctx, "webhooks", 0, -1).Result()
}
func CreateWebhook(ctx context.Context, client *redis.Client, webhook string) error {
return client.LPush(ctx, "webhooks", webhook).Err()
}
func DeleteWebhook(ctx context.Context, client *redis.Client, webhook string) error {
return client.LRem(ctx, "webhooks", 0, webhook).Err()
}

8
structs/request.go Normal file
View File

@@ -0,0 +1,8 @@
package structs
type WebhookJob struct {
TargetURL string
Method string
Headers map[string][]string
Payload []byte
}