Skip to content

Commit

Permalink
1. Updated Readme
Browse files Browse the repository at this point in the history
2. Updated Logger
	- Added a new logging interface
3. Updated tests to cover more code
4. Updated responses to use constants defined in Go's http standard library instead of using
integers for respective HTTP response codes
5. Check error while executing templates and log
  • Loading branch information
bnkamalesh committed Oct 11, 2018
1 parent f3fc330 commit 2c84d07
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 213 deletions.
37 changes: 36 additions & 1 deletion README.md
Expand Up @@ -6,7 +6,7 @@
[![](https://raw.githubusercontent.com/istio/fortio/master/docs/mentioned-badge.svg?sanitize=true)](https://github.com/avelino/awesome-go)


# WebGo v2.3.2
# WebGo v2.4.0

WebGo is a minimalistic web framework for Go. It gives you the following features.

Expand All @@ -17,6 +17,7 @@ WebGo is a minimalistic web framework for Go. It gives you the following feature
5. Helper functions
6. HTTPS ready
7. Graceful shutdown
8. Logging interface

WebGo's route handlers have the same signature as the standard libraries' HTTP handler.
i.e. `http.HandlerFunc`
Expand Down Expand Up @@ -236,6 +237,40 @@ func main() {
```
### Logging interface
Webgo has a singleton, global variable `LOGHANDLER` of type `Logger`. Logger is an interface which
has the following methods:
```go
type Logger interface {
Debug(data ...interface{})
Info(data ...interface{})
Warn(data ...interface{})
Error(data ...interface{})
Fatal(data ...interface{})
}
```
The singleton variable is initialized in the package's `init()` function. The default logger which
is initialized in Webgo has uses Go's standard `log` library.
A custom logger which implements the same interface can be assigned to the singleton to use your own
logger.
```go
package main
import (
"github.com/bnkamalesh/webgo"
)
func main() {
webgo.LOGHANDLER = customLogger
}
```
## Full sample
```go
Expand Down
6 changes: 3 additions & 3 deletions config.go
Expand Up @@ -37,17 +37,17 @@ type Config struct {
func (cfg *Config) Load(filepath string) {
file, err := ioutil.ReadFile(filepath)
if err != nil {
errLogger.Fatal(err)
LOGHANDLER.Fatal(err)
}

err = json.Unmarshal(file, cfg)
if err != nil {
errLogger.Fatal(err)
LOGHANDLER.Fatal(err)
}

err = cfg.Validate()
if err != nil {
errLogger.Fatal(ErrInvalidPort)
LOGHANDLER.Fatal(ErrInvalidPort)
}
}

Expand Down
60 changes: 54 additions & 6 deletions errors.go
Expand Up @@ -11,9 +11,57 @@ var (
ErrInvalidPort = errors.New("Port number not provided or is invalid (should be between 0 - 65535)")
)

var (
errLogger = log.New(os.Stderr, "Error ", log.LstdFlags|log.Lshortfile)
stdLogger = log.New(os.Stdout, "", log.LstdFlags)
infoLogger = log.New(os.Stdout, "Info ", log.LstdFlags)
warnLogger = log.New(os.Stdout, "Warning ", log.LstdFlags)
)
// Service defines all the logging methods to be implemented
type Logger interface {
Debug(data ...interface{})
Info(data ...interface{})
Warn(data ...interface{})
Error(data ...interface{})
Fatal(data ...interface{})
}

// logHandler has all the log writer handlers
type logHandler struct {
debug *log.Logger
info *log.Logger
warn *log.Logger
err *log.Logger
fatal *log.Logger
}

// Debug prints log of severity 5
func (lh *logHandler) Debug(data ...interface{}) {
lh.debug.Println(data...)
}

// Info prints logs of severity 4
func (lh *logHandler) Info(data ...interface{}) {
lh.info.Println(data...)
}

// Warn prints log of severity 3
func (lh *logHandler) Warn(data ...interface{}) {
lh.warn.Println(data...)
}

// Error prints log of severity 2
func (lh *logHandler) Error(data ...interface{}) {
lh.err.Println(data...)
}

// Fatal prints log of severity 1
func (lh *logHandler) Fatal(data ...interface{}) {
lh.fatal.Fatalln(data...)
}

var LOGHANDLER Logger

func init() {
LOGHANDLER = &logHandler{
fatal: log.New(os.Stderr, "Fatal ", log.LstdFlags|log.Llongfile),
err: log.New(os.Stderr, "Error ", log.LstdFlags),
warn: log.New(os.Stderr, "Warning ", log.LstdFlags),
info: log.New(os.Stdout, "Info ", log.LstdFlags),
debug: log.New(os.Stdout, "Debug ", log.LstdFlags),
}
}
32 changes: 26 additions & 6 deletions releasenote.md
@@ -1,39 +1,59 @@
# Release - 09/10/2018
# Release notes

## Release - 11/10/2018

### v2.4.0

1. Updated Readme
2. Updated Logger
- Added a new logging interface
3. Updated tests to cover more code
4. Updated responses to use constants defined in Go's http standard library instead of using
integers for respective HTTP response codes

## Release - 09/10/2018

### v2.3.2

1. Updated Readme
2. Updated Middleware
- Backward incompatible updates
- CORS middleware functions are updated and now accepts list of supported/allowed domains
- The middleware functions will default to "*" if no domains are passed to the functions

# Release - 03/05/2018
## Release - 03/05/2018

### v2.2.4

1. 501 response for unsupported HTTP methods

# Release - 12/04/2018
## Release - 12/04/2018

### v2.2.3

1. Fixed the long standing invalid http status code [bug](https://github.com/bnkamalesh/webgo/issues/7)
2. Fixed bug in access-log middleware which caused invalid HTTP status code
3. Updated docs with all latest updates

# Release - 08/04/2018
## Release - 08/04/2018

### v2.2.0

1. Graceful shutdown added
2. Updated readme with details of how to use the shutdown

# Release - 02/04/2018
## Release - 02/04/2018

### v2.1.0

1. Updated Readme to include godoc badge
2. Renamed `middlewares` to `middleware`

### v2.1.1

1. Initializing `AppContext` in NewRouter to avoid nil pointer assignment

# Release v2.0.0 - 01/04/2018
## Release v2.0.0 - 01/04/2018

1. Log levels
1. Error logs are now printed to `os.Stderr` with a prefix `Error`
Expand Down
33 changes: 18 additions & 15 deletions responses.go
Expand Up @@ -82,7 +82,7 @@ func SendResponse(w http.ResponseWriter, data interface{}, rCode int) {
In case of encoding error, send "internal server error" after
logging the actual error.
*/
errLogger.Println(err)
LOGHANDLER.Error(err)
R500(w, ErrInternalServer)
}
}
Expand All @@ -100,7 +100,7 @@ func SendError(w http.ResponseWriter, data interface{}, rCode int) {
In case of encoding error, send "internal server error" after
logging the actual error.
*/
errLogger.Println(err)
LOGHANDLER.Error(err)
R500(w, ErrInternalServer)
}
}
Expand All @@ -112,66 +112,69 @@ func Render(w http.ResponseWriter, data interface{}, rCode int, tpl *template.Te
w.WriteHeader(rCode)

// Rendering an HTML template with appropriate data
tpl.Execute(w, data)
err := tpl.Execute(w, data)
if err != nil {
LOGHANDLER.Error(err.Error())
}
}

// Render404 - used to render a 404 page
func Render404(w http.ResponseWriter, tpl *template.Template) {
Render(w, ErrorData{
404,
http.StatusNotFound,
"Sorry, the URL you requested was not found on this server... Or you're lost :-/",
},
404,
http.StatusNotFound,
tpl,
)
}

// R200 - Successful/OK response
func R200(w http.ResponseWriter, data interface{}) {
SendResponse(w, data, 200)
SendResponse(w, data, http.StatusOK)
}

// R201 - New item created
func R201(w http.ResponseWriter, data interface{}) {
SendResponse(w, data, 201)
SendResponse(w, data, http.StatusCreated)
}

// R204 - empty, no content
func R204(w http.ResponseWriter) {
SendHeader(w, 204)
SendHeader(w, http.StatusNoContent)
}

// R302 - Temporary redirect
func R302(w http.ResponseWriter, data interface{}) {
SendResponse(w, data, 302)
SendResponse(w, data, http.StatusFound)
}

// R400 - Invalid request, any incorrect/erraneous value in the request body
func R400(w http.ResponseWriter, data interface{}) {
SendError(w, data, 400)
SendError(w, data, http.StatusBadRequest)
}

// R403 - Unauthorized access
func R403(w http.ResponseWriter, data interface{}) {
SendError(w, data, 403)
SendError(w, data, http.StatusForbidden)
}

// R404 - Resource not found
func R404(w http.ResponseWriter, data interface{}) {
SendError(w, data, 404)
SendError(w, data, http.StatusNotFound)
}

// R406 - Unacceptable header. For any error related to values set in header
func R406(w http.ResponseWriter, data interface{}) {
SendError(w, data, 406)
SendError(w, data, http.StatusNotAcceptable)
}

// R451 - Resource taken down because of a legal request
func R451(w http.ResponseWriter, data interface{}) {
SendError(w, data, 451)
SendError(w, data, http.StatusUnavailableForLegalReasons)
}

// R500 - Internal server error
func R500(w http.ResponseWriter, data interface{}) {
SendError(w, data, 500)
SendError(w, data, http.StatusInternalServerError)
}

0 comments on commit 2c84d07

Please sign in to comment.