Skip to content

Commit f07447c

Browse files
author
RahatMelsov
committed
feat: добавлены новые ручки как логин и список файлов (добавлены токены для отслеживания изменении)Э
1 parent d908b3b commit f07447c

File tree

32 files changed

+888
-406
lines changed

32 files changed

+888
-406
lines changed

.env.dev

Lines changed: 0 additions & 18 deletions
This file was deleted.

Makefile

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
1-
SERVER_PKG ?= ./cmd/server
1+
# Makefile (минимальный)
2+
APP := tui
3+
PKG := ./cmd/tui
4+
DIST := dist
25

3-
GRPC_ADDR ?= 127.0.0.1:8080
4-
DB_DSN ?= mem://
6+
.PHONY: build run clean linux mac windows
57

6-
.PHONY: run build tidy help
8+
build:
9+
mkdir -p $(DIST)
10+
go mod tidy
11+
CGO_ENABLED=0 go build -o $(DIST)/$(APP) $(PKG)
712

8-
help:
9-
@echo "make run - run server via 'go run'"
10-
@echo "make build -build server binary into ./bin/server"
11-
@echo "env overrides: GRPC_ADDR (default: $(GRPC_ADDR)), DB_DSN (default: $(DB_DSN))"
12-
@echo "example: make run GRPC_ADDR=:9090 DB_DSN='postgres://...@localhost:5432/vault?sslmode=disable"
13+
run: build
14+
$(DIST)/$(APP)
1315

14-
run:
15-
@echo "===> go run $(SERVER_PKG) (DB_DSN=$(GRPC_ADDR), DB_DSN=$(DB_DSN))"
16-
@GRPC_ADDR=$(GRPC_ADDR) DB_DSN=$(DB_DSN) go run $(SERVER_PKG)
16+
clean:
17+
rm -rf $(DIST)
1718

18-
build:
19-
@mkdir -p bin
20-
@echo "===> go build ./cmd/server -> ./bin/server"
21-
@go build -o bin/server $(SERVER_PKG)
19+
# Кросс-сборки, если нужно
20+
linux:
21+
mkdir -p $(DIST)
22+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $(DIST)/$(APP)_linux_amd64 $(PKG)
23+
24+
mac:
25+
mkdir -p $(DIST)
26+
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o $(DIST)/$(APP)_darwin_arm64 $(PKG)
27+
28+
windows:
29+
mkdir -p $(DIST)
30+
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o $(DIST)/$(APP)_windows_amd64.exe $(PKG)
2231

23-
tidy:
24-
@go mod tidy

api/proto/common/v1/common.proto

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,40 @@ syntax = "proto3";
22
package common.v1;
33
option go_package = "github.com/rtmelsov/adv-keeper/gen/go/common/v1;commonv1";
44

5-
message DeviceInfo {
6-
string device_id = 1; // клиентский идентификатор устройства
7-
}
5+
import "google/protobuf/timestamp.proto";
86

97
service AuthService {
8+
rpc Logout(TokenPair) returns (TokenPair);
109
rpc Register(RegisterRequest) returns (RegisterResponse);
1110
rpc Login(LoginRequest) returns (LoginResponse);
1211
}
1312

