From 0b8e97b215cbbe099d01cd0f605d6438e68885f6 Mon Sep 17 00:00:00 2001 From: khlieng Date: Tue, 3 Feb 2015 00:25:52 +0100 Subject: [PATCH] Started doing client-side routing, added some rewriting on the server to support it --- client/src/js/app.js | 16 +++++++++++++++- client/src/js/components/App.jsx | 4 ++-- client/src/js/components/Settings.jsx | 13 +++++++++++++ irc.go | 2 +- json_types.go | 4 ++-- main.go | 24 +++++++++++++++++++++--- 6 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 client/src/js/components/Settings.jsx diff --git a/client/src/js/app.js b/client/src/js/app.js index 4704f7a3..28d1d1e8 100644 --- a/client/src/js/app.js +++ b/client/src/js/app.js @@ -1,9 +1,14 @@ var React = require('react'); +var Router = require('react-router'); +var Route = Router.Route; +var DefaultRoute = Router.DefaultRoute; require('./irc'); var socket = require('./socket'); var util = require('./util'); var App = require('./components/App.jsx'); +var Chat = require('./components/Chat.jsx'); +var Settings = require('./components/Settings.jsx'); var tabActions = require('./actions/tab'); var serverActions = require('./actions/server'); var channelActions = require('./actions/channel'); @@ -25,4 +30,13 @@ socket.on('error', function(error) { console.log(error.server + ': ' + error.message); }); -React.render(, document.body); \ No newline at end of file +var routes = ( + + + + +); + +Router.run(routes, Router.HistoryLocation, function(Handler) { + React.render(, document.body); +}); \ No newline at end of file diff --git a/client/src/js/components/App.jsx b/client/src/js/components/App.jsx index 13d294d7..de5803c8 100644 --- a/client/src/js/components/App.jsx +++ b/client/src/js/components/App.jsx @@ -1,14 +1,14 @@ var React = require('react'); +var RouteHandler = require('react-router').RouteHandler; var TabList = require('./TabList.jsx'); -var Chat = require('./Chat.jsx'); var App = React.createClass({ render: function() { return (
- +
); } diff --git a/client/src/js/components/Settings.jsx b/client/src/js/components/Settings.jsx new file mode 100644 index 00000000..c787bac1 --- /dev/null +++ b/client/src/js/components/Settings.jsx @@ -0,0 +1,13 @@ +var React = require('react'); + +var Settings = React.createClass({ + render: function() { + return ( +
+

Settings

+
+ ); + } +}); + +module.exports = Settings; \ No newline at end of file diff --git a/irc.go b/irc.go index 4d9bcaf6..6784584e 100644 --- a/irc.go +++ b/irc.go @@ -94,7 +94,7 @@ func (i *IRC) Connect(address string) error { } i.Server = address - dialer := &net.Dialer{Timeout: 5 * time.Second} + dialer := &net.Dialer{Timeout: 10 * time.Second} if i.TLS { if i.TLSConfig == nil { diff --git a/json_types.go b/json_types.go index 5d947568..d7001dcc 100644 --- a/json_types.go +++ b/json_types.go @@ -17,7 +17,7 @@ type WSResponse struct { type Connect struct { Server string `json:"server"` TLS bool `json:"tls"` - Name string `json:"name"` + Name string `json:"name,omitempty"` Nick string `json:"nick"` Username string `json:"username"` } @@ -56,7 +56,7 @@ type Quit struct { type Chat struct { Server string `json:"server"` From string `json:"from"` - To string `json:"to"` + To string `json:"to,omitempty"` Message string `json:"message"` } diff --git a/main.go b/main.go index f3af0621..139511cf 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,10 @@ package main import ( "log" "net/http" + "strings" "sync" + "github.com/julienschmidt/httprouter" "golang.org/x/net/websocket" "github.com/khlieng/name_pending/storage" @@ -14,13 +16,27 @@ var ( channelStore *storage.ChannelStore sessions map[string]*Session sessionLock sync.Mutex + fs http.Handler ) +func serveFiles(w http.ResponseWriter, r *http.Request) { + if strings.HasSuffix(r.URL.Path, "bundle.js") { + r.URL.Path = "/bundle.js" + } else if strings.HasSuffix(r.URL.Path, "style.css") { + r.URL.Path = "/style.css" + } else { + r.URL.Path = "/" + } + + fs.ServeHTTP(w, r) +} + func main() { defer storage.Cleanup() channelStore = storage.NewChannelStore() sessions = make(map[string]*Session) + fs = http.FileServer(http.Dir("client/dist")) /*for _, user := range storage.LoadUsers() { channels := user.GetChannels() @@ -49,9 +65,11 @@ func main() { } }*/ - http.Handle("/", http.FileServer(http.Dir("client/dist"))) - http.Handle("/ws", websocket.Handler(handleWS)) + router := httprouter.New() + + router.Handler("GET", "/ws", websocket.Handler(handleWS)) + router.NotFound = serveFiles log.Println("Listening on port 1337") - http.ListenAndServe(":1337", nil) + http.ListenAndServe(":1337", router) }