This commit is contained in:
Виталий Астахов 2020-08-01 14:39:30 +03:00
parent 50e482fbd4
commit 22c6b76104
6 changed files with 37 additions and 17 deletions

2
go.mod
View File

@ -1,3 +1,5 @@
module git.macaw.me/inhosin/vvod module git.macaw.me/inhosin/vvod
go 1.14 go 1.14
require github.com/BurntSushi/toml v0.3.1

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=

38
main.go
View File

@ -5,25 +5,25 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"os" "os"
"github.com/BurntSushi/toml"
) )
var ( var (
// TOKEN token config Setting
TOKEN = "08bb51ff-1eb6-4167-8ed2-eb19fc343703" configPath string
// MICROSERVICEURL url to microservie filePath string
MICROSERVICEURL = "http://microservice01:8081/api/v1/documents/create?crptDocType=LP_INTRODUCE_OST"
) )
// Hello ... func init() {
func Hello(name string) string { flag.StringVar(&configPath, "config-path", "./settings/settings.toml", "Path to config file")
if name == "" { flag.StringVar(&filePath, "file", "test.csv", "Path to file with some cis")
name = "World"
}
return fmt.Sprintf("Hello, %s", name)
} }
// ParseFile ... // ParseFile ...
@ -94,22 +94,23 @@ func SendToMicroservice(fileName string) error {
products = append(products, cis) products = append(products, cis)
} }
} }
document := NewDocument("122323", products) document := NewDocument(fileName, products)
data, err := json.Marshal(document) data, err := json.Marshal(document)
if err != nil { if err != nil {
return err return err
} }
req, err := http.NewRequest("POST", MICROSERVICEURL, bytes.NewBuffer(data)) req, err := http.NewRequest("POST", config.MicroServiceURL, bytes.NewBuffer(data))
if err != nil { if err != nil {
return err return err
} }
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
req.Header.Set("token", TOKEN) req.Header.Set("token", config.Token)
client := http.Client{} client := http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
fmt.Printf("Make request to %s\n with data %s\n", config.MicroServiceURL, string(data))
if err != nil { if err != nil {
return err return err
} }
@ -130,7 +131,7 @@ func MakePartFile(lines []string, fName string) error {
defer file.Close() defer file.Close()
for _, line := range lines { for _, line := range lines {
if _, err := file.WriteString(fmt.Sprintln(line)); err != nil { if _, err := file.WriteString(fmt.Sprintln(line[0:31])); err != nil {
return err return err
} }
} }
@ -139,9 +140,14 @@ func MakePartFile(lines []string, fName string) error {
} }
func main() { func main() {
Hello("") flag.Parse()
lines, err := ParseFile("codes.csv") _, err := toml.DecodeFile(configPath, &config)
if err != nil {
log.Fatal(err)
}
lines, err := ParseFile(filePath)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }

View File

@ -23,5 +23,6 @@ func NewDocument(docNum string, products []Cis) *Document {
// NewCis ... // NewCis ...
func NewCis(cis string) Cis { func NewCis(cis string) Cis {
return Cis{Ki: cis} shortCIS := cis[0:31]
return Cis{Ki: shortCIS}
} }

7
settings.go Normal file
View File

@ -0,0 +1,7 @@
package main
// Setting struct for config
type Setting struct {
Token string `toml:"TOKEN"`
MicroServiceURL string `toml:"MICROSERVICEURL"`
}

2
settings/settings.toml Normal file
View File

@ -0,0 +1,2 @@
TOKEN = "08bb51ff-1eb6-4167-8ed2-eb19fc343703"
MICROSERVICEURL = "http://microservice01:8081/api/v1/documents/create?crptDocType=LP_INTRODUCE_OST"