From 114465abf5b7aecfc9930f2ca7a5a919a07b543f Mon Sep 17 00:00:00 2001 From: nfel Date: Sat, 23 Nov 2024 16:44:45 +0330 Subject: [PATCH] Service init --- buf.gen.yaml | 25 ++ go.mod | 17 ++ go.sum | 20 ++ makefile | 46 ++++ src/cfg/cfg.go | 106 ++++++++ src/cfg/environment.go | 10 + src/cfg/log.go | 15 ++ src/cmd/serve.go | 16 ++ src/internal/domain/main.go | 5 + src/internal/domain/sms/kavenegar/main.go | 38 +++ src/main.go | 4 + src/ports/grpc/buf.gen.yaml | 15 ++ src/ports/grpc/buf.yaml | 9 + src/ports/grpc/main.go | 1 + src/ports/grpc/proto/.DS_Store | Bin 0 -> 6148 bytes src/ports/grpc/proto/README.md | 0 src/ports/grpc/proto/authorization.proto | 28 ++ .../grpc/proto/authorization_message.proto | 159 ++++++++++++ src/ports/grpc/proto/base_message.proto | 16 ++ src/ports/grpc/proto/wallet.proto | 29 +++ src/ports/grpc/proto/wallet_message.proto | 244 ++++++++++++++++++ src/ports/rest/main.go | 7 + src/ports/rpc/docs.go | 4 + 23 files changed, 814 insertions(+) create mode 100644 buf.gen.yaml create mode 100644 go.mod create mode 100644 go.sum create mode 100644 makefile create mode 100644 src/cfg/cfg.go create mode 100644 src/cfg/environment.go create mode 100644 src/cfg/log.go create mode 100644 src/cmd/serve.go create mode 100644 src/internal/domain/main.go create mode 100644 src/internal/domain/sms/kavenegar/main.go create mode 100644 src/main.go create mode 100644 src/ports/grpc/buf.gen.yaml create mode 100644 src/ports/grpc/buf.yaml create mode 100644 src/ports/grpc/main.go create mode 100644 src/ports/grpc/proto/.DS_Store create mode 100644 src/ports/grpc/proto/README.md create mode 100644 src/ports/grpc/proto/authorization.proto create mode 100644 src/ports/grpc/proto/authorization_message.proto create mode 100644 src/ports/grpc/proto/base_message.proto create mode 100644 src/ports/grpc/proto/wallet.proto create mode 100644 src/ports/grpc/proto/wallet_message.proto create mode 100644 src/ports/rest/main.go create mode 100644 src/ports/rpc/docs.go diff --git a/buf.gen.yaml b/buf.gen.yaml new file mode 100644 index 0000000..32e4268 --- /dev/null +++ b/buf.gen.yaml @@ -0,0 +1,25 @@ +--- +version: v2 +clean: true +managed: + enabled: true + override: + - file_option: go_package_prefix + value: alert/src/stub/go +plugins: + - local: protoc-gen-go-grpc + out: src/stub/go + opt: + - paths=source_relative + - require_unimplemented_servers=false + - local: protoc-gen-go + out: src/stub/go + opt: paths=source_relative + - local: protoc-gen-grpc-gateway + out: src/stub/go + opt: paths=source_relative + - local: protoc-gen-openapiv2 + out: src/stub/swag +inputs: + - git_repo: https://git.kahrobatoken.com/Kahroba/proto.git + branch: v2 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ced9675 --- /dev/null +++ b/go.mod @@ -0,0 +1,17 @@ +module alert + +go 1.23.2 + +require ( + github.com/BurntSushi/toml v1.4.0 + github.com/kavenegar/kavenegar-go v0.0.0-20240205151018-77039f51467d + github.com/spf13/cobra v1.8.1 + google.golang.org/protobuf v1.35.2 +) + +require ( + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect +) + +replace github.com/kavenegar/kavenegar-go v0.0.0-20240205151018-77039f51467d => github.com/nfel/kavenegar-go v1.0.3 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b3ebf29 --- /dev/null +++ b/go.sum @@ -0,0 +1,20 @@ +github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= +github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/nfel/kavenegar-go v1.0.3 h1:ux8YWhG0NmOa0dZpW8cL+qzKusoOewIiw5tufWececU= +github.com/nfel/kavenegar-go v1.0.3/go.mod h1:d+IJ22oTlPtjxl9DMQ4JgqVhnfW1qAfjxkc2UNy98ZY= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= +github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io= +google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/makefile b/makefile new file mode 100644 index 0000000..8f87889 --- /dev/null +++ b/makefile @@ -0,0 +1,46 @@ +.PHONY: all test clean build + +OUT=./tmp/main +GOOS=linux +GOARCH=amd64 +DOCKER_IMG= +DOCKER_REG= +all: tidy build run + +# Ignore +tidy: + @echo -ne "Formating Code \r" + @go fmt ./... + @rm $(OUT) + @go mod tidy -v + @go mod vendor + +test: + @echo -ne "Running Tests \r" + @go test ./... + +build-proto: + @echo -ne "Generating Proto Stubs\r" + @buf generate + +build-docker: + @echo -ne "Generating Docker Image\r" + @docker build . --no-cache -t $(DOCKER_IMG) + @docker tag $(DOCKER_IMG) $(DOCKER_REG)/$(DOCKER_IMG) + +build-bin: + @echo -ne "Building Binary \r" + @if [[ "$OSTYPE" == "linux-gnu" ]]; then\ + go build -o $(OUT) .;\ + else\ + env GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o $(OUT) .;\ + fi + @if ! [ -f "$(OUT)" ]; then\ + echo -ne "could'nt build the binary file. exit the process...";\ + exit 1;\ + fi + +run: + @./$(OUT) serve --conf ./cfg.toml + +build: build-proto build-bin diff --git a/src/cfg/cfg.go b/src/cfg/cfg.go new file mode 100644 index 0000000..30e10e4 --- /dev/null +++ b/src/cfg/cfg.go @@ -0,0 +1,106 @@ +package cfg + +import ( + "log" + "os" + + "github.com/BurntSushi/toml" +) + +var Cfg *Config + +type Config struct { + Version string `toml:"version" json:"version" yaml:"version"` + Environment CfgEnvironment `toml:"environment" json:"environment" yaml:"environment"` + GRPCConnectionTimeOutMinute int `toml:"grpc_connection_timeout_minute" json:"grpc_connection_time_out_minute" yaml:"grpc_connection_time_out_minute"` + SystemWalletAddress string `toml:"system_wallet_address" json:"system_wallet_address" yaml:"system_wallet_address"` + Peers Peer `toml:"peers" json:"peers" yaml:"peers"` + Log LogPath `toml:"log" json:"log" yaml:"log"` + LogLevel LogLevel `toml:"log-level" json:"log_level" yaml:"log_level"` + GRPC ConnectionCfg `toml:"grpc" json:"grpc" yaml:"grpc"` + REST ConnectionCfg `json:"rest" toml:"rest" yaml:"rest"` + DB DBCfg `toml:"db" json:"db" yaml:"db"` + Redis RedisCfg `toml:"redis" json:"redis" yaml:"redis"` + RabbitMQ RabbitMQCfg `toml:"rabbitmq" json:"rabbit_mq" yaml:"rabbit_mq"` + SystemEncryption EncryptionCfg `toml:"system_encryption" json:"system_encryption" yaml:"system_encryption"` + Kavenegar KavenegarCfg `toml:"kavenegar" json:"kavenegar" yaml:"kavenegar"` + MellatIPG IPGCfg `toml:"mellat-ipg" json:"mellat_ipg" yaml:"mellat_ipg"` + ReceiptURL string `toml:"receipt-url" json:"receipt_url" yaml:"receipt_url"` + HardCoded HardCodedCfg `json:"hard_coded" toml:"hard_coded" yaml:"hard_coded"` +} + +type HardCodedCfg struct { + BNPLIDUsedInRedeem uint `toml:"bnpl-id-used-in-redeem" json:"bnplid_used_in_redeem" yaml:"bnplid_used_in_redeem"` + NullBuyAndSellAssetPrice float64 `toml:"null-buy-and-sell-asset-price" json:"null_buy_and_sell_asset_price" yaml:"null_buy_and_sell_asset_price"` +} + +type IPGCfg struct { + // by default is "https://api.kahrobatoken.com/v1/public/wallet/confirm-sale" + BaseUrl string `toml:"base-url" json:"base_url" yaml:"base_url"` + TerminalId int64 `toml:"terminal-id" json:"terminal_id" yaml:"terminal_id"` + UserName string `toml:"username" json:"user_name" yaml:"user_name"` + Password string `toml:"password" json:"password" yaml:"password"` + CallbackUrl string `toml:"callback-url" json:"callback_url" yaml:"callback_url"` +} + +type KavenegarCfg struct { + BaseUrl string `toml:"base-url" json:"base_url" yaml:"base_url"` + ApiKey string `toml:"api-key" json:"api_key" yaml:"api_key"` + Sender string `toml:"sender" json:"sender" yaml:"sender"` + Template string `toml:"template" json:"template" yaml:"template"` +} + +type EncryptionCfg struct { + Public string `toml:"public" json:"public" yaml:"public"` + Private string `toml:"private" json:"private" yaml:"private"` +} + +type RabbitMQCfg struct { + Host string `toml:"host" json:"host" yaml:"host"` + Port int `toml:"port" json:"port" yaml:"port"` + User string `toml:"user" json:"user" yaml:"user"` + Password string `toml:"password" json:"password" yaml:"password"` +} + +type DBCfg struct { + Host string `toml:"host" json:"host" yaml:"host"` + Port int `toml:"port" json:"port" yaml:"port"` + Name string `toml:"name" json:"name" yaml:"name"` + User string `toml:"user" json:"user" yaml:"user"` + Password string `toml:"password" json:"password" yaml:"password"` +} + +type Peer struct { + Wallet string `json:"wallet" toml:"wallet" yaml:"wallet"` + Authorization string `toml:"authorization" json:"authorization" yaml:"authorization"` + Notification string `toml:"notification" json:"notification" yaml:"notification"` +} + +type ConnectionCfg struct { + Port int `toml:"port" json:"port" yaml:"port"` + Host string `toml:"host" json:"host" yaml:"host"` + TLS bool `toml:"tls" json:"tls" yaml:"tls"` +} +type RedisCfg struct { + Host string `toml:"host" json:"host" yaml:"host"` + Port int `toml:"port" json:"port" yaml:"port"` + User string `toml:"user" json:"user" yaml:"user"` + Password string `toml:"password" json:"password" yaml:"password"` + DB int `toml:"db" json:"db" yaml:"db"` +} +type LogPath struct { + Level string `toml:"level" json:"level" yaml:"level"` + ErrorPath string `toml:"error-path" json:"error_path" yaml:"error_path"` + InfoPath string `toml:"info-path" json:"info_path" yaml:"info_path"` + DebugPath string `toml:"debug-path" json:"debug_path" yaml:"debug_path"` +} + +func ParseConfig(path string, dest interface{}) { + content, err := os.ReadFile(path) + if err != nil { + log.Fatalln(err) + } + if _, err := toml.Decode(string(content), dest); err != nil { + log.Fatalln(err) + } +} diff --git a/src/cfg/environment.go b/src/cfg/environment.go new file mode 100644 index 0000000..11ab169 --- /dev/null +++ b/src/cfg/environment.go @@ -0,0 +1,10 @@ +package cfg + +type CfgEnvironment string + +const ( + Local CfgEnvironment = "local" + Development CfgEnvironment = "development" + Staging CfgEnvironment = "staging" + Production CfgEnvironment = "production" +) diff --git a/src/cfg/log.go b/src/cfg/log.go new file mode 100644 index 0000000..f59ef01 --- /dev/null +++ b/src/cfg/log.go @@ -0,0 +1,15 @@ +package cfg + +// LogLevel log level +type LogLevel int + +const ( + // Silent silent log level + Silent LogLevel = iota + 1 + // Error error log level + Error + // Warn warn log level + Warn + // Info info log level + Info +) diff --git a/src/cmd/serve.go b/src/cmd/serve.go new file mode 100644 index 0000000..72c91cc --- /dev/null +++ b/src/cmd/serve.go @@ -0,0 +1,16 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +var serveCmd = &cobra.Command{ + Use: "grpc", + Short: "Start the grpc server", + Run: func(cmd *cobra.Command, args []string) { + serveGrpc() + }, +} + +func serveGrpc() { +} diff --git a/src/internal/domain/main.go b/src/internal/domain/main.go new file mode 100644 index 0000000..360ebad --- /dev/null +++ b/src/internal/domain/main.go @@ -0,0 +1,5 @@ +package domain + +func Func() { + println("Hello") +} diff --git a/src/internal/domain/sms/kavenegar/main.go b/src/internal/domain/sms/kavenegar/main.go new file mode 100644 index 0000000..178e1cf --- /dev/null +++ b/src/internal/domain/sms/kavenegar/main.go @@ -0,0 +1,38 @@ +package kavenegar + +import ( + "fmt" + + "alert/src/cfg" + + "github.com/kavenegar/kavenegar-go" +) + +var api *kavenegar.Kavenegar + +func Init() { + _api := kavenegar.New(cfg.Cfg.Kavenegar.ApiKey) + api = _api +} + +func SendMsg() { + sender := "" + receptor := []string{"", ""} + message := "Hello Go!" + if res, err := api.Message.Send(sender, receptor, message, nil); err != nil { + switch err := err.(type) { + case *kavenegar.APIError: + fmt.Println(err.Error()) + case *kavenegar.HTTPError: + fmt.Println(err.Error()) + default: + fmt.Println(err.Error()) + } + } else { + for _, r := range res { + fmt.Println("MessageID = ", r.MessageID) + fmt.Println("Status = ", r.Status) + //... + } + } +} diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..da29a2c --- /dev/null +++ b/src/main.go @@ -0,0 +1,4 @@ +package main + +func main() { +} diff --git a/src/ports/grpc/buf.gen.yaml b/src/ports/grpc/buf.gen.yaml new file mode 100644 index 0000000..a3a6f37 --- /dev/null +++ b/src/ports/grpc/buf.gen.yaml @@ -0,0 +1,15 @@ +version: v2 +managed: + enabled: true +plugins: + - remote: buf.build/grpc/go + out: pb + opt: + - paths=source_relative + - require_unimplemented_servers=false + - remote: buf.build/protocolbuffers/go + out: pb + opt: + - paths=source_relative +inputs: + - directory: pb diff --git a/src/ports/grpc/buf.yaml b/src/ports/grpc/buf.yaml new file mode 100644 index 0000000..18ce987 --- /dev/null +++ b/src/ports/grpc/buf.yaml @@ -0,0 +1,9 @@ +version: v2 +lint: + use: + - DEFAULT +breaking: + use: + - FILE +modules: + - path: ./proto/pb diff --git a/src/ports/grpc/main.go b/src/ports/grpc/main.go new file mode 100644 index 0000000..21e034e --- /dev/null +++ b/src/ports/grpc/main.go @@ -0,0 +1 @@ +package grpc diff --git a/src/ports/grpc/proto/.DS_Store b/src/ports/grpc/proto/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0