code cleaning and performances improvements
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Paul 2020-12-27 15:07:06 +01:00
parent ef3b816729
commit cca6c96626
4 changed files with 29 additions and 21 deletions

View File

@ -19,8 +19,10 @@ var version string
func main() { func main() {
var cfg config.Config var cfg config.Config
cfg.GetConfig() cfg.GetConfig()
cfg.Version = version cfg.Version = version
// Generate static files embedding
PackrGen() PackrGen()
err := database.Initialize(&cfg) err := database.Initialize(&cfg)
@ -31,15 +33,16 @@ func main() {
err = qrz.InitCronConfig(cfg) err = qrz.InitCronConfig(cfg)
if err != nil { if err != nil {
log.Fatalln(err) log.Println(err)
} }
err = qrzws.RunServer(cfg) err = qrzws.RunServer(cfg)
if err != nil { if err != nil {
log.Fatalln(err) log.Println(err)
} }
} }
// PackrGen act as an assets bundle maker
func PackrGen() { func PackrGen() {
packr.New("static", "../../static") packr.New("static", "../../static")
packr.New("templates", "../../templates") packr.New("templates", "../../templates")

View File

@ -24,37 +24,41 @@ func InitCronConfig(config config.Config) (err error) {
// Run runs the main loop // Run runs the main loop
func Run(config config.Config) (err error) { func Run(config config.Config) (err error) {
for _, group := range config.QrzGroups {
url := fmt.Sprintf(config.URLBase, group)
fmt.Println(fmt.Sprintf("Processing the %s group", group))
bodyStr, err := getBody(url)
if err != nil {
log.Fatalln(err)
}
frsPeople, err := getFrsEntries(config, bodyStr)
if err != nil {
log.Fatalln(err)
}
existingQrz, err := getCurrentEntries(config) existingQrz, err := getCurrentEntries(config)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
for _, group := range config.QrzGroups {
url := fmt.Sprintf(config.URLBase, group)
log.Println(fmt.Sprintf("Processing the %s group with URL %s", group, url))
bodyStr, err := getBody(url)
if err != nil {
log.Println(err)
break
}
frsPeople, err := getFrsEntries(config, bodyStr)
if err != nil {
log.Println(err)
break
}
err = discardExistingEntries(config, &frsPeople, existingQrz) err = discardExistingEntries(config, &frsPeople, existingQrz)
if err != nil { if err != nil {
log.Fatalln(err) log.Println(err)
continue
} }
if len(frsPeople) > 0 { if len(frsPeople) > 0 {
err = insertFrsEntryToDB(config, frsPeople) err = insertFrsEntryToDB(config, frsPeople)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
continue
} }
} else { } else {
fmt.Println( log.Println(
fmt.Sprintf("No entry to insert in database for group %s", fmt.Sprintf("No entry to insert in database for group %s",
group)) group))
} }
@ -153,7 +157,7 @@ func insertFrsEntryToDB(config config.Config, frsPeople map[string]Qrz) (err err
var qrzNum int var qrzNum int
fmt.Println( log.Println(
fmt.Sprintf("Starting insert of %d entries", fmt.Sprintf("Starting insert of %d entries",
len(frsPeople))) len(frsPeople)))
@ -172,7 +176,7 @@ func insertFrsEntryToDB(config config.Config, frsPeople map[string]Qrz) (err err
if err != nil { if err != nil {
return err return err
} }
fmt.Println(fmt.Sprintf("Committed %d qrz entries", qrzNum)) log.Println(fmt.Sprintf("Committed %d qrz entries", qrzNum))
return nil return nil
} }

View File

@ -69,23 +69,24 @@ func Run(c echo.Context, config config.Config) (res QrzDatatableOutput, err erro
rows, err := BuildQuery(config, qrzinputjson) rows, err := BuildQuery(config, qrzinputjson)
if err != nil { if err != nil {
fmt.Println(err)
return return
} }
if len(rows) > 0 {
for _, b := range rows { for _, b := range rows {
res.Data = append(res.Data, qrz.ToSlice(b)) res.Data = append(res.Data, qrz.ToSlice(b))
} }
} else {
res.Data = append(res.Data, []string{"", "", "", "", "", ""})
}
res.RecordsFiltered, err = BuildQCntFil(config, qrzinputjson) res.RecordsFiltered, err = BuildQCntFil(config, qrzinputjson)
if err != nil { if err != nil {
fmt.Println(err, "filtered")
return return
} }
res.RecordsTotal, err = BuildQCntTot(config, qrzinputjson) res.RecordsTotal, err = BuildQCntTot(config, qrzinputjson)
if err != nil { if err != nil {
fmt.Println(err, "total")
return return
} }

View File

@ -3,10 +3,10 @@
package templates package templates
import ( import (
"fmt"
"html/template" "html/template"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"strings" "strings"
@ -50,7 +50,7 @@ func BuildTemplatesDir(dir string) (builttemplates *Template, err error) {
freader, _ := pkger.Open(path) freader, _ := pkger.Open(path)
tmplContent, err := ioutil.ReadAll(freader) tmplContent, err := ioutil.ReadAll(freader)
tmpl.New(info.Name()).Parse(string(tmplContent)) tmpl.New(info.Name()).Parse(string(tmplContent))
fmt.Println(info.Name(), tmplContent) log.Println(info.Name(), tmplContent)
return err return err
}) })