| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package main
- import (
- "fmt"
- "git.sxidc.com/go-framework/baize"
- "git.sxidc.com/go-framework/baize/api"
- "git.sxidc.com/go-framework/baize/application"
- DEATH "github.com/vrecan/death"
- "net/http"
- "syscall"
- )
- func main() {
- app := baize.NewApplication(application.Config{
- ApiConfig: application.ApiConfig{
- UrlPrefix: "test",
- Port: "10000",
- },
- })
- app.Api().
- RootRouter().
- AddMiddlewares(func(c *api.Context) {
- fmt.Println("Global Before1")
- c.Next()
- fmt.Println("Global After1")
- }, func(c *api.Context) {
- fmt.Println("Global Before2")
- c.Next()
- fmt.Println("Global After2")
- }).
- AddRoute(http.MethodGet, "/ping", func(c *api.Context) {
- c.String(http.StatusOK, "pong")
- }, func(c *api.Context) {
- fmt.Println("Root Route Before1")
- c.Next()
- fmt.Println("Root Route After1")
- }, func(c *api.Context) {
- fmt.Println("Root Route Before2")
- c.Next()
- fmt.Println("Root Route After2")
- })
- app.Api().
- PrefixRouter().
- RegisterVersionedRouter("v1", func(c *api.Context) {
- fmt.Println("Global Before1")
- c.Next()
- fmt.Println("Global After1")
- }, func(c *api.Context) {
- fmt.Println("Global Before2")
- c.Next()
- fmt.Println("Global After2")
- }).
- AddRoute(http.MethodGet, "/ping", func(c *api.Context) {
- c.String(http.StatusOK, "pong")
- }, func(c *api.Context) {
- fmt.Println("Versioned Route Before1")
- c.Next()
- fmt.Println("Versioned Route After1")
- }, func(c *api.Context) {
- fmt.Println("Versioned Route Before2")
- c.Next()
- fmt.Println("Versioned Route After2")
- })
- go func() {
- if err := app.Start(); err != nil {
- panic(err)
- }
- }()
- defer func() {
- if err := app.Finish(); err != nil {
- panic(err)
- }
- }()
- death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
- _ = death.WaitForDeath()
- }
|