Skip to content

Commit 0074efe

Browse files
author
erokhinms
committed
init commit
0 parents  commit 0074efe

File tree

13 files changed

+17559
-0
lines changed

13 files changed

+17559
-0
lines changed

.github/workflows/exchanger.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Deploy to GitHub Pages
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
10+
build-and-deploy:
11+
runs-on: ubuntu-latest
12+
environment:
13+
name: github-pages
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- name: Install Node.js
19+
uses: actions/setup-node@v4
20+
with:
21+
node-version: 20
22+
- name: Update libs and build
23+
run: cd frontend && npm ci && npm run build
24+
25+
- name: Deploy
26+
uses: peaceiris/actions-gh-pages@v4
27+
with:
28+
github_token: ${{ secrets.GITHUB_TOKEN }}
29+
publish_dir: ./frontend/dist
30+
user_name: github-actions[bot]
31+
user_email: 41898282+github-actions[bot]@users.noreply.github.com

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
dist/

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "frontend"]
2+
path = frontend
3+
url = ./frontend

connectors/dex_connector.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package connectors
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
)
7+
8+
// DexConnector реализует работу с внешними DEX API
9+
10+
type DexConnector struct{}
11+
12+
func (d *DexConnector) GetFixedFloatCurrencies() ([]byte, error) {
13+
resp, err := PostCurrencies()
14+
if err != nil {
15+
log.Println("Ошибка при запросе FixedFloat:", err)
16+
return nil, err
17+
}
18+
return resp, nil
19+
}
20+
21+
func (d *DexConnector) GetCurrencies() ([]byte, error) {
22+
cryptoResp, err := d.GetFixedFloatCurrencies()
23+
if err != nil {
24+
return nil, err
25+
}
26+
27+
var cryptoObj map[string]any
28+
if err := json.Unmarshal(cryptoResp, &cryptoObj); err != nil {
29+
return nil, err
30+
}
31+
32+
return json.Marshal(cryptoObj)
33+
}

connectors/fixedfloat_api.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package connectors
2+
3+
import (
4+
"bytes"
5+
"crypto/hmac"
6+
"crypto/sha256"
7+
"encoding/hex"
8+
"io"
9+
"net/http"
10+
"os"
11+
)
12+
13+
func PostCurrencies() ([]byte, error) {
14+
url := "https://ff.io/api/v2/ccies"
15+
client := &http.Client{}
16+
17+
apiKey := os.Getenv("FIXEDFLOAT_API_KEY")
18+
apiSecret := os.Getenv("FIXEDFLOAT_API_SECRET")
19+
20+
jsonData := []byte("{}")
21+
22+
// HMAC SHA256 подпись
23+
h := hmac.New(sha256.New, []byte(apiSecret))
24+
h.Write(jsonData)
25+
sign := hex.EncodeToString(h.Sum(nil))
26+
27+
request, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
28+
if err != nil {
29+
return nil, err
30+
}
31+
request.Header.Set("Content-Type", "application/json; charset=UTF-8")
32+
request.Header.Set("Accept", "application/json")
33+
request.Header.Set("X-API-KEY", apiKey)
34+
request.Header.Set("X-API-SIGN", sign)
35+
36+
resp, err := client.Do(request)
37+
if err != nil {
38+
return nil, err
39+
}
40+
defer resp.Body.Close()
41+
return io.ReadAll(resp.Body)
42+
}

frontend

Submodule frontend added at 76133dc

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module xmr_mixer
2+
3+
go 1.24.3
4+
5+
require github.com/gorilla/websocket v1.5.3

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
2+
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=

logo.xcf

1.08 MB
Binary file not shown.

main.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"net/http"
7+
"runtime"
8+
"xmr_mixer/connectors"
9+
10+
"github.com/gorilla/websocket"
11+
)
12+
13+
var upgrader = websocket.Upgrader{
14+
CheckOrigin: func(r *http.Request) bool {
15+
return true // Разрешить любые Origin (НЕ использовать в продакшене!)
16+
},
17+
}
18+
19+
// CurrencyProvider определяет интерфейс для получения списка валют
20+
type CurrencyProvider interface {
21+
GetCurrencies() ([]byte, error)
22+
}
23+
24+
func avoidCORS(h http.HandlerFunc) http.HandlerFunc {
25+
return func(w http.ResponseWriter, r *http.Request) {
26+
w.Header().Set("Access-Control-Allow-Origin", "*")
27+
h(w, r)
28+
}
29+
}
30+
31+
func main() {
32+
http.HandleFunc("/api/test/", func(w http.ResponseWriter, r *http.Request) {
33+
w.Header().Set("Content-Type", "application/json")
34+
json.NewEncoder(w).Encode(map[string]bool{"answer": true})
35+
})
36+
http.HandleFunc("/ws", wsHandler)
37+
38+
// DexConnector как CurrencyProvider
39+
var provider CurrencyProvider = &connectors.DexConnector{}
40+
41+
http.HandleFunc("/api/v1/currencies", avoidCORS(func(w http.ResponseWriter, r *http.Request) {
42+
w.Header().Set("Content-Type", "application/json")
43+
resp, err := provider.GetCurrencies()
44+
if err != nil {
45+
w.WriteHeader(http.StatusInternalServerError)
46+
w.Write([]byte(`[]`))
47+
return
48+
}
49+
w.Write(resp)
50+
}))
51+
52+
log.Println("Сервер запущен на :8080")
53+
log.Fatal(http.ListenAndServe(":8080", nil))
54+
}
55+
56+
func wsHandler(w http.ResponseWriter, r *http.Request) {
57+
log.Printf("Origin: %s", r.Header.Get("Origin"))
58+
conn, err := upgrader.Upgrade(w, r, nil)
59+
if err != nil {
60+
log.Println("Ошибка апгрейда:", err)
61+
return
62+
}
63+
go handleWS(conn)
64+
}
65+
66+
func handleWS(conn *websocket.Conn) {
67+
defer conn.Close()
68+
for {
69+
_, msg, err := conn.ReadMessage()
70+
if err != nil {
71+
log.Println("Ошибка чтения:", err)
72+
break
73+
}
74+
log.Printf("Получено сообщение: %s", msg)
75+
76+
conn.WriteMessage(websocket.TextMessage, []byte("pong"))
77+
log.Printf("Текущее количество горутин: %d", runtime.NumGoroutine())
78+
}
79+
}

0 commit comments

Comments
 (0)