From 5828b0c01661df44a39487d388fcd3231b8dd6dc Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Sat, 13 Nov 2021 12:58:33 +0100 Subject: [PATCH] misc updates --- ci-build.sh | 2 +- go.mod | 2 -- src/config/main.go | 8 +++++--- src/database/main.go | 18 +++++++++++++++--- src/qrz/main.go | 34 +++++++++++++++++++++++----------- src/qrzws/main.go | 6 +++++- src/static/main.go | 6 +++++- src/templates/main.go | 13 +++++++++++-- vendor/modules.txt | 4 ---- 9 files changed, 65 insertions(+), 28 deletions(-) diff --git a/ci-build.sh b/ci-build.sh index 9e75727..7a3de40 100755 --- a/ci-build.sh +++ b/ci-build.sh @@ -16,7 +16,7 @@ build() { echo "Drone tag set, let's do a release" VERSION=${DRONE_TAG} echo "${PROJECTNAME} ${VERSION}" > /build/VERSION - elif [[ ! -z ${DRONE_TAG} ]] + else then echo "Drone not set, let's only do a build" VERSION=${DRONE_COMMIT} diff --git a/go.mod b/go.mod index df3bb5a..14ac3cd 100644 --- a/go.mod +++ b/go.mod @@ -50,6 +50,4 @@ require ( github.com/syndtr/goleveldb v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.1 // indirect - golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect - golang.org/x/tools v0.1.2 // indirect ) diff --git a/src/config/main.go b/src/config/main.go index bccff33..8dab184 100644 --- a/src/config/main.go +++ b/src/config/main.go @@ -18,9 +18,9 @@ func (config *Config) GetConfig() error { flag.Usage = utils.Usage flag.StringVar(&configfile, "configfile", "qrz.ini", "config file to use with qrz section") - flag.IntVar(&port, "port", 8080, "web port to use") - flag.BoolVar(&nofeed, "nofeed", false, "no feed database table with entries at first launch") - flag.BoolVar(&debug, "debug", false, "if debug logging must be enabled") + flag.IntVar(&port, "port", 8080, "http port to use") + flag.BoolVar(&nofeed, "nofeed", false, "do not feed table with entries at first launch") + flag.BoolVar(&debug, "debug", false, "enable debug") flag.Parse() cfg, err := ini.Load(configfile) @@ -33,6 +33,7 @@ func (config *Config) GetConfig() error { config.Port = port config.NoFeed = nofeed config.Debug = debug + config.DbType = qrzsection.Key("db_type").MustString("postgres") config.DbHostname = qrzsection.Key("db_hostname").MustString("localhost") config.DbName = qrzsection.Key("db_name").MustString("database") config.DbUsername = qrzsection.Key("db_username").MustString("username") @@ -50,6 +51,7 @@ func (config *Config) GetConfig() error { // Config is the global config of qrz type Config struct { Db *xorm.Engine + DbType string DbHostname string DbName string DbUsername string diff --git a/src/database/main.go b/src/database/main.go index 5e34666..d4ca2b9 100644 --- a/src/database/main.go +++ b/src/database/main.go @@ -13,12 +13,24 @@ import ( // Initialize creates connection to database and exec Schema func Initialize(config *config.Config) (err error) { - config.Db, err = xorm.NewEngine("postgres", - fmt.Sprintf("postgres://%s:%s@%s/%s", + var connstr string + switch config.DbType { + case "mysql": + connstr = fmt.Sprintf("%s:%s@%s/%s", config.DbUsername, config.DbPassword, config.DbHostname, - config.DbName)) + config.DbName) + default: + // postgresql by default + connstr = fmt.Sprintf("%s://%s:%s@%s/%s", + config.DbType, + config.DbUsername, + config.DbPassword, + config.DbHostname, + config.DbName) + } + config.Db, err = xorm.NewEngine(config.DbType, connstr) if err != nil { log.Fatalln(err) } diff --git a/src/qrz/main.go b/src/qrz/main.go index b492ddc..8fa3249 100644 --- a/src/qrz/main.go +++ b/src/qrz/main.go @@ -33,7 +33,10 @@ func Run(config config.Config) (err error) { for _, group := range groups { url := fmt.Sprintf(config.URLBase, group) - log.Println(fmt.Sprintf("Processing the %s group with URL %s", group, url)) + log.Println( + fmt.Sprintf("Processing the %s group with URL %s", + group, + url)) bodyStr, err := getBody(url) if err != nil { @@ -75,8 +78,14 @@ func getGroups(urlbase string) (groups []string, err error) { clt := &http.Client{} resp, err := clt.Get(urlbase) - a, err := ioutil.ReadAll(resp.Body) - retstr := string(a) + if err != nil { + log.Println(err) + } + pagebody, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Println(err) + } + retstr := string(pagebody) b := re1.FindStringSubmatch(retstr) body := b[1] @@ -195,6 +204,9 @@ func insertFrsEntryToDB(config config.Config, frsPeople map[string]Qrz) (err err for _, frs := range frsPeople { _, err = session.Insert(&frs) + if err != nil { + log.Println(err) + } qrzNum++ if (qrzNum % 5) == 0 { err = session.Commit() @@ -239,12 +251,12 @@ func ToSlice(qrz Qrz) (out []string) { // Qrz describe FRS people type Qrz struct { ID int `db:"id" xorm:"pk autoincr"` - DMRID string `db:"dmrid" xorm:"varchar(50) notnull"` - QRZ string `db:"qrz" xorm:"varchar(50) notnull"` - Name string `db:"name" xorm:"varchar(50) notnull"` - Address string `db:"address" xorm:"varchar(50) notnull"` - City string `db:"city" xorm:"varchar(50) notnull"` - Zipcode string `db:"zipcode" xorm:"varchar(5) notnull"` - Dept string `db:"dept" xorm:"varchar(50) notnull"` - Country string `db:"country" xorm:"varchar(50) notnull"` + QRZ string `db:"qrz" xorm:"index notnull"` + DMRID string `db:"dmrid" xorm:"notnull"` + Name string `db:"name" xorm:"index notnull"` + Address string `db:"address" xorm:"notnull"` + City string `db:"city" xorm:"index notnull"` + Zipcode string `db:"zipcode" xorm:"index varchar(5) notnull"` + Dept string `db:"dept" xorm:"index notnull"` + Country string `db:"country" xorm:"index notnull"` } diff --git a/src/qrzws/main.go b/src/qrzws/main.go index 3f3e41a..7ec25f1 100644 --- a/src/qrzws/main.go +++ b/src/qrzws/main.go @@ -4,6 +4,7 @@ package qrzws import ( "fmt" + "log" "net/http" "regexp" "strings" @@ -25,7 +26,10 @@ func RunServer(config config.Config) (err error) { templatesbox := packr.New("templates", "../../templates") staticbox := packr.New("static", "../../static") - builtTemplates, _ := templates.BuildTemplates(templatesbox) + builtTemplates, err := templates.BuildTemplates(templatesbox) + if err != nil { + log.Println(err) + } e := echo.New() e.HideBanner = true diff --git a/src/static/main.go b/src/static/main.go index 1111c7d..46d0e4f 100644 --- a/src/static/main.go +++ b/src/static/main.go @@ -3,6 +3,7 @@ package static import ( + "log" "net/http" "strings" @@ -13,7 +14,10 @@ import ( // GetStatic returns static file content func GetStatic(box *packr.Box, c echo.Context) (err error) { name := c.Param("*") - cnt, _ := box.FindString(name) + cnt, err := box.FindString(name) + if err != nil { + log.Println(err) + } if strings.HasSuffix(name, ".js") { c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJavaScript) } else if strings.HasSuffix(name, ".css") { diff --git a/src/templates/main.go b/src/templates/main.go index 648bda6..f787ed5 100644 --- a/src/templates/main.go +++ b/src/templates/main.go @@ -28,7 +28,10 @@ func BuildTemplates(box *packr.Box) (builttemplates *Template, err error) { tmpl := template.New("templates") for _, filename := range box.List() { - tmplContent, _ := box.FindString(filename) + tmplContent, err := box.FindString(filename) + if err != nil { + log.Println(err) + } tmpl.New(filename).Parse(tmplContent) } @@ -47,12 +50,18 @@ func BuildTemplatesDir(dir string) (builttemplates *Template, err error) { if err != nil { return err } - freader, _ := pkger.Open(path) + freader, err := pkger.Open(path) + if err != nil { + log.Println(err) + } tmplContent, err := ioutil.ReadAll(freader) tmpl.New(info.Name()).Parse(string(tmplContent)) log.Println(info.Name(), tmplContent) return err }) + if err != nil { + log.Println(err) + } builttemplate := &Template{ templates: tmpl, diff --git a/vendor/modules.txt b/vendor/modules.txt index a5ab4a2..f03262b 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -146,8 +146,6 @@ golang.org/x/net/http2 golang.org/x/net/http2/h2c golang.org/x/net/http2/hpack golang.org/x/net/idna -# golang.org/x/sync v0.0.0-20210220032951-036812b2e83c -## explicit # golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf ## explicit; go 1.17 golang.org/x/sys/internal/unsafeheader @@ -179,8 +177,6 @@ golang.org/x/text/secure/bidirule golang.org/x/text/transform golang.org/x/text/unicode/bidi golang.org/x/text/unicode/norm -# golang.org/x/tools v0.1.2 -## explicit; go 1.17 # gopkg.in/ini.v1 v1.62.0 ## explicit gopkg.in/ini.v1