about summary refs log tree commit diff
path: root/src/rest-api/cmd/main.go
blob: ff918563bd3ddb85d1a88d998616e008c17c8b29 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"

	"github.com/Baitinq/fs-tracer-backend/src/rest-api/handler"
	"github.com/jmoiron/sqlx"
	"github.com/segmentio/kafka-go"
	"github.com/segmentio/kafka-go/sasl/plain"
)

func main() {
	kafka_password, ok := os.LookupEnv("KAFKA_PASSWORD")
	if !ok {
		log.Fatal("KAFKA_PASSWORD not set")
	}

	dialer := &kafka.Dialer{
		Timeout:   10 * time.Second,
		DualStack: true,
		SASLMechanism: plain.Mechanism{
			Username: "user1",
			Password: kafka_password,
		},
	}
	// Create topic
	_, err := dialer.DialLeader(context.Background(), "tcp", "kafka.default.svc.cluster.local:9092", "topic-A", 0)
	if err != nil {
		log.Fatal(err)
	}

	kafka_writer := &kafka.Writer{
		Addr: kafka.TCP("kafka.default.svc.cluster.local:9092"),
		Transport: &kafka.Transport{
			SASL: plain.Mechanism{
				Username: "user1",
				Password: kafka_password,
			},
		},
		Topic:    "topic-A",
		Balancer: &kafka.LeastBytes{},
		// Async:                  true,
		AllowAutoTopicCreation: true,
	}

	db_password, ok := os.LookupEnv("DB_PASSWORD")
	if !ok {
		log.Fatal("DB_PASSWORD not set")
	}
	db, err := sqlx.Connect("postgres", fmt.Sprintf("postgres://postgres.slpoocycjgqsuoedhkbn:%s@aws-0-eu-central-1.pooler.supabase.com:5432/postgres", db_password))
	if err != nil {
		log.Fatal("cannot initalize db client", err)
	}

	handler := handler.NewHandler(db, kafka_writer)

	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "Hello folks!")
	})
	mux.Handle("/api/v1/file/", handler)

	http.ListenAndServe(":8080", mux)
}