Skip to content

Commit

Permalink
Clean up the code
Browse files Browse the repository at this point in the history
  • Loading branch information
lafriakh committed Mar 10, 2019
1 parent 82435b4 commit f8a4fc7
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 45 deletions.
4 changes: 2 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Context struct {
response http.ResponseWriter
Logger *log.Logger
Configs *config.Config
// The data assocaited with the request.
// The data associated with the request.
data map[string]interface{}
}

Expand Down Expand Up @@ -53,7 +53,7 @@ func (c *Context) Response() http.ResponseWriter {
return c.response
}

// Header Write HTTP header to the response.
// HeaderStatus Write HTTP header to the response and also write the status message to the body.
func (c *Context) HeaderStatus(code int) {
c.Response().WriteHeader(code)
}
Expand Down
5 changes: 3 additions & 2 deletions context_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func (c *Context) View(temps string, data ...interface{}) error {
// Set content type
c.Response().Header().Set("Content-Type", "text/html")

// parse the tempaltes
// parse the templates
template, templateData, err := parseView(c, temps, data)
if err != nil {
return err
Expand All @@ -26,6 +26,7 @@ func (c *Context) View(temps string, data ...interface{}) error {
return nil
}

// ViewToString parse a template and return the parsed template as a string.
func (c *Context) ViewToString(temps string, data ...interface{}) (string, error) {
template, templateData, err := parseView(c, temps, data)
if err != nil {
Expand All @@ -41,7 +42,7 @@ func (c *Context) ViewToString(temps string, data ...interface{}) (string, error
return buf.String(), nil
}

// Validate if the view exists.
// ViewExists Validate if the view exists.
func (c *Context) ViewExists(tmp string) bool {
fileSuffix := c.Config().GetString("views.file_suffix", ".go.html")
viewPath := c.Config().GetString("views.path", "app/views/")
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ require (
github.com/cheggaaa/pb v2.0.6+incompatible // indirect
github.com/codesenberg/bombardier v1.2.4 // indirect
github.com/codesenberg/concurrent v0.0.0-20180531114123-64560cfcf964 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-kira/config v0.1.1
github.com/go-kira/kog v0.1.2 // indirect
github.com/go-kira/log v0.1.2
github.com/google/uuid v1.1.1
github.com/gorilla/csrf v1.5.1
github.com/juju/ratelimit v1.0.1 // indirect
github.com/julienschmidt/httprouter v1.2.0
github.com/klauspost/compress v1.4.1 // indirect
github.com/klauspost/cpuid v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/mattn/go-isatty v0.0.6 // indirect
github.com/pmezard/licenses v0.0.0-20160314180953-1117911df3df // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/valyala/fasthttp v1.2.0 // indirect
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
golang.org/x/net v0.0.0-20190301231341-16b79f2e4e95 // indirect
golang.org/x/sys v0.0.0-20190309122539-980fc434d28e // indirect
golang.org/x/text v0.3.0 // indirect
Expand Down
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ github.com/codesenberg/bombardier v1.2.4 h1:3erpAAQYOKmrdE7T94IspdsyQlKvbwn8kga7
github.com/codesenberg/bombardier v1.2.4/go.mod h1:Md2gz997+9PrcVcR8yeE0F/b/pHCVbNvw17oEXqyyF4=
github.com/codesenberg/concurrent v0.0.0-20180531114123-64560cfcf964 h1:9MVnbW3h0Dl4E2oADqwyvODphl9jY1r5HMtcB8U5mGs=
github.com/codesenberg/concurrent v0.0.0-20180531114123-64560cfcf964/go.mod h1:82C6OyVM6eVk7qpBAZXE9uszHUuXWJMHHOeY+b/CSIA=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/go-kira/config v0.1.1 h1:L54LgWCqBKRkNRHOQOZ2k+EK2poQvqWCNNykwOpPrK4=
github.com/go-kira/config v0.1.1/go.mod h1:kEkP3XmeJpQZyAmhw272/NhBoEsaGDukCqccXNlB5GU=
github.com/go-kira/kog v0.0.0/go.mod h1:zqGZJHidG6U0e34KJBAtij6d9AW+/dFynky0vyd/H60=
github.com/go-kira/kog v0.1.2/go.mod h1:zqGZJHidG6U0e34KJBAtij6d9AW+/dFynky0vyd/H60=
github.com/go-kira/log v0.1.2 h1:8ujYuzIPe2Sdxd09gHhi9DqWBDdfQrC6+OmEPEhxsKo=
github.com/go-kira/log v0.1.2/go.mod h1:pZ9iDQiXacX85NI4pA4GqmfDwn7xiCDXzxnX6nSg5T8=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/csrf v1.5.1 h1:UASc2+EB0T51tvl6/2ls2ciA8/qC7KdTO7DsOEKbttQ=
github.com/gorilla/csrf v1.5.1/go.mod h1:HTDW7xFOO1aHddQUmghe9/2zTvg7AYCnRCs7MxTGu/0=
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/juju/ratelimit v1.0.1 h1:+7AIFJVQ0EQgq/K9+0Krm7m530Du7tIz0METWzN0RgY=
github.com/juju/ratelimit v1.0.1/go.mod h1:qapgC/Gy+xNh9UxzV13HGGl/6UXNN+ct+vwSgWNm/qk=
github.com/julienschmidt/httprouter v1.2.0 h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+9HbQbYf7g=
Expand All @@ -33,16 +42,23 @@ github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcncea
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.6 h1:SrwhHcpV4nWrMGdNcC2kXpMfcBVYGDuTArqyhocJgvA=
github.com/mattn/go-isatty v0.0.6/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/licenses v0.0.0-20160314180953-1117911df3df h1:zKXkpgoc3AWB9w5iAUm0IrQ2EKvVgpSGwq9f5yOwuoY=
github.com/pmezard/licenses v0.0.0-20160314180953-1117911df3df/go.mod h1:zNcvL6v2ZCDvc2dcDIK43YknFjk9WFnF0SxvCU2Hp5Q=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.2.0 h1:dzZJf2IuMiclVjdw0kkT+f9u4YdrapbNyGAN47E/qnk=
github.com/valyala/fasthttp v1.2.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk8LWSxF3s=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190301231341-16b79f2e4e95 h1:fY7Dsw114eJN4boqzVSbpVHO6rTdhq6/GnXeu+PKnzU=
golang.org/x/net v0.0.0-20190301231341-16b79f2e4e95/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190309122539-980fc434d28e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
2 changes: 1 addition & 1 deletion helpers/byte.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func BytesFormat(inputNum float64, precision int) string {
unit = " PB" // petabyte
} else if inputNum >= 1000000000000 {
returnVal = RoundUp((inputNum / 1099511627776), precision)
unit = " TB" // terrabyte
unit = " TB" // terabyte
} else if inputNum >= 1000000000 {
returnVal = RoundUp((inputNum / 1073741824), precision)
unit = " GB" // gigabyte
Expand Down
2 changes: 1 addition & 1 deletion helpers/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Between(value string, a string, b string) string {
return value[posFirstAdjusted:posLast]
}

// RandomString return random string by lenght
// RandomString return random string by length
func RandomString(length int) string {
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
bytes := RandomBytes(length)
Expand Down
15 changes: 8 additions & 7 deletions kira.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,13 @@ func defaultNotFound(ctx *Context) {
Message string `json:"message"`
}{http.StatusNotFound, "404 Not Found"})
return
} else { // HTML
// Validate if the template exists
if ctx.ViewExists("errors/404") {
ctx.View("errors/404")
} else {
ctx.String("<!DOCTYPE html><html><head><title>404 Not Found</title></head><body>404 Not Found</body></html>")
}
}

// HTML
// Validate if the template exists
if ctx.ViewExists("errors/404") {
ctx.View("errors/404")
} else {
ctx.String("<!DOCTYPE html><html><head><title>404 Not Found</title></head><body>404 Not Found</body></html>")
}
}
16 changes: 8 additions & 8 deletions kira_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,42 +113,42 @@ func createRoute(app *App, method string, path string, handler HandlerFunc, midd
return route
}

// Handle GET requests.
// Get Handle GET requests.
func (app *App) Get(path string, ctx HandlerFunc, middlewares ...Middleware) *Route {
return createRoute(app, "GET", path, ctx, middlewares...)
}

// Handle HEAD requests.
// Head Handle HEAD requests.
func (app *App) Head(path string, ctx HandlerFunc, middlewares ...Middleware) *Route {
return createRoute(app, "HEAD", path, ctx, middlewares...)
}

// Handle POST requests.
// Post Handle POST requests.
func (app *App) Post(path string, ctx HandlerFunc, middlewares ...Middleware) *Route {
return createRoute(app, "POST", path, ctx, middlewares...)
}

// Handle PUT requests.
// Put Handle PUT requests.
func (app *App) Put(path string, ctx HandlerFunc, middlewares ...Middleware) *Route {
return createRoute(app, "PUT", path, ctx, middlewares...)
}

// Handle PATCH requests.
// Patch Handle PATCH requests.
func (app *App) Patch(path string, ctx HandlerFunc, middlewares ...Middleware) *Route {
return createRoute(app, "PATCH", path, ctx, middlewares...)
}

// Handle DELETE requests.
// Delete Handle DELETE requests.
func (app *App) Delete(path string, ctx HandlerFunc, middlewares ...Middleware) *Route {
return createRoute(app, "DELETE", path, ctx, middlewares...)
}

// Handle OPTIONS requests.
// Options Handle OPTIONS requests.
func (app *App) Options(path string, ctx HandlerFunc, middlewares ...Middleware) *Route {
return createRoute(app, "OPTIONS", path, ctx, middlewares...)
}

// Handle ServeFiles requests.
// ServeFiles serve files in the given root.
func (app *App) ServeFiles(path string, root http.FileSystem) {
app.Router.ServeFiles(path, root)
}
12 changes: 6 additions & 6 deletions kira_router_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,32 @@ func (g Group) Get(path string, handler HandlerFunc, middlewares ...Middleware)
g.app.Get(g.path(path), handler, middlewares...)
}

// Get is a shortcut for app.Head with the group prefix.
// Head is a shortcut for app.Head with the group prefix.
func (g Group) Head(path string, handler HandlerFunc, middlewares ...Middleware) {
g.app.Head(g.path(path), handler, middlewares...)
}

// Get is a shortcut for app.Post with the group prefix.
// Post is a shortcut for app.Post with the group prefix.
func (g Group) Post(path string, handler HandlerFunc, middlewares ...Middleware) {
g.app.Post(g.path(path), handler, middlewares...)
}

// Get is a shortcut for app.Put with the group prefix.
// Put is a shortcut for app.Put with the group prefix.
func (g Group) Put(path string, handler HandlerFunc, middlewares ...Middleware) {
g.app.Put(g.path(path), handler, middlewares...)
}

// Get is a shortcut for app.Patch with the group prefix.
// Patch is a shortcut for app.Patch with the group prefix.
func (g Group) Patch(path string, handler HandlerFunc, middlewares ...Middleware) {
g.app.Patch(g.path(path), handler, middlewares...)
}

// Get is a shortcut for app.Delete with the group prefix.
// Delete is a shortcut for app.Delete with the group prefix.
func (g Group) Delete(path string, handler HandlerFunc, middlewares ...Middleware) {
g.app.Delete(g.path(path), handler, middlewares...)
}

// Get is a shortcut for app.ServeFiles with the group prefix.
// ServeFiles is a shortcut for app.ServeFiles with the group prefix.
func (g Group) ServeFiles(path string, root http.FileSystem) {
g.app.ServeFiles(g.path(path), root)
}
3 changes: 1 addition & 2 deletions kira_view.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package kira

import (
"errors"
"fmt"
"html/template"
"path/filepath"
Expand All @@ -26,7 +25,7 @@ func parseView(c *Context, temps string, data ...interface{}) (*template.Templat
for _, temp := range templates {
tmplPath := viewPath + temp + fileSuffix
if !c.ViewExists(temp) {
return nil, nil, errors.New(fmt.Sprintf("kira: template %s not exits.", tmplPath))
return nil, nil, fmt.Errorf("kira: template %s not exits", tmplPath)
}

templatesFiles = append(templatesFiles, tmplPath)
Expand Down
7 changes: 1 addition & 6 deletions middlewares/csrf/csrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ func New() *CSRF {
// CSRF Middelware
type CSRF struct{}

// Name of the middleware.
func (c *CSRF) Name() string {
return "csrf"
}

// Middleware ...
// Middleware handler.
func (c *CSRF) Middleware(ctx *kira.Context, next kira.HandlerFunc) {
// Here we convert the next context handler to the normal http.Handler.
// We just wrap it so we can use it later with Gorilla CSRF middleware.
Expand Down
6 changes: 1 addition & 5 deletions middlewares/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ func New() *Example {
return &Example{}
}

func (e *Example) Name() string {
return "example"
}

// Middleware ...
// Middleware handler.
func (e *Example) Middleware(c *kira.Context, next kira.HandlerFunc) {
// Next handlerr
c.String("before \n")
Expand Down
3 changes: 2 additions & 1 deletion middlewares/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ func New() *JWT {
return &JWT{}
}

// CreateToken generate JWT token.
func CreateToken(ctx *kira.Context, claims jwt.MapClaims) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)

return token.SignedString([]byte(ctx.Config().GetString("app.key")))
}

// Handler - middelware handler
// Middleware handler
func (j *JWT) Middleware(ctx *kira.Context, next kira.HandlerFunc) {
// The token string that will be validated.
var tokenString string
Expand Down
2 changes: 1 addition & 1 deletion middlewares/limitbody/limitbody.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func New() *Limitbody {
return &Limitbody{}
}

// Handler - middelware handler
// Middleware handler.
func (l *Limitbody) Middleware(ctx *kira.Context, next kira.HandlerFunc) {
if ctx.Request().ContentLength > ctx.Config().GetInt64("server.body_limit", 32)*MB {
http.Error(ctx.Response(), "Request too large", http.StatusExpectationFailed)
Expand Down
2 changes: 1 addition & 1 deletion middlewares/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func New() *Log {
return &Log{}
}

// Handler - middleware handler
// Middleware handler.
func (l *Log) Middleware(ctx *kira.Context, next kira.HandlerFunc) {
// Start time
var now = time.Now()
Expand Down
2 changes: 1 addition & 1 deletion middlewares/recover/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func New() *Recover {
return &Recover{}
}

// Handler - middelware handler
// Middleware handler.
func (rc *Recover) Middleware(ctx *kira.Context, next kira.HandlerFunc) {
defer func() {
r := recover()
Expand Down
3 changes: 2 additions & 1 deletion middlewares/requestid/request_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ func New() *RequestID {
return &RequestID{}
}

// Handler - middleware handler
// Middleware handler.
func (rq *RequestID) Middleware(ctx *kira.Context, next kira.HandlerFunc) {
headerName := ctx.Config().GetString("server.request_id", "X-Request-Id")

// Request ID
requestid := rq.random()

Expand Down

0 comments on commit f8a4fc7

Please sign in to comment.