use log instead of logrus for logging
This commit is contained in:
parent
e2f5aba525
commit
2cb7818efe
2
go.mod
2
go.mod
|
@ -4,7 +4,5 @@ go 1.13
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/dpapathanasiou/go-recaptcha v0.0.0-20190121160230-be5090b17804
|
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
|
github.com/spf13/viper v1.7.0
|
||||||
)
|
)
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -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/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 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
|
||||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
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/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-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=
|
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
|
||||||
|
|
30
main.go
30
main.go
|
@ -20,9 +20,8 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -47,7 +46,7 @@ var routes = Routes{
|
||||||
Route{
|
Route{
|
||||||
"Healthz",
|
"Healthz",
|
||||||
"GET",
|
"GET",
|
||||||
"/healthz",
|
"/",
|
||||||
http.HandlerFunc(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() {
|
func main() {
|
||||||
viper.AutomaticEnv()
|
viper.AutomaticEnv()
|
||||||
viper.SetDefault("SERVER_TLS_CERT", "/etc/cert/cert.pem")
|
viper.SetDefault("SERVER_TLS_CERT", "/etc/cert/cert.pem")
|
||||||
|
@ -95,12 +80,15 @@ func main() {
|
||||||
port := os.Getenv("PORT")
|
port := os.Getenv("PORT")
|
||||||
if port == "" {
|
if port == "" {
|
||||||
port = "8080"
|
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))
|
||||||
}
|
}
|
||||||
|
|
25
sendmail.go
25
sendmail.go
|
@ -27,9 +27,9 @@ import (
|
||||||
"net/smtp"
|
"net/smtp"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/dpapathanasiou/go-recaptcha"
|
"github.com/dpapathanasiou/go-recaptcha"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"github.com/spf13/viper"
|
"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)
|
// On port 465 ssl connection is required from the very beginning (no starttls)
|
||||||
conn, err := tls.Dial("tcp", smtpServerAddr, tlsconfig)
|
conn, err := tls.Dial("tcp", smtpServerAddr, tlsconfig)
|
||||||
if err != nil {
|
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()
|
defer conn.Close()
|
||||||
|
|
||||||
smtpClient, err := smtp.NewClient(conn, smtpServerHost)
|
smtpClient, err := smtp.NewClient(conn, smtpServerHost)
|
||||||
if err != nil {
|
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()
|
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 {
|
if len(r.Header["Origin"]) == 0 || len(r.Header["Referer"]) == 0 {
|
||||||
rawHeader, _ := json.Marshal(r.Header)
|
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)
|
w.WriteHeader(http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
reqOrigin := r.Header["Origin"][0]
|
reqOrigin := r.Header["Origin"][0]
|
||||||
if _, domainFound := allowedOrigins[reqOrigin]; !domainFound {
|
if _, domainFound := allowedOrigins[reqOrigin]; !domainFound {
|
||||||
log.Errorln("not allowed origin", reqOrigin)
|
log.Println("not allowed origin", reqOrigin)
|
||||||
w.WriteHeader(http.StatusForbidden)
|
w.WriteHeader(http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -193,9 +193,7 @@ func MuxSecReCaptchaHandler(next http.Handler) http.Handler {
|
||||||
|
|
||||||
result, err := recaptcha.Confirm(remoteIp, recaptchaResponse[0])
|
result, err := recaptcha.Confirm(remoteIp, recaptchaResponse[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithFields(log.Fields{
|
log.Println("reCaptcha server error:", err.Error())
|
||||||
"error": err,
|
|
||||||
}).Errorln("reCaptcha server error")
|
|
||||||
w.WriteHeader(http.StatusForbidden)
|
w.WriteHeader(http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -230,13 +228,14 @@ func SendMail(httpResp http.ResponseWriter, httpReq *http.Request) {
|
||||||
case "contact":
|
case "contact":
|
||||||
recipients = []string{viper.GetString("CONTACT_REPLY_BCC_EMAIL")}
|
recipients = []string{viper.GetString("CONTACT_REPLY_BCC_EMAIL")}
|
||||||
default:
|
default:
|
||||||
log.Infoln("not allowed request type:", contactRequest.RequestTarget)
|
log.Println("not allowed request type:", contactRequest.RequestTarget)
|
||||||
httpResp.WriteHeader(http.StatusForbidden)
|
httpResp.WriteHeader(http.StatusForbidden)
|
||||||
|
httpResp.Write([]byte(`{"status": "error", "message": "unauthorized request"}`))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userData, _ := json.Marshal(contactRequest)
|
userData, _ := json.Marshal(contactRequest)
|
||||||
log.Infoln("New Request:", string(userData))
|
log.Println("New Request:", string(userData))
|
||||||
|
|
||||||
templateData := struct {
|
templateData := struct {
|
||||||
Name string
|
Name string
|
||||||
|
@ -280,7 +279,7 @@ func SendMail(httpResp http.ResponseWriter, httpReq *http.Request) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err := sendMailReq.Execute()
|
err := sendMailReq.Execute()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Infof("error: %s", err.Error())
|
log.Println(err.Error())
|
||||||
contactResponse.Status = "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"))
|
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 {
|
} else {
|
||||||
|
@ -292,14 +291,14 @@ func SendMail(httpResp http.ResponseWriter, httpReq *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Infof("error: %s", err.Error())
|
log.Println(err.Error())
|
||||||
contactResponse.Status = "error"
|
contactResponse.Status = "error"
|
||||||
contactResponse.Message = "Invalid request, please review your input and try again."
|
contactResponse.Message = "Invalid request, please review your input and try again."
|
||||||
}
|
}
|
||||||
|
|
||||||
refererURL, err := url.Parse(httpReq.Header.Get("Referer"))
|
refererURL, err := url.Parse(httpReq.Header.Get("Referer"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Infof("error: %s", err.Error())
|
log.Println(err.Error())
|
||||||
refererURL = &url.URL{} // continue with default (empty) url
|
refererURL = &url.URL{} // continue with default (empty) url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue