Skip to content
Permalink
Browse files

Fixed function comments based on effective go best practices

  • Loading branch information...
eko committed Mar 16, 2019
1 parent 8dfbc3d commit 4c8fa9cb631ab120217243ce202fb5561c8d9053
Showing with 32 additions and 32 deletions.
  1. +5 −5 context.go
  2. +5 −5 gofast.go
  3. +1 −1 middleware.go
  4. +2 −2 middleware_test.go
  5. +2 −2 request.go
  6. +2 −2 request_test.go
  7. +2 −2 response.go
  8. +1 −1 response_test.go
  9. +4 −4 router.go
  10. +3 −3 router_test.go
  11. +3 −3 templating.go
  12. +2 −2 templating_test.go
@@ -17,12 +17,12 @@ type Context struct {
route *Route
}

// Creates a new context component instance
// NewContext creates a new context component instance
func NewContext() Context {
return Context{}
}

// Sets Logrus logger instance
// SetLogger sets Logrus logger instance
func (c *Context) SetLogger(logger *logrus.Logger) {
c.logger = logger
}
@@ -32,7 +32,7 @@ func (c *Context) GetLogger() *logrus.Logger {
return c.logger
}

// Sets a HTTP request instance
// SetRequest sets a HTTP request instance
func (c *Context) SetRequest(req *http.Request) {
request := NewRequest(req)
c.request = &request
@@ -43,7 +43,7 @@ func (c *Context) GetRequest() *Request {
return c.request
}

// Sets a route instance
// SetRoute sets a route instance
func (c *Context) SetRoute(route *Route) {
c.route = route
}
@@ -53,7 +53,7 @@ func (c *Context) GetRoute() *Route {
return c.route
}

// Sets a HTTP response instance
// SetResponse sets a HTTP response instance
func (c *Context) SetResponse(res http.ResponseWriter) {
response := NewResponse(res)
c.response = &response
@@ -27,7 +27,7 @@ type Gofast struct {
*Middleware
}

// Bootstraps a new instance
// Bootstrap bootstraps a new instance
func Bootstrap() *Gofast {
logrus.WithFields(logrus.Fields{"version": VERSION}).Info("gofast is running")

@@ -39,7 +39,7 @@ func Bootstrap() *Gofast {
return &Gofast{logger, &router, &templating, &middleware}
}

// Prepares a HTTP server
// PrepareHttpServer prepares a HTTP server
func (g *Gofast) PrepareHttpServer() string {
sort.Sort(RouteLen(g.GetRoutes()))
http.Handle("/", g)
@@ -59,14 +59,14 @@ func (g *Gofast) PrepareHttpServer() string {
return port
}

// Listens and handles HTTP requests
// Listen listens and handles HTTP requests
func (g *Gofast) Listen() {
port := g.PrepareHttpServer()

http.ListenAndServe(port, nil)
}

// Listens and handles HTTP/2 requests
// ListenHttp2 listens and handles HTTP/2 requests
func (g *Gofast) ListenHttp2(certificate string, key string) {
port := g.PrepareHttpServer()
server := &http.Server{Addr: port, Handler: nil}
@@ -75,7 +75,7 @@ func (g *Gofast) ListenHttp2(certificate string, key string) {
logrus.Fatal(server.ListenAndServeTLS(certificate, key))
}

// Serves HTTP request by matching the correct route
// ServeHTTP serves HTTP request by matching the correct route
func (g *Gofast) ServeHTTP(res http.ResponseWriter, req *http.Request) {
matchedRoute := g.GetFallback()

@@ -10,7 +10,7 @@ type Middleware struct {

type MiddlewareFunc func(context Context, middleware MiddlewareFunc) Handler

// Creates a new middleware component instance
// NewMiddleware creates a new middleware component instance
func NewMiddleware() Middleware {
return Middleware{middlewares: make([]MiddlewareFunc, 0)}
}
@@ -8,7 +8,7 @@ import (
"testing"
)

// Tests initializing a new middleware component
// TestMiddleware tests initializing a new middleware component
func TestMiddleware(t *testing.T) {
middleware := NewMiddleware()

@@ -17,7 +17,7 @@ func TestMiddleware(t *testing.T) {
}
}

// Tests adding a new middlewares
// TestUseNewMiddlewares tests adding a new middlewares
func TestUseNewMiddlewares(t *testing.T) {
middleware := NewMiddleware()
middleware.Use(func(context Context, next MiddlewareFunc) Handler {
@@ -18,14 +18,14 @@ type Parameter struct {
value interface{}
}

// Creates a new Request component instance
// NewRequest creates a new Request component instance
func NewRequest(req *http.Request) Request {
req.ParseForm()

return Request{req, make([]Parameter, 0)}
}

// Returs HTTP request
// GetHttpRequest returs HTTP request
func (r *Request) GetHttpRequest() *http.Request {
return r.httpRequest
}
@@ -9,7 +9,7 @@ import (
"testing"
)

// Tests setting and retrieving request parameters
// TestParameters tests setting and retrieving request parameters
func TestParameters(t *testing.T) {
httpRequest := new(http.Request)
request := NewRequest(httpRequest)
@@ -26,7 +26,7 @@ func TestParameters(t *testing.T) {
}
}

// Tests retrieving a header
// TestGetHeader tests retrieving a header
func TestGetHeader(t *testing.T) {
httpRequest, _ := http.NewRequest("GET", "/", nil)
httpRequest.Header.Set("X-Test-Header", "yes")
@@ -13,12 +13,12 @@ type Response struct {
statusCode int
}

// Creates a new Response component instance
// NewResponse creates a new Response component instance
func NewResponse(res http.ResponseWriter) Response {
return Response{res, 200}
}

// Sets Response status code
// SetStatusCode sets Response status code
func (r *Response) SetStatusCode(statusCode int) {
r.WriteHeader(statusCode)
r.statusCode = statusCode
@@ -9,7 +9,7 @@ import (
"testing"
)

// Tests setting and retrieving a status code
// TestStatusCode tests setting and retrieving a status code
func TestStatusCode(t *testing.T) {
recorder := httptest.NewRecorder()

@@ -23,7 +23,7 @@ type RouteLen []Route

type Handler func(context Context)

// Creates a new router component instance
// NewRouter creates a new router component instance
func NewRouter() Router {
return Router{routes: make([]Route, 0)}
}
@@ -85,7 +85,7 @@ func (r *Router) GetRoute(name string) Route {
return result
}

// Sets route fallback (for 404 error pages)
// SetFallback sets route fallback (for 404 error pages)
func (r *Router) SetFallback(handler Handler) {
r.Add("*", "fallback", "/", handler)
}
@@ -100,7 +100,7 @@ func (r *Route) GetPattern() *regexp.Regexp {
return r.pattern
}

// Sets a route handler
// SetHandler sets a route handler
func (r *Route) SetHandler(handler Handler) {
r.handler = handler
}
@@ -110,7 +110,7 @@ func (r *Route) GetHandler() Handler {
return r.handler
}

// Route sort functions
// Len route sort functions
func (this RouteLen) Len() int {
return len(this)
}
@@ -8,7 +8,7 @@ import (
"testing"
)

// Tests different add methods
// TestAllAddMethods tests different add methods
func TestAllAddMethods(t *testing.T) {
router := NewRouter()

@@ -60,7 +60,7 @@ func TestAllAddMethods(t *testing.T) {
}
}

// Tests adding a fallback route
// TestFallbackRoute tests adding a fallback route
func TestFallbackRoute(t *testing.T) {
router := NewRouter()

@@ -73,7 +73,7 @@ func TestFallbackRoute(t *testing.T) {
}
}

// Tests route handling getter
// TestRouteGetHandler tests route handling getter
func TestRouteGetHandler(t *testing.T) {
handler := func(c Context) {}

@@ -17,12 +17,12 @@ type Templating struct {
assetsDirectory string
}

// Creates a new templating component instance
// NewTemplating creates a new templating component instance
func NewTemplating() Templating {
return Templating{}
}

// Sets templating views directory
// SetViewsDirectory sets templating views directory
func (t *Templating) SetViewsDirectory(name string) {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
@@ -39,7 +39,7 @@ func (t *Templating) GetViewsDirectory() string {
return t.viewsDirectory
}

// Sets templating assets directory
// SetAssetsDirectory sets templating assets directory
func (t *Templating) SetAssetsDirectory(name string) {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
@@ -9,7 +9,7 @@ import (
"testing"
)

// Tests directory setter/getter
// TestSetDirectories tests directory setter/getter
func TestSetDirectories(t *testing.T) {
templating := NewTemplating()

@@ -25,7 +25,7 @@ func TestSetDirectories(t *testing.T) {
}
}

// Tests rendering a view via pongo2 library
// TestRender tests rendering a view via pongo2 library
func TestRender(t *testing.T) {
templating := NewTemplating()
templating.SetViewsDirectory("../")

0 comments on commit 4c8fa9c

Please sign in to comment.
You can’t perform that action at this time.