github.com/blend/go-sdk@v1.20220411.3/examples/web/basic/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  	"fmt"
    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(
    20  		web.OptBindAddr(":8080"),
    21  		web.OptLog(logger.Prod()),
    22  	)
    23  	app.GET("/", func(r *web.Ctx) web.Result {
    24  		return web.Text.Result("ok!")
    25  	})
    26  
    27  	app.POST("/reparse", func(r *web.Ctx) web.Result {
    28  		body, err := r.PostBody()
    29  		if err != nil {
    30  			return web.Text.BadRequest(err)
    31  		}
    32  		if len(body) == 0 {
    33  			return web.Text.BadRequest(fmt.Errorf("empty body"))
    34  		}
    35  		return web.Text.Result(web.StringValue(r.Param("foo")))
    36  	})
    37  	if err := graceful.Shutdown(app); err != nil {
    38  		logger.FatalExit(err)
    39  	}
    40  }