14-
message RegisterRequest {
13+
message TokenPair {
14+
string access_token = 1; // JWT (HS256)
15+
//google.protobuf.Timestamp
16+
google.protobuf.Timestamp expires_at = 3; // RFC3339 серверное время истечения refresh
17+
}
18+
19+
message LoginRequest {
1520
string email = 1;
1621
string password = 2;
17-
DeviceInfo device = 3;
1822
}
1923

20-
message RegisterResponse {
24+
message LoginResponse {
2125
string user_id = 1;
22-
string device_id = 2; // вернём то, что прислал клиент
26+
string device_id = 2;
2327
string email = 3;
28+
TokenPair tokens = 4; // ← добавили
2429
}
2530

26-
message LoginRequest {
31+
message RegisterRequest {
2732
string email = 1;
2833
string password = 2;
29-
DeviceInfo device = 3;
3034
}
3135

32-
message LoginResponse {
36+
message RegisterResponse {
3337
string user_id = 1;
3438
string device_id = 2; // вернём то, что прислал клиент
35-
string email = 3;
39+
string email = 3;
40+
TokenPair tokens = 4; // ← добавили
3641
}

cmd/server/main.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,51 @@ import (
55
"google.golang.org/grpc/reflection"
66
"log"
77
"net"
8-
"os"
8+
"time"
99

1010
commonv1 "github.com/rtmelsov/adv-keeper/gen/go/proto/common/v1"
1111
filev1 "github.com/rtmelsov/adv-keeper/gen/go/proto/file/v1"
12-
"github.com/rtmelsov/adv-keeper/internal/auth"
1312
db "github.com/rtmelsov/adv-keeper/internal/db"
1413
"github.com/rtmelsov/adv-keeper/internal/file"
14+
"github.com/rtmelsov/adv-keeper/internal/helpers"
15+
"github.com/rtmelsov/adv-keeper/internal/middleware"
16+
"github.com/rtmelsov/adv-keeper/internal/server"
17+
"google.golang.org/grpc/keepalive"
1518

1619
_ "github.com/jackc/pgx/v5/stdlib"
1720
"google.golang.org/grpc"
1821
)
1922

2023
func main() {
21-
dsn := os.Getenv("DB_DSN")
22-
if dsn == "" {
23-
log.Fatal("DB_DSN is required")
24-
}
25-
addr := os.Getenv("GRPC_ADDR")
26-
if addr == "" {
27-
addr = "127.0.0.1:8080"
24+
envs, err := helpers.LoadConfig()
25+
if err != nil {
26+
log.Fatal(err)
2827
}
29-
// получаем урл бд
3028

31-
lis, err := net.Listen("tcp", addr)
29+
lis, err := net.Listen("tcp", envs.Addr)
3230
if err != nil {
3331
log.Fatal(err)
3432
}
3533

3634
// Подключение к Postgres
37-
dbx, err := sql.Open("pgx", dsn)
35+
dbx, err := sql.Open("pgx", envs.DBDSN)
3836
if err != nil {
3937
log.Fatal(err)
4038
}
4139
defer dbx.Close()
4240

4341
q := db.New(dbx)
44-
s := grpc.NewServer()
45-
commonv1.RegisterAuthServiceServer(s, auth.New(q))
42+
s := grpc.NewServer(
43+
grpc.UnaryInterceptor(middleware.ServerInterceptor),
44+
grpc.KeepaliveParams(keepalive.ServerParameters{
45+
MaxConnectionIdle: 0,
46+
MaxConnectionAge: 0,
47+
MaxConnectionAgeGrace: 0,
48+
Time: 2 * time.Minute,
49+
Timeout: 20 * time.Second,
50+
}),
51+
)
52+
commonv1.RegisterAuthServiceServer(s, server.New(q))
4653
filev1.RegisterFileServiceServer(s, file.New(q))
4754

4855
reflection.Register(s)

cmd/tui/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ var (
1717
)
1818

1919
func main() {
20-
2120
showVersion := flag.Bool("version", false, "print version and exit")
2221
flag.Parse()
2322
if *showVersion {

db/queries/auth.sql

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
-- name: RegisterWithDevice :one
2-
WITH u AS (
3-
INSERT INTO users (email, pwd_phc, e2ee_pub)
4-
VALUES ($1, $2, $3)
5-
RETURNING id
6-
),
7-
d AS (
8-
INSERT INTO devices (user_id, device_id)
9-
SELECT u.id, $4
10-
FROM u
11-
RETURNING device_id
12-
)
13-
SELECT
14-
(SELECT id FROM u) AS user_id,
15-
(SELECT device_id FROM d) AS device_id;
2+
INSERT INTO users (email, pwd_phc, e2ee_pub)
3+
VALUES ($1, $2, $3)
4+
RETURNING id;
165

6+
-- name: GetUserByEmail :one
7+
SELECT id, email, pwd_phc, e2ee_pub, created_at
8+
FROM users
9+
WHERE email = $1;
10+
11+
-- name: AddFile :one
12+
INSERT INTO files (user_id, name, path)
13+
VALUES ($1, $2, $3)
14+
RETURNING id, user_id, name, path, created_at;
15+
16+
-- name: ListFilesByUser :many
17+
SELECT id, name, path, created_at
18+
FROM files
19+
WHERE user_id = $1
20+
ORDER BY created_at DESC;
21+
22+
-- name: DeleteFile :exec
23+
DELETE FROM files
24+
WHERE id = $1 AND user_id = $2;

0 commit comments

Comments
 (0)