Permalink
Browse files

Merge pull request #17 from AquiGorka/feature/remove-go-speedo

Removes go-speedo dependency
  • Loading branch information...
AquiGorka committed Aug 2, 2017
2 parents 0f67597 + 696293c commit cb44d1f4a74a220e170e3a9d60b86d6f436b023e
View
@@ -4,16 +4,23 @@ package main
import (
"github.com/AquiGorka/kickoff/game/server"
"github.com/go-speedo/go-speedo"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
)
func main() {
app := iris.New()
//
r := mux.NewRouter()
// http server
app = server.HTTPServer(app)
r = server.HTTPServer(r)
// websocket server
app = server.WebsocketServer(app)
r = server.WebsocketServer(r)
//
app.Run(iris.Addr(":" + os.Getenv("APP_PORT")))
app := &http.Server{
Addr: ":" + os.Getenv("APP_PORT"),
Handler: r,
}
log.Fatal(app.ListenAndServe())
}
View
@@ -1,9 +1,26 @@
package server
import (
"github.com/go-speedo/go-speedo/websocket"
"github.com/gorilla/websocket"
"log"
)
func onConnectionHandler(c websocket.Connection) {
c.On("ping", func(msg string) { c.Emit("pong", "") })
func pingMsg(mt int, conn *websocket.Conn) {
conn.WriteMessage(mt, []byte("pong"))
}
func onConnection(conn *websocket.Conn) {
defer conn.Close()
for {
mt, msg, err := conn.ReadMessage()
if err != nil {
log.Println("Read error: ", err)
break
}
// messages
//log.Println("Message received: ", string(msg))
if string(msg) == "ping" {
pingMsg(mt, conn)
}
}
}
@@ -10,8 +10,8 @@ import (
func TestOnConnectionHandler(t *testing.T) {
wsEndpoint := "ws://localhost:" + os.Getenv("APP_PORT") + "/ws"
pingMsg := "iris-websocket-message:ping;0;all;"
pongMsg := "iris-websocket-message:pong;0;"
pingMsg := "ping"
pongMsg := "pong"
// net/websocket client
ws, err := websocket.Dial(wsEndpoint, "", "http://localhost")
View

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
View
@@ -1,11 +1,10 @@
package server
import (
"github.com/go-speedo/go-speedo"
"github.com/go-speedo/go-speedo/context"
"net/http"
)
func pingHandler(ctx context.Context) {
ctx.StatusCode(iris.StatusOK)
ctx.HTML("pong")
func pingHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("pong"))
}
View
@@ -1,43 +1,28 @@
package server
import (
"github.com/go-speedo/go-speedo"
"github.com/go-speedo/go-speedo/context"
"github.com/go-speedo/go-speedo/middleware/logger"
"github.com/go-speedo/go-speedo/websocket"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"net/http"
)
// HTTPServer enhances an iris app to accept http requests
func HTTPServer(app *iris.Application) *iris.Application {
// request logger
customLogger := logger.New(logger.Config{
Status: true,
IP: true,
Method: true,
Path: true,
})
app.Use(customLogger)
// 404
app.OnErrorCode(iris.StatusNotFound, func(ctx context.Context) {
customLogger(ctx)
notFoundHandler(ctx)
})
// HTTPServer sets the handlers for the http routes
func HTTPServer(router *mux.Router) *mux.Router {
// ping
app.Get("/ping", pingHandler)
router.HandleFunc("/ping", pingHandler)
//
return app
return router
}
// WebsocketServer enhances an iris app to accept websocket connections
func WebsocketServer(app *iris.Application) *iris.Application {
// endpoint
ws := websocket.New(websocket.Config{
Endpoint: "/ws",
// WebsocketServer sets the handler for websocket connections
func WebsocketServer(router *mux.Router) *mux.Router {
router.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
conn, err := websocket.Upgrade(w, r, w.Header(), 1024, 1024)
if err != nil {
http.Error(w, "Could not open websocket connection", http.StatusBadRequest)
}
go onConnection(conn)
})
// connection handler
ws.OnConnection(onConnectionHandler)
// websocket
ws.Attach(app)
//
return app
return router
}
View
@@ -3,19 +3,27 @@ package server_test
import (
"context"
"github.com/AquiGorka/kickoff/game/server"
"github.com/go-speedo/go-speedo"
"github.com/gorilla/mux"
"golang.org/x/net/websocket"
"log"
"net/http"
"os"
"testing"
)
func TestMain(m *testing.M) {
// run the server
app := iris.New()
app = server.HTTPServer(app)
app = server.WebsocketServer(app)
go app.Run(iris.Addr(":" + os.Getenv("APP_PORT")))
var app *http.Server
go func() {
// run the server
r := mux.NewRouter()
r = server.HTTPServer(r)
r = server.WebsocketServer(r)
app = &http.Server{
Addr: ":" + os.Getenv("APP_PORT"),
Handler: r,
}
log.Fatal(app.ListenAndServe())
}()
// tests
code := m.Run()
// stop the server

0 comments on commit cb44d1f

Please sign in to comment.