Embed servers, channels and users in index.html

This commit is contained in:
Ken-Håvard Lieng 2016-01-26 22:10:44 +01:00
parent 04d61dd9a6
commit 22d8b8b363
7 changed files with 123 additions and 45 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { render } from 'react-dom'; import { render } from 'react-dom';
import { browserHistory } from 'react-router'; import { browserHistory } from 'react-router';
import { routeActions } from 'redux-simple-router';
import configureStore from './store'; import configureStore from './store';
import createRoutes from './routes'; import createRoutes from './routes';
import Socket from './util/Socket'; import Socket from './util/Socket';
@ -11,6 +12,30 @@ const host = __DEV__ ? `${window.location.hostname}:1337` : window.location.host
const socket = new Socket(host); const socket = new Socket(host);
const store = configureStore(socket, browserHistory); const store = configureStore(socket, browserHistory);
if (window.__ENV__.servers) {
store.dispatch({
type: 'SOCKET_SERVERS',
data: window.__ENV__.servers
});
} else {
store.dispatch(routeActions.replace('/connect'));
}
if (window.__ENV__.channels) {
store.dispatch({
type: 'SOCKET_CHANNELS',
data: window.__ENV__.channels
});
}
if (window.__ENV__.users) {
store.dispatch({
type: 'SOCKET_USERS',
...window.__ENV__.users
});
}
handleSocket(socket, store); handleSocket(socket, store);
const routes = createRoutes(); const routes = createRoutes();

74
server/index_data.go Normal file
View File

@ -0,0 +1,74 @@
package server
import (
"net/http"
"strings"
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/spf13/viper"
"github.com/khlieng/dispatch/storage"
)
type connectDefaults struct {
Name string `json:"name"`
Address string `json:"address"`
Channels []string `json:"channels"`
Password string `json:"password"`
SSL bool `json:"ssl"`
}
type indexData struct {
Defaults connectDefaults `json:"defaults"`
Servers []storage.Server `json:"servers,omitempty"`
Channels []storage.Channel `json:"channels,omitempty"`
// Users in the selected channel
Users *Userlist `json:"users,omitempty"`
// Last messages in the selected channel
Messages []storage.Message `json:"messages,omitempty"`
}
func getIndexData(r *http.Request, session *Session) *indexData {
servers := session.user.GetServers()
connections := session.getConnectionStates()
for i, server := range servers {
servers[i].Connected = connections[server.Host]
servers[i].Port = ""
servers[i].TLS = false
servers[i].Password = ""
servers[i].Username = ""
servers[i].Realname = ""
}
channels := session.user.GetChannels()
for i, channel := range channels {
channels[i].Topic = channelStore.GetTopic(channel.Server, channel.Name)
}
data := indexData{
Defaults: connectDefaults{
Name: viper.GetString("defaults.name"),
Address: viper.GetString("defaults.address"),
Channels: viper.GetStringSlice("defaults.channels"),
Password: viper.GetString("defaults.password"),
SSL: viper.GetBool("defaults.ssl"),
},
Servers: servers,
Channels: channels,
}
params := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
if len(params) == 2 && isChannel(params[1]) {
users := channelStore.GetUsers(params[0], params[1])
if len(users) > 0 {
data.Users = &Userlist{
Server: params[0],
Channel: params[1],
Users: users,
}
}
}
return &data
}

View File

@ -3,8 +3,6 @@ package server
import ( import (
"encoding/json" "encoding/json"
"io" "io"
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/spf13/viper"
) )
var ( var (
@ -14,32 +12,12 @@ var (
index_3 = []byte(`></script></body></html>`) index_3 = []byte(`></script></body></html>`)
) )
type connectDefaults struct { func renderIndex(w io.Writer, data interface{}) {
Name string `json:"name"`
Address string `json:"address"`
Channels []string `json:"channels"`
Password string `json:"password"`
SSL bool `json:"ssl"`
}
type indexData struct {
Defaults connectDefaults `json:"defaults"`
}
func renderIndex(w io.Writer, session *Session) {
w.Write(index_0) w.Write(index_0)
w.Write([]byte(files[1].Path)) w.Write([]byte(files[1].Path))
w.Write(index_1) w.Write(index_1)
json.NewEncoder(w).Encode(indexData{ json.NewEncoder(w).Encode(data)
Defaults: connectDefaults{
Name: viper.GetString("defaults.name"),
Address: viper.GetString("defaults.address"),
Channels: viper.GetStringSlice("defaults.channels"),
Password: viper.GetString("defaults.password"),
SSL: viper.GetBool("defaults.ssl"),
},
})
w.Write(index_2) w.Write(index_2)
w.Write([]byte(files[0].Path)) w.Write([]byte(files[0].Path))

View File

@ -141,10 +141,10 @@ func serveIndex(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Encoding", "gzip") w.Header().Set("Content-Encoding", "gzip")
gzw := gzip.NewWriter(w) gzw := gzip.NewWriter(w)
renderIndex(gzw, session) renderIndex(gzw, getIndexData(r, session))
gzw.Close() gzw.Close()
} else { } else {
renderIndex(w, session) renderIndex(w, getIndexData(r, session))
} }
} }

View File

@ -26,8 +26,8 @@ func newWSHandler(conn *websocket.Conn, session *Session) *wsHandler {
session: session, session: session,
addr: conn.RemoteAddr().String(), addr: conn.RemoteAddr().String(),
} }
h.initHandlers()
h.init() h.init()
h.initHandlers()
return h return h
} }
@ -63,13 +63,13 @@ func (h *wsHandler) init() {
h.session.numWS(), "WebSocket connections") h.session.numWS(), "WebSocket connections")
channels := h.session.user.GetChannels() channels := h.session.user.GetChannels()
for i, channel := range channels { /*for i, channel := range channels {
channels[i].Topic = channelStore.GetTopic(channel.Server, channel.Name) channels[i].Topic = channelStore.GetTopic(channel.Server, channel.Name)
} }
h.session.sendJSON("channels", channels) h.session.sendJSON("channels", channels)
h.session.sendJSON("servers", h.session.user.GetServers()) h.session.sendJSON("servers", h.session.user.GetServers())
h.session.sendJSON("connection_update", h.session.getConnectionStates()) h.session.sendJSON("connection_update", h.session.getConnectionStates())*/
for _, channel := range channels { for _, channel := range channels {
h.session.sendJSON("users", Userlist{ h.session.sendJSON("users", Userlist{

View File

@ -26,11 +26,12 @@ type Server struct {
Name string `json:"name"` Name string `json:"name"`
Host string `json:"host"` Host string `json:"host"`
Port string `json:"port,omitempty"` Port string `json:"port,omitempty"`
TLS bool `json:"tls"` TLS bool `json:"tls,omitempty"`
Password string `json:"password,omitempty"` Password string `json:"password,omitempty"`
Nick string `json:"nick"` Nick string `json:"nick"`
Username string `json:"username"` Username string `json:"username,omitempty"`
Realname string `json:"realname"` Realname string `json:"realname,omitempty"`
Connected bool `json:"connected"`
} }
type Channel struct { type Channel struct {