Skip to content

Commit c1caaf4

Browse files
committed
resolve rackcash#1
1 parent ebd2248 commit c1caaf4

File tree

22 files changed

+242
-574
lines changed

22 files changed

+242
-574
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ tests/
1414
c2.toml
1515
config.toml
1616
scripts/
17+
examples/telegram-bot/node_modules/
1718
ns-restart.sh
1819
start_dev.sh
1920
infra/
21+
docker/
22+
deploy/

Makefile

+9-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ ARGS := $(wordlist 2, $(words $(MAKECMDGOALS)), $(MAKECMDGOALS))
99
WEB_CONFIG = ../config.toml
1010
CURRENCY_CONFIG = ../config.toml
1111
BOT_CONFIG = ./config.toml
12+
1213
LOGGER_ENV = ../.env
1314
WEB_ENV = ../.env
15+
BLOCKCHAIN_ENV=../.env
16+
17+
1418
SECRETS = ../secrets
1519
BOT_SECRETS = ./secrets
1620
LOCALES = ./locales
@@ -25,12 +29,15 @@ run_bot:
2529
# CONFIG=$(BOT_CONFIG) SECRETS=$(BOT_SECRETS) LOCALES=$(LOCALES) tsx rackBot/index.ts
2630
CONFIG=$(BOT_CONFIG) SECRETS=$(BOT_SECRETS) LOCALES=$(LOCALES) bun rackBot/index.ts
2731
run_currency:
28-
cd blockchain && SECRETS=$(SECRETS) CONFIG=$(CURRENCY_CONFIG) go run .
32+
cd blockchain && ENVPATH=$(BLOCKCHAIN_ENV) go run .
2933
run_logger:
3034
# killall racklog || true
3135
cd logger && ENVPATH=$(LOGGER_ENV) go run . &
3236
run_web:
33-
cd api && TZ=UTC+3 SECRETS=$(SECRETS) CONFIG=$(WEB_CONFIG) ENVPATH=$(WEB_ENV) go run .
37+
cd api && TZ=UTC+3 ENVPATH=$(WEB_ENV) go run .
38+
39+
build_images:
40+
echo hi
3441

3542
# parseable:
3643
# docker run -p 8000:8000 \

api/Dockerfile

+7-11
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,23 @@ FROM golang:1.22-alpine AS dev
22

33
WORKDIR /root/app
44

5-
ARG LOGGER_PATH=rackLogger
6-
ARG LOGGER_BIN=logger
5+
ARG LOGGER_PATH=logger
6+
ARG PKG_PATH=pkg
7+
ARG LOGGER_BIN=logger_bin
78

89
ARG WEB_PATH=api
910
ARG WEB_BIN=web
1011

11-
ARG SECRETS=secrets
12-
ENV SECRETS=${SECRETS}
13-
1412
COPY ${WEB_PATH}/ ./${WEB_PATH}/
1513
COPY ${LOGGER_PATH}/ ./${LOGGER_PATH}/
16-
COPY config.toml .
14+
COPY ${PKG_PATH}/ ./${PKG_PATH}/
1715
COPY .env .
1816

19-
ENV ENVPATH=./.env
20-
ENV CONFIG=./config.toml
17+
RUN go mod init infra
18+
RUN go mod tidy
2119

2220
RUN cd ${LOGGER_PATH} && go build -o ${LOGGER_BIN} && cd - && mv ${LOGGER_PATH}/${LOGGER_BIN} ./
2321

2422
RUN cd ${WEB_PATH} && go build -o ${WEB_BIN} && cd - && mv ${WEB_PATH}/${WEB_BIN} ./
2523

26-
RUN mkdir rack && mv api/rack/frontend rack/ && mv api/rack/templates rack/
27-
28-
CMD ["/bin/sh", "-c", "./logger & ./web"]
24+
CMD ["/bin/sh", "-c", "./logger_bin & ./web"]

