func AuthMiddleware(c *mv.Context) { log.Println("Simulate user authentication.") rand.Seed(time.Now().Unix()) if rand.Int()%2 == 0 { c.Response.Write([]byte("User authenticated.\n")) c.Next() } else { c.Response.WriteHeader(http.StatusUnauthorized) c.Response.Write([]byte("Use not authenticated.\n")) c.Abort() } }
func (uae UserAgentExtractor) Handle(c *mv.Context) { ua := user_agent.New(c.Request.Header["User-Agent"][0]) name, version := ua.Browser() browserEngine, browserEngineVersion := ua.Engine() browserInfo := BrowserInfo{ verbose: uae.verbose, Name: name, Version: version, OS: ua.OS(), Engine: browserEngine, EngineVersion: browserEngineVersion, Localization: ua.Localization(), Platform: ua.Platform(), Mobile: ua.Mobile(), } c.WithValue(browserInfoKey, browserInfo) c.Next() }
func BrowserInfoHandler(c *mv.Context) { browserInfo := c.Value(browserInfoKey).(BrowserInfo) c.Response.Write([]byte(browserInfo.String())) }
func Middleware1(c *mv.Context) { c.Response.Write([]byte("Added in middleware 1 before calling Next\n")) c.Next() c.Response.Write([]byte("Added in middleware 1 after calling Next\n")) }
func LoggingMiddleware(c *mv.Context) { log.Println("Simluate real logging here") c.Next() }