Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Run gofmt
Browse files Browse the repository at this point in the history
  • Loading branch information
GimignanoF committed Aug 6, 2019
1 parent 7428c90 commit 5e6b78a
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 114 deletions.
188 changes: 94 additions & 94 deletions elmahio.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,50 @@
package elmahio

import (
"bytes"
"encoding/json"
"errors"
"log"
"net/http"
"net/url"
"os"
"strconv"
"time"
"bytes"
"encoding/json"
"errors"
"log"
"net/http"
"net/url"
"os"
"strconv"
"time"
)

// Settings struct
// This will set the
// APIKey and the LogID
// for writing on Elmah.io
type Settings struct {
APIKey string
LogID string
Source string
Version float64
APIKey string
LogID string
Source string
Version float64
}

// Message struct
// This will be the message
// sent to Elmah.io
type Message struct {
Application string `json:"application,omitempty"`
Detail string `json:"detail,omitempty"`
Hostname string `json:"hostname,omitempty"`
Title string `json:"title,omitempty"`
Source string `json:"source,omitempty"`
StatusCode int `json:"statuscode,omitempty"`
DateTime time.Time `json:"datetime,omitempty"`
Type string `json:"type,omitempty"`
User string `json:"user,omitempty"`
Severity string `json:"severity,omitempty"`
URL string `json:"url,omitempty"`
Method string `json:"method,omitempty"`
Version string `json:"version,omitempty"`
Cookies map[string]string `json:"cookies,omitempty"`
Form map[string][]string `json:"form,omitempty"`
QueryString url.Values `json:"querystring,omitempty"`
ServerVariables map[string]string `json:"servervariables,omitempty"`
Data map[string]string `json:"data,omitempty"`
Application string `json:"application,omitempty"`
Detail string `json:"detail,omitempty"`
Hostname string `json:"hostname,omitempty"`
Title string `json:"title,omitempty"`
Source string `json:"source,omitempty"`
StatusCode int `json:"statuscode,omitempty"`
DateTime time.Time `json:"datetime,omitempty"`
Type string `json:"type,omitempty"`
User string `json:"user,omitempty"`
Severity string `json:"severity,omitempty"`
URL string `json:"url,omitempty"`
Method string `json:"method,omitempty"`
Version string `json:"version,omitempty"`
Cookies map[string]string `json:"cookies,omitempty"`
Form map[string][]string `json:"form,omitempty"`
QueryString url.Values `json:"querystring,omitempty"`
ServerVariables map[string]string `json:"servervariables,omitempty"`
Data map[string]string `json:"data,omitempty"`
}

// ElmahHandlerFunc type declaration
Expand All @@ -59,80 +59,80 @@ var settings *Settings

// Setup Elmah.io settings
func Setup(APIKey string, LogID string) error {
if len(APIKey) == 0 {
return errors.New("Please specify an API Key")
}
if len(LogID) == 0 {
return errors.New("Please specify a Log ID")
}
settings = &Settings{APIKey: APIKey, LogID: LogID}
return nil
if len(APIKey) == 0 {
return errors.New("Please specify an API Key")
}
if len(LogID) == 0 {
return errors.New("Please specify a Log ID")
}
settings = &Settings{APIKey: APIKey, LogID: LogID}
return nil
}

// SetVersion - Set the application version
func SetVersion(version float64) {
settings.Version = version
settings.Version = version
}

// SetSource - Set the application source
func SetSource(source string) {
settings.Source = source
settings.Source = source
}

