From 63c952cce7b8ecc35e1819e0745ea39e492652bc Mon Sep 17 00:00:00 2001 From: dehwyy Date: Thu, 2 Nov 2023 17:26:34 +0300 Subject: [PATCH 1/3] implement signin, hashmap + dockerize it --- .../twirp/internal/impl/authorization.go | 15 ++++-- apps/gateway/twirp/internal/impl/hashmap.go | 3 +- apps/gateway/twirp/main.go | 7 +-- apps/hashmap/Dockerfile | 51 +++++++++++++++++++ docker-compose.yml | 15 ++++++ 5 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 apps/hashmap/Dockerfile diff --git a/apps/gateway/twirp/internal/impl/authorization.go b/apps/gateway/twirp/internal/impl/authorization.go index ca02f18..34cd513 100644 --- a/apps/gateway/twirp/internal/impl/authorization.go +++ b/apps/gateway/twirp/internal/impl/authorization.go @@ -28,15 +28,11 @@ func NewAuthorizationService(auth_service_url string, args TwirpAuthorizationSer } func (s *TwirpAuthorizationService) SignUp(ctx context.Context, req *auth.SignUpRequest) (*auth.AuthResponse, error) { - fmt.Printf("SignUp %v", req) response, err := s.client.SignUp(ctx, req) if err != nil { - fmt.Printf("Error %v", err) return nil, err } - fmt.Printf("Response %v", response) - if err = s.set_token(ctx, response.Token); err != nil { return nil, err } @@ -54,7 +50,16 @@ func (s *TwirpAuthorizationService) SignIn(ctx context.Context, req *auth.SignIn return nil, err } - return response, nil + if err = s.set_token(ctx, response.Token); err != nil { + return nil, err + } + + new_response := &auth.AuthResponse{ + UserId: response.UserId, + Username: response.Username, + } + + return new_response, nil } func (s *TwirpAuthorizationService) IsUniqueUsername(ctx context.Context, req *auth.IsUniqueUsernamePayload) (*auth.IsUnique, error) { diff --git a/apps/gateway/twirp/internal/impl/hashmap.go b/apps/gateway/twirp/internal/impl/hashmap.go index 66d8a8e..6d86ba5 100644 --- a/apps/gateway/twirp/internal/impl/hashmap.go +++ b/apps/gateway/twirp/internal/impl/hashmap.go @@ -7,6 +7,7 @@ import ( "github.com/dehwyy/makoto/libs/grpc/generated/general" "github.com/dehwyy/makoto/libs/grpc/generated/hashmap" "github.com/golang/protobuf/ptypes/empty" + tw "github.com/twitchtv/twirp" ) type Empty = empty.Empty @@ -24,7 +25,7 @@ func NewHashmapService(hashmap_service_url string, args TwirpHashmapService) has client: services.NewHashmapService(services.HashmapService{ HashmapServiceUrl: hashmap_service_url, }), - }) + }, tw.WithServerPathPrefix("/hashmap")) } func (s *TwirpHashmapService) GetItems(ctx context.Context, req *hashmap.GetItemsPayload) (*hashmap.GetItemsResponse, error) { diff --git a/apps/gateway/twirp/main.go b/apps/gateway/twirp/main.go index 76f5899..6816e29 100644 --- a/apps/gateway/twirp/main.go +++ b/apps/gateway/twirp/main.go @@ -38,14 +38,11 @@ func main() { ReadAuthorizationData: md_authorization.Read, }) - r.Get("/", func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("hello")) - }) - // mount r.Mount(authorization_service.PathPrefix(), md_with_authorization_header.Middleware(authorization_service)) - r.Mount("/hashmap", md_authorization.Middleware(hashmap_service)) + r.Mount(hashmap_service.PathPrefix(), md_authorization.Middleware(hashmap_service)) + // as TwirpGatewayUrl looks like `http://{host}:{port}/*` port := ":" + strings.Split(config.TwirpGatewayUrl, ":")[2] log.Infof("Gateway server started on port %s", port) diff --git a/apps/hashmap/Dockerfile b/apps/hashmap/Dockerfile new file mode 100644 index 0000000..c2d6ebf --- /dev/null +++ b/apps/hashmap/Dockerfile @@ -0,0 +1,51 @@ +FROM oven/bun as bun + +# node_modules +RUN mkdir -p /temp/dev +COPY package.json /temp/dev +RUN mkdir -p /temp/dev/frontend +COPY libs /temp/dev/libs +RUN cd /temp/dev && bun install + +# app build +FROM node:18-alpine as builder + +# deps +RUN apk add build-base curl libc6-compat g++ git protoc + +# cargo install +RUN curl https://sh.rustup.rs -sSf | sh -s -- -y +ENV PATH="$HOME/.cargo/bin:${PATH}" +ENV PATH="/root/.cargo/bin:${PATH}" + +# go +COPY --from=golang:1.21.0-alpine /usr/local/go/ /usr/local/go/ +ENV PATH="$PATH:/usr/local/go/bin" +ENV PATH="$PATH:/root/go/bin" + +# +WORKDIR /src + +# install deps for gprc(twirp gen) +RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28.1 && \ + go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.3.0 && \ + go install github.com/twitchtv/twirp/protoc-gen-twirp@latest && \ + npm i -g pnpm + +# copy files +COPY --from=bun /temp/dev/node_modules ./node_modules +COPY ./apps/hashmap/go.mod ./apps/hashmap/go.sum ./apps/hashmap/ +COPY ./apps/hashmap ./apps/hashmap +COPY Cargo.toml . +COPY libs libs + +# generate files go/ts +RUN cd libs/grpc && cargo run + +# create go workspace only with necessary packages +RUN go work init ./apps/hashmap ./libs/database ./libs/config ./libs/logger ./libs/grpc + + +RUN go build -o hashmap_backend ./apps/hashmap/cmd + +CMD ["./hashmap_backend"] diff --git a/docker-compose.yml b/docker-compose.yml index 64b6976..b970e2c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -26,6 +26,21 @@ services: - makoto_network - makoto_db + hashmap_backend: + build: + context: . + dockerfile: ./apps/hashmap/Dockerfile + ports: + - ${HASHMAP_PORT}:4000 + container_name: hashmap_backend + env_file: + - .env.prod + depends_on: + - db + networks: + - makoto_network + - makoto_db + auth_frontend: build: context: . From 157a50ae4f7745a7861184f28abe9528b1af95be Mon Sep 17 00:00:00 2001 From: dehwyy Date: Fri, 3 Nov 2023 11:25:08 +0300 Subject: [PATCH 2/3] fixed tsconfig & docker (partially) --- docker-compose.yml | 7 ++++--- libs/grpc/src/main.rs | 2 +- libs/grpc/tsconfig.json | 1 + 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 604cb87..e234f2c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -23,8 +23,8 @@ services: depends_on: - db networks: - - makoto_network - makoto_db + - makoto_network hashmap_backend: build: @@ -38,8 +38,8 @@ services: depends_on: - db networks: - - makoto_network - makoto_db + - makoto_network auth_frontend: build: @@ -75,12 +75,13 @@ services: - POSTGRES_PASSWORD=postgres - POSTGRES_USER=postgres ports: - - 5432:5432 + - 5435:5432 networks: - makoto_db networks: makoto_network: external: true + name: makoto_network makoto_db: name: makoto_db diff --git a/libs/grpc/src/main.rs b/libs/grpc/src/main.rs index c12568f..d832c28 100644 --- a/libs/grpc/src/main.rs +++ b/libs/grpc/src/main.rs @@ -40,7 +40,7 @@ fn main() { }); // 2. post-build : transpile .ts -> .js and generate .d.ts - thread::sleep(time::Duration::from_secs(3)); + thread::sleep(time::Duration::from_secs(5)); let mut file = fs::File::options().write(true).read(true).append(true).open("./generated/auth/auth.ts").unwrap(); diff --git a/libs/grpc/tsconfig.json b/libs/grpc/tsconfig.json index 48ea3c3..bcc3c15 100644 --- a/libs/grpc/tsconfig.json +++ b/libs/grpc/tsconfig.json @@ -5,6 +5,7 @@ "compilerOptions": { "strict": false, "sourceMap": false, + "lib": ["ES2015.Proxy", "ES2015", "ES2015.Reflect"], "outDir": "./dist", "skipLibCheck": true, "declaration": true, From 4d0e21cb9d6902e0a827dcf85f55218b80bdb833 Mon Sep 17 00:00:00 2001 From: dehwyy Date: Sat, 25 Nov 2023 00:29:05 +0300 Subject: [PATCH 3/3] stage --- Caddyfile | 12 ++ Idea.md | 60 ++++++ frontend/auth/src/lib/api/handle_auth.ts | 23 +++ .../main/src/app/[locale]/me/edit/page.tsx | 177 +++--------------- .../main/src/app/[locale]/me/edit/privacy.tsx | 44 +++++ .../main/src/app/[locale]/me/edit/profile.tsx | 153 +++++++++++++++ .../me/edit/profile_bgcolor-picker.tsx | 121 ++++++++++++ .../app/[locale]/me/edit/profile_preview.tsx | 52 +++++ .../src/app/api/v1/email/confirm/route.ts | 5 + .../api/v1/email/send-confirmation/route.ts | 5 + .../src/app/api/v1/userinfo/update/route.ts | 22 +++ frontend/main/src/lib/fetches.ts | 11 ++ frontend/main/src/lib/route-cookies.ts | 18 ++ frontend/main/src/lib/route-response.ts | 27 +++ frontend/main/src/lib/route.ts | 2 + go.work.sum | 6 + libs/database/go.mod | 1 + libs/database/go.sum | 2 + libs/database/models/user_data.go | 7 +- libs/database/models/user_privacy.go | 10 + libs/database/types/enums.go | 22 +++ libs/grpc/.tslib/clients/auth.d.ts | 3 + libs/grpc/.tslib/clients/auth.js | 7 + libs/grpc/.tslib/clients/hashmap.d.ts | 3 + libs/grpc/.tslib/clients/hashmap.js | 7 + libs/grpc/.tslib/clients/index.d.ts | 7 + libs/grpc/.tslib/clients/index.js | 52 +++++ libs/grpc/.tslib/clients/user.d.ts | 3 + libs/grpc/.tslib/clients/user.js | 7 + libs/grpc/.tslib/index.d.ts | 1 + libs/grpc/.tslib/index.js | 1 + libs/grpc/.tslib/interceptors.d.ts | 13 ++ libs/grpc/.tslib/interceptors.js | 18 ++ 33 files changed, 747 insertions(+), 155 deletions(-) create mode 100644 Caddyfile create mode 100644 Idea.md create mode 100644 frontend/auth/src/lib/api/handle_auth.ts create mode 100644 frontend/main/src/app/[locale]/me/edit/privacy.tsx create mode 100644 frontend/main/src/app/[locale]/me/edit/profile.tsx create mode 100644 frontend/main/src/app/[locale]/me/edit/profile_bgcolor-picker.tsx create mode 100644 frontend/main/src/app/[locale]/me/edit/profile_preview.tsx create mode 100644 frontend/main/src/app/api/v1/email/confirm/route.ts create mode 100644 frontend/main/src/app/api/v1/email/send-confirmation/route.ts create mode 100644 frontend/main/src/app/api/v1/userinfo/update/route.ts create mode 100644 frontend/main/src/lib/route-cookies.ts create mode 100644 frontend/main/src/lib/route-response.ts create mode 100644 frontend/main/src/lib/route.ts create mode 100644 libs/database/models/user_privacy.go create mode 100644 libs/database/types/enums.go create mode 100644 libs/grpc/.tslib/clients/auth.d.ts create mode 100644 libs/grpc/.tslib/clients/auth.js create mode 100644 libs/grpc/.tslib/clients/hashmap.d.ts create mode 100644 libs/grpc/.tslib/clients/hashmap.js create mode 100644 libs/grpc/.tslib/clients/index.d.ts create mode 100644 libs/grpc/.tslib/clients/index.js create mode 100644 libs/grpc/.tslib/clients/user.d.ts create mode 100644 libs/grpc/.tslib/clients/user.js create mode 100644 libs/grpc/.tslib/index.d.ts create mode 100644 libs/grpc/.tslib/index.js create mode 100644 libs/grpc/.tslib/interceptors.d.ts create mode 100644 libs/grpc/.tslib/interceptors.js diff --git a/Caddyfile b/Caddyfile new file mode 100644 index 0000000..4c2d9bc --- /dev/null +++ b/Caddyfile @@ -0,0 +1,12 @@ +{ + auto_https off +} + +http://makoto.dehwyy { + + handle_path /passport* { + rewrite * / + reverse_proxy localhost:3001 + } + +} diff --git a/Idea.md b/Idea.md new file mode 100644 index 0000000..3110e84 --- /dev/null +++ b/Idea.md @@ -0,0 +1,60 @@ +# Makoto/v3 + +## Frontend +### Main page +- /: Welcome page +- /me: Current user page +- /me/edit: Current user edit page +- /me/friends: User's friends +______ +- /: logo + text `Makoto` +- /me: user's picture, username, join date, intergration (providers like Discord, Spotify) | edit button | microservice stat +- /me/edit: +1. update username, description, picture, preferred color, languages +2. "who can see my user page?", "who can send me friend request?", "who can see my micro stats?", "who can message me?", "who can see my user picture?" +3. integrations' settings, email settings (redirect) + + +### Authentication +- /: login +- /signup +- /confirm-email +- /password/change +- /password/recover +______ +- SignIn via OAuth2/Credentials +- SignUp via credentials +- Confirm email when via credentials +- Change/Recover password for credentials +- Max SignIn tries ~ 5 -> timeout +- Error messages +- Clarify whether username/email is reserved +- Password strongness validation + +## Backend +### Authentication +- SignIn +- SignUp +- SignInOAuth +- SendConfirmationMail (no) +- ConfirmMailByCode (tends to be hashed username -> compare hash maybe?) +- ProceedToUpdatePassword ( using old password ) +- UpdatePassword ( via some sort of generated and stored in db (or redis) token ) +- SendRecoverPasswordMail (no) +- SubmitNewPasswordByRecoverdCode ( same sort token as for UpdatePassword) +- IsEmailAvailable +- IsUsernameAvailable +_______ + +### Mail Service +- SendEmail + +### CDN + +### Server Registry && Discovery +- RegisterService +- UnregisterService +_____ +- Redis ( as key-value pairs ) + +### diff --git a/frontend/auth/src/lib/api/handle_auth.ts b/frontend/auth/src/lib/api/handle_auth.ts new file mode 100644 index 0000000..52dfe8c --- /dev/null +++ b/frontend/auth/src/lib/api/handle_auth.ts @@ -0,0 +1,23 @@ +import type { RpcStatus } from '@protobuf-ts/runtime-rpc' +import { redirect } from '@sveltejs/kit' + +interface HandlerProps { + status: RpcStatus +} + +export class HandleAuth { + static Handle({ status }: HandlerProps): Response | never { + const { code, detail } = status || { code: 'Error', detail: 'Internal Server Error' } + + console.log(`code: ${code} with detail ${detail}`) + + if (code != 'OK') { + return new Response(null, { + status: 500, + statusText: detail.split('\n')[0] || 'Internal Server Error' + }) + } + + throw redirect(307, '/?redirect=/') + } +} diff --git a/frontend/main/src/app/[locale]/me/edit/page.tsx b/frontend/main/src/app/[locale]/me/edit/page.tsx index 6e47e6b..d009143 100644 --- a/frontend/main/src/app/[locale]/me/edit/page.tsx +++ b/frontend/main/src/app/[locale]/me/edit/page.tsx @@ -1,33 +1,21 @@ 'use client' -import { Dialog, DialogTrigger, DialogContent } from '$/components/ui/dialog' import { Tabs, TabsContent, TabsList, TabsTrigger } from '$/components/ui/tabs' -import { Input } from '$/components/ui/input' -import { Label } from '$/components/ui/label' -import { useState } from 'react' -import { Separator } from '$/components/ui/separator' -import { Textarea } from '$/components/ui/textarea' -import dynamic from 'next/dynamic' -import { Button } from '$/components/ui/button' -import Image from 'next/image' -import { cn } from '$/lib/utils' -import Link from 'next/link' -import { Switch } from '$/components/ui/switch' - -const PictureEditor = dynamic(() => import('$/components/picture-editor'), { ssr: false }) +import Privacy from './privacy' +import Profile from './profile' + +const user = { + name: 'dehwyy', + email: 'dehwyy@google.com', + customId: '', + description: "Hello, I'm dehwyy and using Makoto but way longer sentence to test if it works.", + dark_background: '#171717', + light_background: '#2ccce7', + image: '', + + isVerifiedEmail: false, +} const Page = () => { - const initialName = 'dehwyy!' - const customId = 'a0131d5d-6b2b-4b2a-8b5b-4b2a8b5b4b2a' - const initialDescription = "Hello, I'm dehwyy and using Makoto but way longer sentence to test if it works." - const dark_background = 'dark:bg-[#171717]' - const background = 'bg-[#2ccce7]' - - const [name, setName] = useState(initialName) - const [id, setId] = useState(customId) - const [description, setDescription] = useState(initialDescription) - - const [image, setImage] = useState('') - return (
@@ -38,140 +26,25 @@ const Page = () => { {/* User profile */} -
-
- - setName(e.target.value)} - /> - - - - setId(e.target.value)} - /> - - - -