github.com/blend/go-sdk@v1.20220411.3/examples/web/timeout/main.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package main
     9  
    10  import (
    11  	"time"
    12  
    13  	"github.com/blend/go-sdk/graceful"
    14  	"github.com/blend/go-sdk/logger"
    15  	"github.com/blend/go-sdk/web"
    16  )
    17  
    18  func main() {
    19  	app := web.MustNew(web.OptLog(logger.All()))
    20  
    21  	app.GET("/", func(_ *web.Ctx) web.Result {
    22  		return web.NoContent
    23  	}, web.WithTimeout(500*time.Millisecond), web.JSONProviderAsDefault)
    24  
    25  	app.GET("/for/:duration", func(r *web.Ctx) web.Result {
    26  		duration, err := web.DurationValue(r.RouteParam("duration"))
    27  		if err != nil {
    28  			return web.JSON.BadRequest(err)
    29  		}
    30  		time.Sleep(duration)
    31  		return web.NoContent
    32  	}, web.WithTimeout(5*time.Second), web.JSONProviderAsDefault)
    33  
    34  	app.GET("/panic", func(_ *web.Ctx) web.Result {
    35  		panic("ONLY A TEST")
    36  	}, web.WithTimeout(500*time.Millisecond), web.JSONProviderAsDefault)
    37  
    38  	if err := graceful.Shutdown(app); err != nil {
    39  		logger.FatalExit(err)
    40  	}
    41  }