Пример #1
0
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()
}
Пример #2
0
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")
}
Пример #3
0
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")
}