api/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
```sh
33
make parseable
44
bash scripts/init.sh
5-
```
5+
```

api/internal/config/config.go

+90-53
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,124 @@
11
package config
22

33
import (
4-
"fmt"
54
"os"
65
"strings"
7-
"time"
6+
"sync"
87

9-
"github.com/BurntSushi/toml"
8+
"github.com/kelseyhightower/envconfig"
109
"gorm.io/gorm"
1110
)
1211

1312
type Config struct {
14-
DB *gorm.DB
13+
DB *gorm.DB `ignored:"true"`
1514

16-
ProxyPath string `toml:"proxy_path"` // used in webhook-sender
17-
ProxyList []string `toml:"-"` // reads proxies from ProxyPath and fills it with
15+
// TODO: remove
16+
// ProxyPath string `toml:"proxy_path"` // used in webhook-sender
17+
// ProxyList []string `toml:"-"` // reads proxies from ProxyPath and fills it with
1818

19-
Prod_env bool
19+
Prod_env bool `envconfig:"PROD_ENV" required:"true"`
2020

21-
Telegram struct {
22-
Token string `toml:"token"`
21+
// Telegram struct {
22+
// Token string `toml:"token"`
23+
// }
24+
25+
Testing struct {
26+
Enabled bool `envconfig:"TESTING" required:"true"`
27+
TxConfirmDelay int `envconfig:"TESTING_TX_CONFIRM_DELAY" required:"true"`
28+
TxFinProcessingDelay int `envconfig:"TX_FIN_PROCESSING_DELAY" required:"true"`
29+
TxFinDelay int `envconfig:"TX_FIN_DELAY" required:"true"`
30+
}
31+
32+
Postgres struct {
33+
Host string `envconfig:"DB_HOST" required:"true"`
34+
User string `envconfig:"DB_USER" required:"true"`
35+
Password string `envconfig:"DB_PASSWORD" required:"true"`
36+
Db_name string `envconfig:"DB_NAME" required:"true"`
37+
Port uint16 `envconfig:"DB_PORT" required:"true"`
38+
Ssl_mode string `envconfig:"DB_SSL_MODE" required:"true"`
39+
}
40+
Nats struct {
41+
Servers string `envconfig:"NATS_SERVERS" required:"true"`
42+
// TomlServers []string `toml:"servers"`
43+
}
44+
45+
PrivateKey string `envconfig:"API_ADMIN_KEY" required:"true"`
46+
Api struct {
47+
Ipv4 string `envconfig:"API_IPV4" required:"true"`
48+
Proto string `envconfig:"API_PROTO" required:"true"`
2349
}
50+
}
51+
52+
type ConfigS struct {
53+
DB *gorm.DB `ignored:"true"`
54+
55+
// TODO: remove
56+
// ProxyPath string `toml:"proxy_path"` // used in webhook-sender
57+
// ProxyList []string `toml:"-"` // reads proxies from ProxyPath and fills it with
58+
59+
Prod_env bool `envconfig:"PROD_ENV" required:"true"`
60+
61+
// Telegram struct {
62+
// Token string `toml:"token"`
63+
// }
2464

2565
Testing struct {
26-
Enabled bool
27-
TxConfirmDelay time.Duration `toml:"tx_confirm_delay"`
28-
TxFinProcessingDelay time.Duration `toml:"tx_fin_processing_delay"`
29-
TxFinDelay time.Duration `toml:"tx_fin_delay"`
30-
} `toml:"testing"`
31-
32-
PrivateKey string `toml:"private_key"`
33-
Postgres struct {
34-
Host string
35-
User string
36-
Password string
37-
Db_name string
38-
Port uint16
39-
Ssl_mode string
66+
Enabled bool `envconfig:"TESTING" required:"true"`
67+
TxConfirmDelay int `envconfig:"TESTING_TX_CONFIRM_DELAY" required:"true"`
68+
TxFinProcessingDelay int `envconfig:"TX_FIN_PROCESSING_DELAY" required:"true"`
69+
TxFinDelay int `envconfig:"TX_FIN_DELAY" required:"true"`
70+
}
71+
72+
// PrivateKey string `toml:"private_key"`
73+
Postgres struct {
74+
Host string `envconfig:"DB_HOST" required:"true"`
75+
User string `envconfig:"DB_USER" required:"true"`
76+
Password string `envconfig:"DB_PASSWORD" required:"true"`
77+
Db_name string `envconfig:"DB_NAME" required:"true"`
78+
Port uint16 `envconfig:"DB_PORT" required:"true"`
79+
Ssl_mode string `envconfig:"DB_SSL_MODE" required:"true"`
4080
}
4181
Nats struct {
42-
Servers string
43-
TomlServers []string `toml:"servers"`
82+
Servers string `envconfig:"NATS_SERVERS" required:"true"`
83+
// TomlServers []string `toml:"servers"`
4484
}
4585
Api struct {
46-
Ipv4 string
47-
Proto string
48-
} `toml:"rack_web"`
86+
Ipv4 string `envconfig:"API_IPV4" required:"true"`
87+
Proto string `envconfig:"API_PROTO" required:"true"`
88+
}
4989
}
5090

51-
func ReadConfig() *Config {
52-
byte_config, err := os.ReadFile(os.Getenv("CONFIG"))
53-
if err != nil {
54-
panic(err)
55-
}
91+
var once sync.Once
5692

93+
func ReadConfig() *Config {
5794
var config Config
58-
_, err = toml.Decode(string(byte_config), &config)
59-
if err != nil {
60-
panic(err)
61-
}
6295

63-
fmt.Println("PATH", config.ProxyPath)
96+
once.Do(func() {
97+
if err := envconfig.Process("", &config); err != nil {
98+
panic(err)
99+
}
100+
})
64101

65-
user, err := os.ReadFile(os.Getenv("SECRETS") + "/nats-user.txt")
66-
if err != nil {
67-
panic(err)
68-
}
102+
// user, err := os.ReadFile(os.Getenv("SECRETS") + "/nats-user.txt")
103+
// if err != nil {
104+
// panic(err)
105+
// }
69106

70-
pass, err := os.ReadFile(os.Getenv("SECRETS") + "/nats-password.txt")
71-
if err != nil {
72-
panic(err)
73-
}
107+
// pass, err := os.ReadFile(os.Getenv("SECRETS") + "/nats-password.txt")
108+
// if err != nil {
109+
// panic(err)
110+
// }
74111

75-
var formatedServers string
76-
for _, x := range config.Nats.TomlServers {
77-
connectUrl := fmt.Sprintf("nats://%s:%s@%s,", user, pass, string(x))
78-
formatedServers += connectUrl
79-
}
112+
// var formatedServers string
113+
// for _, x := range config.Nats.TomlServers {
114+
// connectUrl := fmt.Sprintf("nats://%s:%s@%s,", user, pass, string(x))
115+
// formatedServers += connectUrl
116+
// }
80117

81-
config.Nats.Servers = formatedServers
118+
// config.Nats.Servers = formatedServers
82119

83120
// webhook proxies
84-
config.ProxyList = GetProxyList(config.ProxyPath)
121+
// config.ProxyList = GetProxyList(config.ProxyPath)
85122

86123
if config.Prod_env && config.Testing.Enabled {
87124
panic("cannot use testing in prod")

api/internal/delivery/rest/v1/invoice_private.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ func (h *Handler) updateProxyList(c *gin.Context) {
1414

1515
fmt.Println("UPDATE PROXY LIST")
1616

17-
h.services.WebhookSender.UpdateList(config.GetProxyList(h.config.ProxyPath))
17+
// BUG: remove ""
18+
h.services.WebhookSender.UpdateList(config.GetProxyList(""))
1819
c.JSON(200, gin.H{
1920
"ok": true,
2021
})

api/internal/service/invoices.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (s *InvoicesService) RunCheck(ctx context.Context, cancel context.CancelFun
175175
Status: "paid",
176176
Paid: true,
177177
}
178-
time.Sleep(s.config.Testing.TxConfirmDelay * time.Second)
178+
time.Sleep(time.Duration(s.config.Testing.TxConfirmDelay) * time.Second)
179179
} else {
180180
// checks whether the client has sent crypto to temp wallet
181181
msg, err = s.n.ReqIsPaid(invoice, tempWalletAddr)
@@ -292,7 +292,7 @@ func (s *InvoicesService) RunCheck(ctx context.Context, cancel context.CancelFun
292292
// send withdrawal success msg
293293
s.l.Debug("SLEEP TX FIN PROCESSING")
294294

295-
time.Sleep(s.config.Testing.TxFinProcessingDelay * time.Second)
295+
time.Sleep(time.Duration(s.config.Testing.TxFinProcessingDelay) * time.Second)
296296

297297
s.l.Debug("NO SLEEP")
298298

api/internal/service/qr-code.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package service
22

33
import (
44
"bytes"
5-
"encoding/base64"
65
"fmt"
76
"infra/api/internal/infra/cache"
87
"infra/pkg/utils"
@@ -113,5 +112,5 @@ func generateQrCode(content string) (string, error) {
113112

114113
// os.WriteFile("qr.txt", []byte(base64.RawStdEncoding.EncodeToString(qrBytes)), os.ModePerm)
115114

116-
return base64.RawStdEncoding.EncodeToString(qrBytes), nil
115+
return string(qrBytes), nil
117116
}

api/internal/service/qr-code_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package service
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
)
8+
9+
func TestFindOrNew(t *testing.T) {
10+
11+
s := NewQrCodesService()
12+
13+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
14+
qr, err := s.FindOrNew("test")
15+
if err != nil {
16+
t.Error(err)
17+
}
18+
19+
w.Header().Add("Content-Type", "image/png")
20+
w.Write([]byte(qr))
21+
})
22+
23+
if err := http.ListenAndServe(":9999", nil); err != nil {
24+
fmt.Println("Error starting server:", err)
25+
}
26+
27+
// fmt.Println()
28+
29+
}

api/internal/service/service.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func HewServices(ns *natsdomain.Ns, db *gorm.DB, l logger.Logger, config *config
115115
balancesRepo := repository.InitBalancesRepo()
116116
lockerService := NewLockerService(cache.InitStorage())
117117

118-
webhookSender := NewWebhookSenderService(config.ProxyList, l)
118+
webhookSender := NewWebhookSenderService(nil, l)
119119

120120
invoiceService := NewInvoicesService(db, repository.InitInvoicesRepo(), walletsRepo, balancesRepo, lockerService, n, l, cache.InitStorage(), config)
121121

blockchain/Dockerfile

+9-13
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@ FROM golang:1.22-alpine AS dev
22

33
WORKDIR /root/app
44

5-
ARG CURRENCY_PATH=blockchain
6-
ARG CURRENCY_BIN=currency
5+
ARG BLOCKCHAIN_PATH=blockchain
6+
ARG BLOCKCHAIN_BIN=blockchain_bin
7+
ARG PKG_PATH=pkg
78

8-
ARG SECRETS=secrets
9-
ENV SECRETS=${SECRETS}
9+
COPY ${BLOCKCHAIN_PATH}/ ./${BLOCKCHAIN_PATH}/
10+
COPY ${PKG_PATH}/ ./${PKG_PATH}/
11+
RUN rm -r ${PKG_PATH}/protos
1012

11-
COPY ${CURRENCY_PATH}/ ./${CURRENCY_PATH}/
12-
COPY config.toml .
13-
COPY .env .
13+
RUN go mod init infra && go mod tidy
14+
RUN cd ${BLOCKCHAIN_PATH} && go build -o ${BLOCKCHAIN_BIN} && cd - && mv ${BLOCKCHAIN_PATH}/${BLOCKCHAIN_BIN} ./
1415

15-
ENV ENVPATH=./.env
16-
ENV CONFIG=./config.toml
17-
18-
RUN cd ${CURRENCY_PATH} && go build -o ${CURRENCY_BIN} && cd - && mv ${CURRENCY_PATH}/${CURRENCY_BIN} ./
19-
20-
CMD ["/bin/sh", "-c", "./currency"]
16+
CMD ["/bin/sh", "-c", "./blockchain_bin"]

0 commit comments

Comments
 (0)