// Users ... func Users(c lars.Context) { ctx := c.(*MyContext) ctx.AppContext.Log.Println("In Users Function") c.Response().Write([]byte("Users")) }
// Home ... func Home(c lars.Context) { ctx := c.(*MyContext) var username string // username = ctx.AppContext.DB.find(user by .....) ctx.AppContext.Log.Println("Found User") c.Response().Write([]byte("Welcome Home " + username)) }
// UserProfile ... func UserProfile(c lars.Context) { ctx := c.(*MyContext) id := c.Param("id") var profile string // profile = ctx.AppContext.DB.find(user profile by .....) ctx.AppContext.Log.Println("Found User Profile") c.Response().Write([]byte("Here's your profile " + profile + " user " + id)) }
// User ... func User(c lars.Context) { ctx := c.(*MyContext) id := c.Param("id") var username string // username = ctx.AppContext.DB.find(user by id.....) ctx.AppContext.Log.Println("Found User") c.Response().Write([]byte("Welcome " + username + " with id " + id)) }
// Logger ... func Logger(c lars.Context) { start := time.Now() c.Next() stop := time.Now() path := c.Request().URL.Path if path == "" { path = "/" } log.Printf("%s %d %s %s", c.Request().Method, c.Response().Status(), path, stop.Sub(start)) }
// LoggingAndRecovery handle HTTP request logging + recovery func LoggingAndRecovery(c lars.Context) { t1 := time.Now() defer func() { if err := recover(); err != nil { trace := make([]byte, 1<<16) n := runtime.Stack(trace, true) log.Printf(" %srecovering from panic: %+v\nStack Trace:\n %s%s", Red, err, trace[:n], Reset) HandlePanic(c, trace[:n]) return } }() c.Next() var color string res := c.Response() req := c.Request() code := res.Status() switch { case code >= http.StatusInternalServerError: color = Underscore + Blink + Red case code >= http.StatusBadRequest: color = Red case code >= http.StatusMultipleChoices: color = Yellow default: color = Green } t2 := time.Now() log.Printf("%s %d %s[%s%s%s] %q %v %d\n", color, code, Reset, color, req.Method, Reset, req.URL, t2.Sub(t1), res.Size()) }
// Gzip returns a middleware which compresses HTTP response using gzip compression // scheme. func Gzip(c lars.Context) { c.Response().Header().Add(lars.Vary, lars.AcceptEncoding) if strings.Contains(c.Request().Header.Get(lars.AcceptEncoding), lars.Gzip) { w := writerPool.Get().(*gzip.Writer) w.Reset(c.Response().Writer()) defer func() { w.Close() writerPool.Put(w) }() gw := gzipWriter{Writer: w, ResponseWriter: c.Response().Writer()} c.Response().Header().Set(lars.ContentEncoding, lars.Gzip) c.Response().SetWriter(gw) } c.Next() }
// lars func larsHandler(c lars.Context) { if sleepTime > 0 { time.Sleep(sleepTimeDuration) } c.Response().Write(message) }
// HelloWorld ... func HelloWorld(c lars.Context) { c.Response().Write([]byte("Hello World")) }
// Redirect ... func Redirect(c lars.Context) { c.Response().Write([]byte("Redirect")) }
// AdminProfile ... func AdminProfile(c lars.Context) { c.Response().Write([]byte("Admin Profile")) }
// Admin ... func Admin(c lars.Context) { c.Response().Write([]byte("Admin")) }
// UserProfile ... func UserProfile(c lars.Context) { c.Response().Write([]byte("User Profile")) }
// User ... func User(c lars.Context) { c.Response().Write([]byte("User")) }
// Home ... func Home(c lars.Context) { c.Response().Write([]byte("Welcome Home")) }