logging improvements and ensure unique names

This commit is contained in:
pluja 2024-03-25 11:00:08 +01:00
parent 9f6b310676
commit 41448ea966
5 changed files with 35 additions and 31 deletions

View File

@ -1,11 +1,14 @@
package database package database
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"sort" "sort"
"strings"
"github.com/pluja/pocketbase" "github.com/pluja/pocketbase"
"github.com/rs/zerolog/log"
) )
type PbClient struct { type PbClient struct {
@ -49,7 +52,7 @@ func (p *PbClient) GetServices(filters, sort string) ([]Service, error) {
response, err := collection.List(params) response, err := collection.List(params)
if err != nil { if err != nil {
return nil, err return nil, errors.New("database error")
} }
return response.Items, nil return response.Items, nil
@ -67,7 +70,7 @@ func (p *PbClient) GetServiceByNameOrUrl(id string) (*Service, error) {
response, err := collection.List(params) response, err := collection.List(params)
if err != nil { if err != nil {
return nil, err return nil, errors.New("database error")
} }
if len(response.Items) > 0 { if len(response.Items) > 0 {
@ -85,7 +88,7 @@ func (p *PbClient) GetServiceByNameOrUrl(id string) (*Service, error) {
}) })
return &service, nil return &service, nil
} else { } else {
return nil, fmt.Errorf("Service not found.") return nil, errors.New("service not found")
} }
} }
@ -99,7 +102,7 @@ func (p *PbClient) GetServiceById(id string) (*Service, error) {
response, err := collection.OneWithParams(id, params) response, err := collection.OneWithParams(id, params)
if err != nil { if err != nil {
return nil, err return nil, errors.New("service not found")
} }
return &response, nil return &response, nil
@ -110,7 +113,11 @@ func (p *PbClient) CreateService(service Service) error {
_, err := collection.Create(service) _, err := collection.Create(service)
if err != nil { if err != nil {
return err if strings.Contains(err.Error(), "validation_not_unique") {
return errors.New("a service with this name already exsits, check out /pending page")
}
log.Debug().Err(err).Msg("could not create service")
return errors.New("could not create service")
} }
return nil return nil
@ -121,7 +128,7 @@ func (p *PbClient) UpdateService(id string, service Service) error {
err := collection.Update(id, service) err := collection.Update(id, service)
if err != nil { if err != nil {
return err return fmt.Errorf("could not update: %v", err)
} }
return nil return nil
@ -132,7 +139,7 @@ func (p *PbClient) GetAttribute(id string) (*Attribute, error) {
response, err := collection.One(id) response, err := collection.One(id)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("could not get service: %v", err)
} }
return &response, nil return &response, nil
@ -155,7 +162,7 @@ func (p *PbClient) GetAttributes(filters, sort string) ([]Attribute, error) {
response, err := collection.List(params) response, err := collection.List(params)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("could not list: %s", err)
} }
return response.Items, nil return response.Items, nil
@ -166,7 +173,7 @@ func (p *PbClient) CreateAttribute(attribute Attribute) error {
_, err := collection.Create(attribute) _, err := collection.Create(attribute)
if err != nil { if err != nil {
return err return fmt.Errorf("could not create attribute: %v", err)
} }
return nil return nil

View File

@ -53,7 +53,7 @@ func (s *Server) handleApiPicture(c iris.Context) {
service, err := database.Pb.GetServiceById(id) service, err := database.Pb.GetServiceById(id)
if err != nil || service.LogoURL == "" { if err != nil || service.LogoURL == "" {
log.Error().Err(err).Msg("GetPicture: Could not get service or logo URL is empty") log.Debug().Err(err).Msg("GetPicture: Could not get service or logo URL is empty")
respondWithPlaceholder(c, service.Name) respondWithPlaceholder(c, service.Name)
return return
} }
@ -76,7 +76,7 @@ func (s *Server) handleApiPicture(c iris.Context) {
bodyBytes, _ := io.ReadAll(resp.Body) bodyBytes, _ := io.ReadAll(resp.Body)
if len(bodyBytes) == 0 { if len(bodyBytes) == 0 {
log.Error().Msg("GetPicture: Empty response body") log.Debug().Msg("GetPicture: Empty response body")
respondWithPlaceholder(c, service.Name) respondWithPlaceholder(c, service.Name)
return return
} }

View File

@ -13,7 +13,7 @@ func (s *Server) handleApiService(c iris.Context) {
service, err := database.Pb.GetServiceByNameOrUrl(serviceName) service, err := database.Pb.GetServiceByNameOrUrl(serviceName)
if err != nil { if err != nil {
log.Error().Err(err).Msgf("Could not get service %v from database", serviceName) log.Debug().Err(err).Msgf("Could not get service %v from database", serviceName)
c.HTML("<h3>%s</h3>", err.Error()) c.HTML("<h3>%s</h3>", err.Error())
return return
} }

View File

@ -3,6 +3,7 @@ package server
import ( import (
"fmt" "fmt"
"math/rand" "math/rand"
"net/url"
"strings" "strings"
"github.com/kataras/iris/v12" "github.com/kataras/iris/v12"
@ -65,7 +66,7 @@ func (s *Server) handleIndex(c iris.Context) {
) )
if err != nil { if err != nil {
log.Error().Err(err).Msg("Could not get services from Pocketbase") log.Debug().Err(err).Msg("Could not get services from Pocketbase")
c.HTML("<h3>%s</h3>", err.Error()) c.HTML("<h3>%s</h3>", err.Error())
return return
} }
@ -100,7 +101,7 @@ func (s *Server) handleService(c iris.Context) {
service, err := database.Pb.GetServiceByNameOrUrl(serviceName) service, err := database.Pb.GetServiceByNameOrUrl(serviceName)
if err != nil { if err != nil {
log.Error().Err(err).Msgf("Could not get service %v from database", serviceName) log.Debug().Err(err).Msgf("Could not get service %v from database", serviceName)
c.HTML("<h3>%s</h3>", err.Error()) c.HTML("<h3>%s</h3>", err.Error())
return return
} }
@ -130,7 +131,7 @@ func (s *Server) handleScoreSummary(c iris.Context) {
service, err := database.Pb.GetServiceByNameOrUrl(serviceName) service, err := database.Pb.GetServiceByNameOrUrl(serviceName)
if err != nil { if err != nil {
log.Error().Err(err).Msgf("Could not get service %v from database", serviceName) log.Debug().Err(err).Msgf("Could not get service %v from database", serviceName)
c.HTML("<h3>%s</h3>", err.Error()) c.HTML("<h3>%s</h3>", err.Error())
return return
} }
@ -146,7 +147,7 @@ func (s *Server) handleAttribute(c iris.Context) {
// Get service from database by name // Get service from database by name
attribute, err := database.Pb.GetAttribute(attributeId) attribute, err := database.Pb.GetAttribute(attributeId)
if err != nil { if err != nil {
log.Error().Err(err).Msgf("Could not get attribute %v from database", attributeId) log.Debug().Err(err).Msgf("Could not get attribute %v from database", attributeId)
c.HTML("<h3>%s</h3>", err.Error()) c.HTML("<h3>%s</h3>", err.Error())
return return
} }
@ -157,7 +158,7 @@ func (s *Server) handleAttribute(c iris.Context) {
"-score", "-score",
) )
if err != nil { if err != nil {
log.Error().Err(err).Msgf("Could not get services with attribute %v from database", attributeId) log.Debug().Err(err).Msgf("Could not get services with attribute %v from database", attributeId)
c.HTML("<h3>%s</h3>", err.Error()) c.HTML("<h3>%s</h3>", err.Error())
return return
} }
@ -183,7 +184,7 @@ func (s *Server) handleRequestServiceForm(c iris.Context) {
attributes, err := database.Pb.GetAttributes("", "-rating") attributes, err := database.Pb.GetAttributes("", "-rating")
if err != nil { if err != nil {
log.Error().Err(err).Msg("Could not get attributes from database") log.Debug().Err(err).Msg("Could not get attributes from database")
c.HTML("<h3>%s</h3>", err.Error()) c.HTML("<h3>%s</h3>", err.Error())
return return
} }
@ -236,7 +237,7 @@ func (s *Server) handleNostr(c iris.Context) {
func (s *Server) handlePending(c iris.Context) { func (s *Server) handlePending(c iris.Context) {
services, err := database.Pb.GetServices("pending=true", "score") services, err := database.Pb.GetServices("pending=true", "score")
if err != nil { if err != nil {
log.Error().Err(err).Msg("Could not get services from database") log.Debug().Err(err).Msg("Could not get services from database")
c.HTML("<h3>%s</h3>", err.Error()) c.HTML("<h3>%s</h3>", err.Error())
return return
} }
@ -278,26 +279,26 @@ func (s *Server) handlePostRequestServiceForm(c iris.Context) {
log.Debug().Msg("Handling request service form") log.Debug().Msg("Handling request service form")
var data RequestFormData var data RequestFormData
if err := c.ReadForm(&data); err != nil { if err := c.ReadForm(&data); err != nil {
log.Error().Err(err).Msg("Could not parse form data") log.Debug().Err(err).Msg("Could not parse form data")
c.Redirect("/request/service?error=Invalid%20Form", iris.StatusSeeOther) c.Redirect("/request/service?error=Invalid%20Form", iris.StatusSeeOther)
return return
} }
log.Printf("Nonce: %v, ID: %v", data.PowNonce, data.PowId) log.Printf("Nonce: %v, ID: %v", data.PowNonce, data.PowId)
if !s.PowChallenger.PowVerifyProof(data.PowId, data.PowNonce) { if !s.PowChallenger.PowVerifyProof(data.PowId, data.PowNonce) {
log.Error().Msg("Invalid PoW") log.Debug().Msg("Invalid PoW")
c.Redirect("/request/service?error=Invalid%20Captcha", iris.StatusSeeOther) c.Redirect("/request/service?error=Invalid%20Captcha", iris.StatusSeeOther)
return return
} }
if data.KYCLevel < 0 || data.KYCLevel >= 4 { if data.KYCLevel < 0 || data.KYCLevel >= 4 {
log.Error().Msgf("Invalid KYC Level value: %v", c.FormValue("kyc_level")) log.Debug().Msgf("Invalid KYC Level value: %v", c.FormValue("kyc_level"))
c.Redirect("/request/service?error=Invalid%20KYC%20Level", iris.StatusSeeOther) c.Redirect("/request/service?error=Invalid%20KYC%20Level", iris.StatusSeeOther)
return return
} }
if len(data.Attributes) < 1 { if len(data.Attributes) < 1 {
log.Error().Msgf("Invalid number of attributes: %v", len(data.Attributes)) log.Debug().Msgf("Invalid number of attributes: %v", len(data.Attributes))
c.Redirect("/request/service?error=You%20must%20select%20at%20least%203%20attributes", iris.StatusSeeOther) c.Redirect("/request/service?error=You%20must%20select%20at%20least%203%20attributes", iris.StatusSeeOther)
return return
} }
@ -346,8 +347,8 @@ func (s *Server) handlePostRequestServiceForm(c iris.Context) {
// Save service to database // Save service to database
err := database.Pb.CreateService(service) err := database.Pb.CreateService(service)
if err != nil { if err != nil {
log.Error().Err(err).Msg("Could not save service to database") log.Debug().Err(err).Msg("Could not save service to database")
c.Redirect("/request/service?error=internal-error", iris.StatusSeeOther) c.Redirect(fmt.Sprintf("/request/service?error=%s", url.QueryEscape(err.Error())), iris.StatusSeeOther)
return return
} }

View File

@ -26,11 +26,7 @@ func InitTosScraperDaemon() {
trigerScraping() trigerScraping()
} }
for { for {
log.Info().Msgf("Next scraping in %v", duration)
if config.Conf.Dev {
//duration = 1 * time.Hour
log.Debug().Bool("DevMode", config.Conf.Dev).Msgf("Next scraping in %v", duration)
}
// Set the ticker for that duration // Set the ticker for that duration
ticker := time.NewTicker(duration) ticker := time.NewTicker(duration)
@ -58,7 +54,7 @@ func InitTosScraperDaemon() {
} }
func trigerScraping() { func trigerScraping() {
log.Debug().Msg("Starting scraper...") log.Info().Msg("Starting ToS analysis...")
// Get all the Services from the DB // Get all the Services from the DB
services, err := database.Pb.GetServices("pending=false && listed=true", "") services, err := database.Pb.GetServices("pending=false && listed=true", "")
if err != nil { if err != nil {