use log instead of logrus for logging

This commit is contained in:
Rodrigue Chakode 2021-09-19 22:49:48 +02:00
parent e2f5aba525
commit 2cb7818efe
4 changed files with 21 additions and 38 deletions

2
go.mod
View File

@ -4,7 +4,5 @@ go 1.13
require (
github.com/dpapathanasiou/go-recaptcha v0.0.0-20190121160230-be5090b17804
github.com/gorilla/mux v1.7.4
github.com/sirupsen/logrus v1.2.0
github.com/spf13/viper v1.7.0
)

2
go.sum
View File

@ -69,8 +69,6 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=

30
main.go
View File

@ -20,9 +20,8 @@ import (
"net/http"
"os"
"time"
"log"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
@ -47,7 +46,7 @@ var routes = Routes{
Route{
"Healthz",
"GET",
"/healthz",
"/",
http.HandlerFunc(Healthz),
},
}
@ -66,20 +65,6 @@ func MuxLoggerHandler(inner http.Handler, name string) http.Handler {
})
}
func NewRouter() *mux.Router {
router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
handler := MuxLoggerHandler(route.Handler, route.Name)
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
}
return router
}
func main() {
viper.AutomaticEnv()
viper.SetDefault("SERVER_TLS_CERT", "/etc/cert/cert.pem")
@ -95,12 +80,15 @@ func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Infof("Defaulting to port %s", port)
log.Printf("Defaulting to port %s", port)
}
router := NewRouter()
for _, route := range routes {
handler := MuxLoggerHandler(route.Handler, route.Name)
http.Handle(route.Pattern, handler)
}
log.Infof("Listening on port %s", port)
log.Printf("Listening on port %s", port)
log.Fatal(http.ListenAndServe(":"+port, router))
log.Fatal(http.ListenAndServe(":"+port, nil))
}

View File

@ -27,9 +27,9 @@ import (
"net/smtp"
"net/url"
"strings"
"log"
"github.com/dpapathanasiou/go-recaptcha"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
@ -89,13 +89,13 @@ func (m *SendMailRequest) Execute() error {
// On port 465 ssl connection is required from the very beginning (no starttls)
conn, err := tls.Dial("tcp", smtpServerAddr, tlsconfig)
if err != nil {
return fmt.Errorf("failed initiating smtp connection (%s)", err)
return fmt.Errorf("failed initiating smtp connection to host %s (%s)", smtpServerAddr, err)
}
defer conn.Close()
smtpClient, err := smtp.NewClient(conn, smtpServerHost)
if err != nil {
return fmt.Errorf("failed creating the smtp client (%s)", err)
return fmt.Errorf("failed creating smtp client to host %s (%s)", smtpServerHost, err)
}
defer smtpClient.Quit()
@ -163,14 +163,14 @@ func MuxSecAllowedDomainsHandler(next http.Handler) http.Handler {
if len(r.Header["Origin"]) == 0 || len(r.Header["Referer"]) == 0 {
rawHeader, _ := json.Marshal(r.Header)
log.Infoln("request with unexpected headers", string(rawHeader))
log.Println("request with unexpected headers", string(rawHeader))
w.WriteHeader(http.StatusForbidden)
return
}
reqOrigin := r.Header["Origin"][0]
if _, domainFound := allowedOrigins[reqOrigin]; !domainFound {
log.Errorln("not allowed origin", reqOrigin)
log.Println("not allowed origin", reqOrigin)
w.WriteHeader(http.StatusForbidden)
return
}
@ -193,9 +193,7 @@ func MuxSecReCaptchaHandler(next http.Handler) http.Handler {
result, err := recaptcha.Confirm(remoteIp, recaptchaResponse[0])
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Errorln("reCaptcha server error")
log.Println("reCaptcha server error:", err.Error())
w.WriteHeader(http.StatusForbidden)
return
}
@ -230,13 +228,14 @@ func SendMail(httpResp http.ResponseWriter, httpReq *http.Request) {
case "contact":
recipients = []string{viper.GetString("CONTACT_REPLY_BCC_EMAIL")}
default:
log.Infoln("not allowed request type:", contactRequest.RequestTarget)
log.Println("not allowed request type:", contactRequest.RequestTarget)
httpResp.WriteHeader(http.StatusForbidden)
httpResp.Write([]byte(`{"status": "error", "message": "unauthorized request"}`))
return
}
userData, _ := json.Marshal(contactRequest)
log.Infoln("New Request:", string(userData))
log.Println("New Request:", string(userData))
templateData := struct {
Name string
@ -280,7 +279,7 @@ func SendMail(httpResp http.ResponseWriter, httpReq *http.Request) {
if err == nil {
err := sendMailReq.Execute()
if err != nil {
log.Infof("error: %s", err.Error())
log.Println(err.Error())
contactResponse.Status = "error"
contactResponse.Message = fmt.Sprintf("An internal error occurred, please try later or send us an email at %s.", viper.GetString("CONTACT_REPLY_BCC_EMAIL"))
} else {
@ -292,14 +291,14 @@ func SendMail(httpResp http.ResponseWriter, httpReq *http.Request) {
}
}
} else {
log.Infof("error: %s", err.Error())
log.Println(err.Error())
contactResponse.Status = "error"
contactResponse.Message = "Invalid request, please review your input and try again."
}
refererURL, err := url.Parse(httpReq.Header.Get("Referer"))
if err != nil {
log.Infof("error: %s", err.Error())
log.Println(err.Error())
refererURL = &url.URL{} // continue with default (empty) url
}