1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| package main
import ( "encoding/json" "fmt" "github.com/kataras/iris" "github.com/kataras/iris/middleware/logger" "github.com/kataras/iris/middleware/recover" "os" )
func main() { app := iris.New() app.Use(recover.New()) app.Use(logger.New())
app.Configure(iris.WithConfiguration(iris.Configuration{ DisableInterruptHandler: false, EnablePathEscape: false, TimeFormat: "Mon,02 Jan 2006 15:04:05 GMT", Charset: "utf-8", }))
app.Configure(iris.WithConfiguration(iris.YAML("/user/.../iris.yaml"))) file, _ := os.Open("/User/.../config.json") defer file.Close() decoder := json.NewDecoder(file) conf := Configuration{} err := decoder.Decode(&conf) if err != nil { fmt.Println("error",err) } fmt.Println(conf.Port)
app.Handle("GET", "/", func(ctx iris.Context) { path := ctx.Path() UserName := ctx.URLParam("username") app.Logger().Info(path, UserName) ctx.HTML("<h1>Welcome" + UserName + "</h1>") })
app.Get("/ping", func(ctx iris.Context) { ctx.WriteString("pong") })
app.Post("/hello", func(ctx iris.Context) { UserName := ctx.PostValue("name") app.Logger().Info(UserName)
var person Person if err := ctx.ReadJSON(&person); err != nil { panic(err.Error()) } ctx.JSON(iris.Map{"message": "Hello Iris!"}) })
app.Get("/weather/{date}/{city}/{isLogin:bool}/{userid:uint64}", func(ctx iris.Context) { date := ctx.Params().Get("date") isLogin, err := ctx.Params().GetBool("isLogin") userid, err := ctx.Params().GetUint("userid") if err == nil && isLogin { app.Logger().Info("isLogin") } ctx.JSON(map[string]interface{}{ "requestCode": 200, "userId": userid, "date": date, }) })
userParty := app.Party("/users", func (ctx iris.Context) { ctx.Next() }) userParty.Get("/register", func(ctx iris.Context) { ctx.WriteString("用户注册功能") })
userRouter := app.Party("/admin", PathLogMiddleware) userRouter.Get("/info", func(ctx iris.Context) { ctx.WriteString("info") }) userRouter.Get("/query", func(ctx iris.Context) { ctx.WriteString("query") ctx.Next() }) userRouter.Done(func(ctx iris.Context) { ctx.Application().Logger().Infof("response to " + ctx.Path()) })
app.Run(iris.Addr(":8080")) }
type Configuration struct { AppName string `json:"appname"` Port string `json:"port"` }
type Person struct { Name string `json:"name"` Age int `json:"age"` }
func PathLogMiddleware(ctx iris.Context) { path := ctx.Path() ctx.Application().Logger().Infof(path) ctx.Next() }
|