refactor: replace standard logger with logrus
This commit is contained in:
parent
55c9698c74
commit
c64bbdf591
4
go.mod
4
go.mod
@ -4,11 +4,13 @@ go 1.18
|
||||
|
||||
require (
|
||||
github.com/PuerkitoBio/goquery v1.8.0
|
||||
github.com/allegro/bigcache/v3 v3.0.2
|
||||
github.com/gorilla/mux v1.8.0
|
||||
github.com/sirupsen/logrus v1.9.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/allegro/bigcache/v3 v3.0.2 // indirect
|
||||
github.com/andybalholm/cascadia v1.3.1 // indirect
|
||||
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8 // indirect
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||
)
|
||||
|
15
go.sum
15
go.sum
@ -4,12 +4,27 @@ github.com/allegro/bigcache/v3 v3.0.2 h1:AKZCw+5eAaVyNTBmI2fgyPVJhHkdWder3O9Irpr
|
||||
github.com/allegro/bigcache/v3 v3.0.2/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I=
|
||||
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
|
||||
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
||||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
|
||||
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8 h1:/6y1LfuqNuQdHAm0jjtPtgRcxIxjVZgm5OTu8/QhZvk=
|
||||
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
13
main.go
13
main.go
@ -10,12 +10,15 @@ import (
|
||||
|
||||
"github.com/allegro/bigcache/v3"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var logger = logrus.New()
|
||||
|
||||
func main() {
|
||||
c, err := bigcache.NewBigCache(bigcache.DefaultConfig(time.Hour * 2))
|
||||
c, err := bigcache.NewBigCache(bigcache.DefaultConfig(time.Hour * 24))
|
||||
if err != nil {
|
||||
fatal("can't initialize caching")
|
||||
logger.Fatalln("can't initialize caching")
|
||||
}
|
||||
cache = c
|
||||
|
||||
@ -42,10 +45,10 @@ func main() {
|
||||
|
||||
l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
logger.Fatalln(err)
|
||||
}
|
||||
|
||||
info(fmt.Sprintf("server is listening on port %d", port))
|
||||
logger.Infof("server is listening on port %d\n", port)
|
||||
|
||||
fatal(server.Serve(l))
|
||||
logger.Fatalln(server.Serve(l))
|
||||
}
|
||||
|
19
utils.go
19
utils.go
@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"path"
|
||||
"text/template"
|
||||
@ -13,7 +12,7 @@ import (
|
||||
|
||||
var cache *bigcache.BigCache
|
||||
|
||||
func setCache(key string, entry any) error {
|
||||
func setCache(key string, entry interface{}) error {
|
||||
data, err := json.Marshal(&entry)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -22,13 +21,13 @@ func setCache(key string, entry any) error {
|
||||
return cache.Set(key, data)
|
||||
}
|
||||
|
||||
func getCache(key string) (any, error) {
|
||||
func getCache(key string) (interface{}, error) {
|
||||
data, err := cache.Get(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var decoded any
|
||||
var decoded interface{}
|
||||
|
||||
if err = json.Unmarshal(data, &decoded); err != nil {
|
||||
return nil, err
|
||||
@ -37,14 +36,6 @@ func getCache(key string) (any, error) {
|
||||
return decoded, nil
|
||||
}
|
||||
|
||||
func fatal(err any) {
|
||||
log.Fatalf("[ERR] %s\n", err)
|
||||
}
|
||||
|
||||
func info(s string) {
|
||||
log.Printf("[INFO] %s\n", s)
|
||||
}
|
||||
|
||||
func write(w http.ResponseWriter, status int, data []byte) {
|
||||
w.WriteHeader(status)
|
||||
w.Write(data)
|
||||
@ -63,13 +54,13 @@ func securityHeaders(next http.Handler) http.Handler {
|
||||
func getTemplates(templates ...string) []string {
|
||||
var pths []string
|
||||
for _, t := range templates {
|
||||
tmpl := path.Join("views",fmt.Sprintf("%s.tmpl", t))
|
||||
tmpl := path.Join("views", fmt.Sprintf("%s.tmpl", t))
|
||||
pths = append(pths, tmpl)
|
||||
}
|
||||
return pths
|
||||
}
|
||||
|
||||
func render(n string, w http.ResponseWriter, data any) {
|
||||
func render(n string, w http.ResponseWriter, data interface{}) {
|
||||
t, err := template.ParseFiles(getTemplates(n, "navbar", "footer")...)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
|
Loading…
x
Reference in New Issue
Block a user