diff --git a/Makefile b/Makefile index febfa9f..3ec5cfd 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ build: - go build -v ./cmd/apisirius + go build -v ./cmd/sirius run: - go run ./cmd/apisirius + go run ./cmd/sirius .DEFAULT_GOAL := build diff --git a/README.md b/README.md new file mode 100644 index 0000000..6f089c1 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ + +Онлайн конвертер конфураций ППКУП "Сириус" v1.03.022 (Болид) + +Предназначен для конвертации сохраненой конфигурации c ППКУП "Сириус" v1.03.022 в формате "json", в удобно читаемый формат файла типа exel, для последующей работы с ним (например создании инструкций). + +В выходном файле присутствуют поля. Номер зоны, Наименование зоны, Адреса прибора и шлейфы входящие в данную зону. + + +Запуск конвертации из командной строки и из файла + +./sirius --file --conv-path="./converts" --conv-file="sirius.json" + +Запуск API Сервера + +./sirius --api --conf-path="./configs" --conf-file="config.yml" diff --git a/cmd/apisirius/main.go b/cmd/apisirius/main.go deleted file mode 100644 index feacc79..0000000 --- a/cmd/apisirius/main.go +++ /dev/null @@ -1,27 +0,0 @@ -package main - -import ( - "flag" - "fmt" - - "gitstore.ru/tolikproh/sirius/internal/server" -) - -func main() { - flag.Parse() - - cfg, err := initConfig() - if err != nil { - fmt.Printf("Error initializing configs file (configs set default): %s\n", err.Error()) - } - - log := NewLogger(cfg) - - app := server.New(cfg, log) - - fmt.Printf("Start server on lister port: %s\n", cfg.Srv.Port) - - if err := app.Run(); err != nil { - log.Fatalf("%s", err.Error()) - } -} diff --git a/cmd/cli/main.go b/cmd/cli/main.go new file mode 100644 index 0000000..041d421 --- /dev/null +++ b/cmd/cli/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "flag" + "fmt" +) + +func main() { + + wordPtr := flag.String("word", "foo", "a string") + + numbPtr := flag.Int("numb", 42, "an int") + forkPtr := flag.Bool("api", false, "Start Api Server") + + var svar string + flag.StringVar(&svar, "svar", "bar", "a string var") + + flag.Parse() + + fmt.Println("word:", *wordPtr) + fmt.Println("numb:", *numbPtr) + fmt.Println("api:", *forkPtr) + fmt.Println("svar:", svar) + fmt.Println("tail:", flag.Args()) +} diff --git a/cmd/apisirius/config.go b/cmd/sirius/config.go similarity index 59% rename from cmd/apisirius/config.go rename to cmd/sirius/config.go index ef3b815..892cc7e 100644 --- a/cmd/apisirius/config.go +++ b/cmd/sirius/config.go @@ -8,13 +8,21 @@ import ( ) var ( - configPath string - configFile string = "config" + apiPtr *bool + configPath string + configFile string = "config" + filePtr *bool + filePathConv string + fileConv string = "sirius.json" ) func init() { + apiPtr = flag.Bool("api", false, "Start Api Server") flag.StringVar(&configPath, "conf-path", "configs", "path to config file 'config.yml'") flag.StringVar(&configFile, "conf-file", "config", "name file to config") + filePtr = flag.Bool("file", false, "File converted cli") + flag.StringVar(&filePathConv, "conv-path", "converts", "path to converts file 'sirius.json'") + flag.StringVar(&fileConv, "conv-file", "sirius.json", "name file to converting") } func initConfig() (*model.Config, error) { diff --git a/cmd/apisirius/logger.go b/cmd/sirius/logger.go similarity index 100% rename from cmd/apisirius/logger.go rename to cmd/sirius/logger.go diff --git a/cmd/sirius/main.go b/cmd/sirius/main.go new file mode 100644 index 0000000..dc59040 --- /dev/null +++ b/cmd/sirius/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "flag" + "fmt" + + "gitstore.ru/tolikproh/sirius/internal/controller/file" + "gitstore.ru/tolikproh/sirius/internal/server" +) + +func main() { + flag.Parse() + + if *apiPtr { + cfg, err := initConfig() + if err != nil { + fmt.Printf("Error initializing configs file (configs set default): %s\n", err.Error()) + } + + log := NewLogger(cfg) + + app := server.New(cfg, log) + + fmt.Printf("Start server on lister port: %s\n", cfg.Srv.Port) + + if err := app.Run(); err != nil { + log.Fatalf("%s", err.Error()) + } + return + } + + if *filePtr { + file.FileConvert(filePathConv + "/" + fileConv) + return + } + fmt.Println("For help, used flags --help") +} diff --git a/sirius.json b/converts/sirius.json similarity index 100% rename from sirius.json rename to converts/sirius.json diff --git a/init/sirius.service b/init/sirius.service new file mode 100644 index 0000000..4e1bb42 --- /dev/null +++ b/init/sirius.service @@ -0,0 +1,16 @@ +[Unit] +Description=Sirius +After=syslog.target +After=network.target +After=nginx.service + +[Service] +Type=simple +User=root +Group=root +WorkingDirectory=/opt/sirius +ExecStart=/opt/sirius/sirius --api +Restart=always + +[Install] +WantedBy=multi-user.target diff --git a/internal/controller/file/file.go b/internal/controller/file/file.go index 55eb610..60d0a17 100644 --- a/internal/controller/file/file.go +++ b/internal/controller/file/file.go @@ -1,75 +1,33 @@ package file import ( - "errors" - "net/http" + "os" + "path" + "strings" - "github.com/gin-gonic/gin" - "github.com/google/uuid" - "github.com/sirupsen/logrus" - "gitstore.ru/tolikproh/sirius/internal/model" "gitstore.ru/tolikproh/sirius/internal/service" ) -type Handler struct { - log *logrus.Logger - cfg *model.Config -} +func FileConvert(srcFilePathName string) error { -var ( - TEXT_TYPES = map[string]interface{}{ - "text/plain; charset=utf-8": nil, - } -) + srcDir, srcFileName := path.Split(srcFilePathName) -func New(cfg *model.Config, log *logrus.Logger) *Handler { - // HTTP Server - return &Handler{cfg: cfg, log: log} -} + dstFileName := strings.Split(srcFileName, ".")[0] + ".xlsx" -func (s *Handler) GinConvert(c *gin.Context) { - sessionId := uuid.New().String() - - s.log.Printf("start convert. session id: %s; user agent: %s; remote addr: %s; file size [byte]: %d", - sessionId, c.Request.UserAgent(), c.Request.RemoteAddr, c.Request.ContentLength) - - c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, s.cfg.Srv.MaxSizeFile<<20) - - file, fileHeader, err := c.Request.FormFile("file") + buffer, err := os.ReadFile(srcDir + srcFileName) if err != nil { - s.log.Errorf("session id: %s. error code: 001. %s", sessionId, err.Error()) - c.JSON(http.StatusBadRequest, gin.H{ - "error": err.Error(), - }) - return - } - defer file.Close() - - buffer := make([]byte, fileHeader.Size) - file.Read(buffer) - - fileType := http.DetectContentType(buffer) - - // Validate File Type - if _, ex := TEXT_TYPES[fileType]; !ex { - s.log.Errorf("session id: %s. error code: 002. formated data not text/plain", sessionId) - c.JSON(http.StatusBadRequest, gin.H{ - "error": errors.New("formated data not text/plain").Error(), - }) - return + return err } respBuff, err := service.SiriusConv(buffer) if err != nil { - s.log.Errorf("session id: %s. error code: 004. %s", sessionId, err.Error()) - c.JSON(http.StatusBadRequest, gin.H{ - "error": err.Error(), - }) - return + return err } - c.Data(200, "application/vnd.ms-excel", respBuff.Bytes()) - - s.log.Printf("session id: %s.converted: ok. filename: %s; file size: %d bytes", sessionId, fileHeader.Filename, fileHeader.Size) + err = os.WriteFile(srcDir+dstFileName, respBuff.Bytes(), 0644) + if err != nil { + return err + } + return nil }