diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..51fbfcc --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "johnpapa.vscode-peacock" + ] +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..16c9c47 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,19 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch", + "type": "go", + "request": "launch", + "mode": "debug", + "program": "${workspaceRoot}/cmd/main.go", + "env": { + "KAFKA_PORT":"9092", + }, + "args": [] + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..bf19f1d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,25 @@ +{ + "workbench.colorCustomizations": { + "activityBar.activeBackground": "#0bba5d", + "activityBar.activeBorder": "#eee1fd", + "activityBar.background": "#0bba5d", + "activityBarBadge.background": "#eee1fd", + "activityBarBadge.foreground": "#15202b", + "statusBar.background": "#088a45", + "statusBarItem.hoverBackground": "#0bba5d", + "titleBar.activeBackground": "#088a45", + "titleBar.inactiveBackground": "#088a4599", + "activityBar.foreground": "#e7e7e7", + "activityBar.inactiveForeground": "#e7e7e799", + "statusBar.foreground": "#e7e7e7", + "titleBar.activeForeground": "#e7e7e7", + "titleBar.inactiveForeground": "#e7e7e799", + "statusBarItem.remoteBackground": "#088a45", + "statusBarItem.remoteForeground": "#e7e7e7", + "sash.hoverBorder": "#0bba5d", + "commandCenter.border": "#e7e7e799" + }, + "peacock.color": "#088a45", + "todo-tree.tree.disableCompactFolders": false, + "todo-tree.tree.showBadges": true +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..cd64240 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,55 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "build linux", + "type": "shell", + "group": { + "kind": "build", + "isDefault": true + }, + "presentation": { + "echo": true, + "panel": "new" + }, + "options": { + "cwd": "${workspaceRoot}", + "env": { + "APP": "pakitara", + "GOOS": "linux", + "GOARCH": "amd64", + "GOBIN": "${env:GOPATH}/bin" + }, + "args": [ + "ldflags '-s -w'" + ] + }, + "command": "go build -o $GOBIN/$APP-$GOOS-$GOARCH -ldflags \"-s -w\" $APP.go", + "problemMatcher": [] + }, + { + "label": "build windows", + "type": "shell", + "group": "build", + "presentation": { + "echo": true, + "panel": "new" + }, + "options": { + "cwd": "${workspaceRoot}", + "env": { + "APP": "pakitara", + "GOOS": "windows", + "GOARCH": "amd64", + "GOBIN": "${env:GOPATH}/bin" + }, + "args": [ + "ldflags '-s -w'" + ] + }, + "command": "go build -o $GOBIN\\$APP-$GOOS-$GOARCH.exe -ldflags \"-s -w\" $APP.go" + } + ] +} \ No newline at end of file diff --git a/cmd/main.go b/cmd/main.go index e8ab571..0adb81d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,32 +1,51 @@ package main import ( + "applicatura/core/config" "context" - "fmt" "log" + "os" + "os/signal" "github.com/segmentio/kafka-go" ) func main() { r := kafka.NewReader(kafka.ReaderConfig{ - Brokers: []string{"localhost:9092", "localhost:9093", "localhost:9094"}, - Topic: "topic-A", + Topic: "registrations", + Brokers: []string{"localhost:9092"}, + GroupID: "consumer-group-id", Partition: 0, MinBytes: 10e3, // 10KB MaxBytes: 10e6, // 10MB }) - r.SetOffset(42) + defer r.Close() + + // + // r.SetOffset(0) for { m, err := r.ReadMessage(context.Background()) if err != nil { break } - fmt.Printf("message at offset %d: %s = %s\n", m.Offset, string(m.Key), string(m.Value)) - } - if err := r.Close(); err != nil { - log.Fatal("failed to close reader:", err) + log.Printf("message at offset %d: %s = %s\n", m.Offset, string(m.Key), string(m.Value)) } } + +// Остановка сервера +func stop(signalChannel chan os.Signal, srv *server.AuthDBServer) { + defer srv.GracefulStop() + defer signal.Stop(signalChannel) + + log.Println("authPostmanServer stopping ...") +} + +// Запуск сервера +func start(config *config.Config, srv *server.AuthDBServer) { + // connStr := net.JoinHostPort("", strconv.Itoa(config.App.Port)) + + // + log.Printf("authPostmanServer starting (%s)\n", connStr) +} diff --git a/go.mod b/go.mod index 628b5ce..605c34f 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.19 require github.com/segmentio/kafka-go v0.4.38 require ( + github.com/kelseyhightower/envconfig v1.4.0 // indirect github.com/klauspost/compress v1.15.9 // indirect github.com/pierrec/lz4/v4 v4.1.15 // indirect ) diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..99f0914 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,27 @@ +package config + +import ( + "log" + + "github.com/kelseyhightower/envconfig" +) + +type kafkaConfig struct { + Host string `envconfig:"KAFKA_HOST"` + Port int `envconfig:"KAFKA_PORT"` +} + +// ... +type Config struct { + Kafka kafkaConfig +} + +func NewConfig() *Config { + c := Config{} + err := envconfig.Process("", &c) + if err != nil { + log.Fatal(err.Error()) + } + + return &c +}