Example #1
0
// Users ...
func Users(c lars.Context) {

	ctx := c.(*MyContext)

	ctx.AppContext.Log.Println("In Users Function")

	c.Response().Write([]byte("Users"))
}
Example #2
0
// 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))
}
Example #3
0
// 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))
}
Example #4
0
// 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))
}
Example #5
0
// 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))
}
Example #6
0
// 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())
}
Example #7
0
// 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)
}
Example #9
0
// Redirect ...
func Redirect(c lars.Context) {
	c.Response().Write([]byte("Redirect"))
}
Example #10
0
// AdminProfile ...
func AdminProfile(c lars.Context) {
	c.Response().Write([]byte("Admin Profile"))
}
Example #11
0
// HelloWorld ...
func HelloWorld(c lars.Context) {
	c.Response().Write([]byte("Hello World"))
}
Example #12
0
// Admin ...
func Admin(c lars.Context) {
	c.Response().Write([]byte("Admin"))
}
Example #13
0
// UserProfile ...
func UserProfile(c lars.Context) {
	c.Response().Write([]byte("User Profile"))
}
Example #14
0
// User ...
func User(c lars.Context) {
	c.Response().Write([]byte("User"))
}
Example #15
0
// Home ...
func Home(c lars.Context) {
	c.Response().Write([]byte("Welcome Home"))
}
Example #16
0
func route2a(ctx *lars.Context) {
	ctx.Response.Write([]byte("Route 2a " + ctx.Param("name")))
}