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 { render } from 'react-dom';
import { browserHistory } from 'react-router';
import { routeActions } from 'redux-simple-router';
import configureStore from './store';
import createRoutes from './routes';
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 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);
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 (
"encoding/json"
"io"
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/spf13/viper"
)
var (
@ -14,32 +12,12 @@ var (
index_3 = []byte(`></script></body></html>`)
)
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"`
}
func renderIndex(w io.Writer, session *Session) {
func renderIndex(w io.Writer, data interface{}) {
w.Write(index_0)
w.Write([]byte(files[1].Path))
w.Write(index_1)
json.NewEncoder(w).Encode(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"),
},
})
json.NewEncoder(w).Encode(data)
w.Write(index_2)
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")
gzw := gzip.NewWriter(w)
renderIndex(gzw, session)
renderIndex(gzw, getIndexData(r, session))
gzw.Close()
} 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,
addr: conn.RemoteAddr().String(),
}
h.initHandlers()
h.init()
h.initHandlers()
return h
}
@ -63,13 +63,13 @@ func (h *wsHandler) init() {
h.session.numWS(), "WebSocket connections")
channels := h.session.user.GetChannels()
for i, channel := range channels {
/*for i, channel := range channels {
channels[i].Topic = channelStore.GetTopic(channel.Server, channel.Name)
}
h.session.sendJSON("channels", channels)
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 {
h.session.sendJSON("users", Userlist{

View File

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