// ElmahHandler middleware
// Will log the error message and send
// the details to Elmah.io
func ElmahHandler(handler ElmahHandlerFunc) http.Handler {
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
httpResponse, err := handler(response, request)
if err != nil {
log.Println(err.Error())
requestURL, urlErr := url.Parse(request.RequestURI)
if urlErr != nil {
log.Fatal(urlErr)
}
request.ParseForm()
requestQueryString, _ := url.ParseQuery(requestURL.RawQuery)
message := Message{
Hostname: requestURL.Host,
URL: requestURL.Path,
DateTime: time.Now(),
Severity: "Error",
StatusCode: httpResponse.StatusCode,
Type: err.Error(),
Method: request.Method,
User: requestURL.User.Username(),
QueryString: requestQueryString,
Application: os.Args[0],
Detail: err.Error(),
Title: err.Error(),
Cookies: make(map[string]string, len(request.Cookies())),
Form: make(map[string][]string, len(request.Form)),
Source: "",
Version: "",
}
if settings.Version > 0.0 {
message.Version = strconv.FormatFloat(settings.Version, 'f', -1, 64)
}
if len(settings.Source) > 0 {
message.Source = settings.Source
}
if len(request.Cookies()) > 0 {
for _, cookie := range request.Cookies() {
message.Cookies[cookie.Name] = cookie.Value
}
}
if len(request.Form) > 0 {
for key, value := range request.Form {
message.Form[key] = value
}
}
messageBytes := new(bytes.Buffer)
json.NewEncoder(messageBytes).Encode(message)
_, err = http.Post("https://api.elmah.io/v3/messages/"+settings.LogID+"?api_key="+settings.APIKey, "application/json; charset=utf-8", messageBytes)
if err != nil {
log.Fatal(err.Error())
}
}
})
}
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
httpResponse, err := handler(response, request)
if err != nil {
log.Println(err.Error())
requestURL, urlErr := url.Parse(request.RequestURI)
if urlErr != nil {
log.Fatal(urlErr)
}
request.ParseForm()
requestQueryString, _ := url.ParseQuery(requestURL.RawQuery)
message := Message{
Hostname: requestURL.Host,
URL: requestURL.Path,
DateTime: time.Now(),
Severity: "Error",
StatusCode: httpResponse.StatusCode,
Type: err.Error(),
Method: request.Method,
User: requestURL.User.Username(),
QueryString: requestQueryString,
Application: os.Args[0],
Detail: err.Error(),
Title: err.Error(),
Cookies: make(map[string]string, len(request.Cookies())),
Form: make(map[string][]string, len(request.Form)),
Source: "",
Version: "",
}
if settings.Version > 0.0 {
message.Version = strconv.FormatFloat(settings.Version, 'f', -1, 64)
}
if len(settings.Source) > 0 {
message.Source = settings.Source
}
if len(request.Cookies()) > 0 {
for _, cookie := range request.Cookies() {
message.Cookies[cookie.Name] = cookie.Value
}
}
if len(request.Form) > 0 {
for key, value := range request.Form {
message.Form[key] = value
}
}
messageBytes := new(bytes.Buffer)
json.NewEncoder(messageBytes).Encode(message)
_, err = http.Post("https://api.elmah.io/v3/messages/"+settings.LogID+"?api_key="+settings.APIKey, "application/json; charset=utf-8", messageBytes)
if err != nil {
log.Fatal(err.Error())
}
}
})
}
38 changes: 19 additions & 19 deletions examples/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@
package main

import (
"errors"
"net/http"
"errors"
"net/http"

elmahio "github.com/jimiit92/elmahio-go"
"github.com/gorilla/mux"
"github.com/gorilla/mux"
elmahio "github.com/jimiit92/elmah.io-go"
)

// Application entry point
func main() {
err := elmahio.Setup("Your-API-Key", "LogId")
elmahio.SetVersion(1.0)
elmahio.SetSource("TestApp")
if err != nil {
panic(err.Error())
}
rtr := mux.NewRouter()
rtr.Handle("/error", elmahio.ElmahHandler(handler)).Methods("GET")
http.ListenAndServe(":8080", rtr)
err := elmahio.Setup("Your-API-Key", "LogId")
elmahio.SetVersion(1.0)
elmahio.SetSource("TestApp")
if err != nil {
panic(err.Error())
}
rtr := mux.NewRouter()
rtr.Handle("/error", elmahio.ElmahHandler(handler)).Methods("GET")
http.ListenAndServe(":8080", rtr)
}

// Handler for the /error route
// Will throw an error that will be logged on Elmah.io
func handler(w http.ResponseWriter, r *http.Request) (*http.Response, error) {
err := errors.New("Hello from Go")
response := http.Response{
StatusCode: 400,
}
return &response, err
}
err := errors.New("Hello from Go")
response := http.Response{
StatusCode: 400,
}
return &response, err
}
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module testelmah
go 1.12

require (
github.com/jimiit92/elmahio-go v1.0.0
"github.com/jimiit92/elmah.io-go" v1.0.0
github.com/gorilla/mux v1.7.3
)

0 comments on commit 5e6b78a

Please sign in to comment.