Skip to content

Commit

Permalink
cursed contraption to make tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
gempir committed Feb 12, 2024
1 parent f29d765 commit 7edb761
Show file tree
Hide file tree
Showing 10 changed files with 496 additions and 414 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ web/dbdir

# Binaries
/main
/gempbot
/gempbot
/internal/ysweet/createToken.cjs
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ build_server:
yjs_server:
cd web && yarn yjs

ysweet_token:
cd web && yarn build-ysweet-token

test:
go test ./internal/...

Expand Down
10 changes: 2 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.1
github.com/nicklaw5/helix/v2 v2.26.0
github.com/puzpuzpuz/xsync v1.5.2
github.com/rs/cors v1.10.1
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
Expand All @@ -18,25 +17,20 @@ require (
gorm.io/gorm v1.25.7
)

require github.com/rogpeppe/go-internal v1.12.0 // indirect

replace github.com/nicklaw5/helix/v2 v2.12.0 => github.com/gempir/helix/v2 v2.0.2-0.20221223221449-fe5671ac8ea7

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/pgtype v1.14.2 // indirect
github.com/jackc/pgx/v4 v4.18.1 // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jellydator/ttlcache/v2 v2.11.1
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sync v0.6.0 // indirect
Expand Down
164 changes: 8 additions & 156 deletions go.sum

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions internal/ysweet/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ysweet

import (
"bytes"
_ "embed"
"encoding/json"
"fmt"
"os/exec"
)

//go:embed createToken.cjs

Check failure on line 11 in internal/ysweet/factory.go

View workflow job for this annotation

GitHub Actions / ci

pattern createToken.cjs: no matching files found
var createToken string

type Factory struct {
ysweetUrl string
}

func NewFactory(ysweetUrl string) *Factory {
return &Factory{
ysweetUrl: ysweetUrl,
}
}

type TokenResponse struct {
Url string `json:"url"`
DocId string `json:"docId"`
Token string `json:"token"`
}

func (f *Factory) CreateToken(docID string) (TokenResponse, error) {
cmd := exec.Command("node", "-", "")
cmd.Env = append(cmd.Env, "YSWEET_URL="+f.ysweetUrl)
cmd.Env = append(cmd.Env, "YSWEET_DOC_ID=1"+docID)

cmd.Stdin = bytes.NewBufferString(createToken)

var out bytes.Buffer
var errOut bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &errOut

err := cmd.Run()
if err != nil {
return TokenResponse{}, fmt.Errorf("%s %w", errOut.String(), err)
}

var tokenResponse TokenResponse
err = json.Unmarshal(out.Bytes(), &tokenResponse)
if err != nil {
return TokenResponse{}, fmt.Errorf("%s %w", out.String(), err)
}

return tokenResponse, nil
}
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/gempir/gempbot/internal/store"
"github.com/gempir/gempbot/internal/user"
"github.com/gempir/gempbot/internal/ws"
"github.com/gempir/gempbot/internal/ysweet"
"github.com/rs/cors"
)

Expand Down Expand Up @@ -71,6 +72,13 @@ func main() {
mux.HandleFunc("/api/overlay", apiHandlers.OverlayHandler)
mux.HandleFunc("/api/ws", wsHandler.HandleWs)

tokenFactory := ysweet.NewFactory("xdd")
str, err := tokenFactory.CreateToken("doc")
if err != nil {
log.Error(err)
}
log.Warn(str)

handler := cors.New(cors.Options{
AllowedOrigins: []string{cfg.WebBaseUrl},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH"},
Expand All @@ -79,7 +87,7 @@ func main() {
}).Handler(mux)

log.Info("Starting server on " + cfg.ListenAddress)
err := http.ListenAndServe(cfg.ListenAddress, handler)
err = http.ListenAndServe(cfg.ListenAddress, handler)
if err != nil {
log.Fatal(err)
}
Expand Down
11 changes: 11 additions & 0 deletions web/createYsweetToken.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { DocumentManager } from '@y-sweet/sdk';

const manager = new DocumentManager(process.env.YSWEET_URL);

async function main() {
const clientToken = await manager.getOrCreateDocAndToken(process.env.YSWEET_DOC_ID)

console.log(JSON.stringify(clientToken));
}

main();
7 changes: 6 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"yjs": "node yjs/server.js"
"yjs": "node yjs/server.js",
"build-ysweet-token": "esbuild createYsweetToken.cjs --bundle --outfile=../internal/ysweet/createToken.cjs --platform=node --target=node20"
},
"type": "module",
"dependencies": {
"@heroicons/react": "^2.0.12",
"@tailwindcss/forms": "^0.5.7",
"@tldraw/tldraw": "^2.0.0-beta.2",
"@types/seedrandom": "^3.0.4",
"@y-sweet/react": "^0.1.0",
"@y-sweet/sdk": "^0.1.0",
"dayjs": "^1.11.7",
"eslint-config-next": "^14.1.0",
"jsonwebtoken": "^9.0.2",
Expand All @@ -24,6 +27,7 @@
"react-use-websocket": "^4.5.0",
"seedrandom": "^3.0.5",
"tailwindcss": "^3.4.1",
"y-sweet": "^0.1.0",
"y-utility": "^0.1.3",
"y-websocket": "^1.5.3",
"yjs": "^13.6.11",
Expand All @@ -33,6 +37,7 @@
"@types/node": "^20.11.16",
"@types/react": "^18.2.52",
"autoprefixer": "^10.4.17",
"esbuild": "^0.20.0",
"postcss": "^8.4.33",
"typescript": "^5.3.3"
}
Expand Down
Loading

0 comments on commit 7edb761

Please sign in to comment.