func main() { // register global middleware, you can pass more than one handler comma separated iris.UseFunc(func(c *iris.Context) { fmt.Println("(1)Global logger: %s", c.PathString()) c.Next() }) // register a global structed iris.Handler as middleware myglobal := MyGlobalMiddlewareStructed{loggerId: "my logger id"} iris.Use(myglobal) // register route's middleware iris.Get("/home", func(c *iris.Context) { fmt.Println("(1)HOME logger for /home") c.Next() }, func(c *iris.Context) { fmt.Println("(2)HOME logger for /home") c.Next() }, func(c *iris.Context) { c.Write("Hello from /home") }) println("Iris is listening on :8080") iris.Listen("8080") }
func main() { iris.Get("/hello/:name", func(c *iris.Context) { name := c.Param("name") c.Write("Hello " + name) }) iris.Listen(":8080") }
func main() { iris.Get("/hi", func(ctx *iris.Context) { ctx.Write("Hi %s", "iris") }) iris.Listen(":8080") //err := iris.ListenWithErr(":8080") }
func main() { iris.Use(recovery.Handler) iris.Get("/", func(ctx *iris.Context) { ctx.Write("Hi, let's panic") panic("Don't worry, be happy!!") }) iris.Listen(":8000") }
func main() { authentication := basicauth.Default(map[string]string{"user": "******"}) iris.Get("/secret", authentication, func(ctx *iris.Context) { username := ctx.GetString("user") ctx.Write("Hello authenticated user: %s ", username) }) iris.Listen(":8000") }
func main() { // set the global middlewares iris.Use(logger.New()) // register the routes & the public API registerAPI() // start the server iris.Listen("127.0.0.1:8080") }
func main() { flag.Parse() iris.Use(logger.New(iris.Logger())) iris.Use(cors.DefaultCors()) iris.Get("/", controller.Index) iris.Get("/course/:courseID/:lessonID", controller.GetCourse) iris.Post("/submit/:classID/:courseID/:lessonID/:qn", controller.SubmitCode) fmt.Printf("listen on %s\n", *port) iris.Listen(*port) }
func main() { dbinfo := fmt.Sprintf("port=32768 user=%s password=%s dbname=%s sslmode=disable", "postgres", "pass123", "postgres") db, err := sql.Open("postgres", dbinfo) if err != nil { fmt.Printf("err: %+v ", err) } defer db.Close() iris.Get("/people", GetAll(db)) iris.Get("/people/:id", Get(db)) iris.Post("/people/:id", Post(db)) iris.Delete("/people/:id", Delete(db)) iris.Listen(":8000") }
func main() { mongoSession, pastas, err := database.NewPastasConnection() if err != nil { log.Fatal(err) } iris.UseTemplate(html.New(html.Config{ Layout: "layout.html", })).Directory("./templates", ".html") iris.Static("/public", "./static", 1) iris.Get("/", func(ctx *iris.Context) { ctx.Render("home.html", Page{"Tonnarello", Pasta{"null", "null", "null"}}, iris.RenderOptions{"gzip": true}) }) iris.Post("/insert", func(ctx *iris.Context) { pasta := Pasta{} err := ctx.ReadForm(&pasta) if err != nil { fmt.Printf("ERR") } pasta.Id = bson.NewObjectId() err = pastas.Insert(pasta) if err != nil { log.Fatal(err) } ctx.Redirect("pasta/"+pasta.Id.Hex(), http.StatusSeeOther) }) iris.Get("/pasta/:id", func(ctx *iris.Context) { objId := bson.ObjectIdHex(ctx.Param("id")) pasta := &Pasta{} pastas.FindId(objId).One(pasta) ctx.Render("pasta.html", Page{"Tonnarello", pasta}, iris.RenderOptions{"gzip": true}) }) iris.Listen(":4000") defer mongoSession.Close() }
func main() { iris.Static("/js", "./static/js", 1) iris.Get("/", func(ctx *iris.Context) { ctx.Render("client.html", clientPage{"Client Page", ctx.HostString()}) }) iris.Config.Websocket.Endpoint = "/gowroc" iris.Websocket.OnConnection(func(c iris.WebsocketConnection) { c.Join("room") c.On("message", func(message string) { c.To("room").Emit("message", "From: "+c.ID()+": "+message) }) c.OnDisconnect(func() { fmt.Printf("\nConnection with ID: %s has been disconnected!", c.ID()) }) }) iris.Listen(":8000") }
func main() { iris.StaticWeb("/", "./public", 0) iris.Listen(":8000") }
func main() { iris.Use(utils.NewIrisRecoverMiddleware(&logger.DefaultLogger{})) iris.Listen(fmt.Sprintf(`:%s`, config.GetAPIPort())) }
func main() { configName := os.Getenv("IRIS_CONFIG_NAME") if len(configName) > 0 { viper.SetConfigName(configName) } else { viper.SetConfigName("config") } viper.AddConfigPath("./config/") err := viper.ReadInConfig() if err != nil { panic(fmt.Errorf("設定ファイル読み込みエラー: %s \n", err)) } var dataSource bytes.Buffer dataSource.WriteString(viper.GetString("db.user")) dataSource.WriteString(":") dataSource.WriteString(viper.GetString("db.password")) dataSource.WriteString("@tcp(") dataSource.WriteString(viper.GetString("db.url")) dataSource.WriteString(":") dataSource.WriteString(viper.GetString("db.port")) dataSource.WriteString(")/chat") dataSource.WriteString("?interpolateParams=true&collation=utf8mb4_bin") db, dbErr = sql.Open("mysql", dataSource.String()) if dbErr != nil { log.Fatal(dbErr) } iris.Static("/js", "./static/js", 1) iris.Static("/css", "./static/css", 1) iris.Get("/messages", getMessage) iris.Get("/", func(ctx *iris.Context) { ctx.Render("client.html", clientPage{"Client Page", ctx.HostString()}) }) // important staff iris.Config.Websocket.Endpoint = "/my_endpoint" // you created a new websocket server, you can create more than one... I leave that to you: w2:= websocket.New...; w2.OnConnection(...) // for default 'iris.' station use that: w := websocket.New(iris.DefaultIris, "/my_endpoint") iris.Websocket.OnConnection(func(c iris.WebsocketConnection) { c.On("init", func(message string) { var d data json.Unmarshal([]byte(message), &d) c.Emit("join", "join room: "+d.Room) if err != nil { log.Fatal(err) } c.Join(d.Room) }) c.On("chat", func(message string) { var d data json.Unmarshal([]byte(message), &d) // to all except this connection -> //c.To(websocket.Broadcast).Emit("chat", "Message from: "+c.ID()+"-> "+message) // to the client -> //c.Emit("chat", "Message from myself: "+message) //send the message to the whole room, //all connections are inside this room will receive this message _, err = db.Exec( `INSERT INTO chats (roomid, text) VALUES (?, ?) `, d.Room, d.Msg, ) if err != nil { log.Fatal(err) } c.To(d.Room).Emit("chat", "From: "+c.ID()+": "+d.Msg) }) c.On("leave", func(message string) { var d data json.Unmarshal([]byte(message), &d) c.Leave(d.Room) c.To(d.Room).Emit("chat", "leave room: "+d.Room) }) c.OnDisconnect(func() { fmt.Printf("\nConnection with ID: %s has been disconnected!", c.ID()) }) }) iris.Listen("0.0.0.0:80") }