Use easyjson
This commit is contained in:
parent
e7cff1686e
commit
09d57b7023
25 changed files with 6167 additions and 122 deletions
|
@ -1,6 +1,6 @@
|
|||
<%! data *indexData, cssPath, jsPath string %>
|
||||
|
||||
<%% import "encoding/json" %%>
|
||||
<%% import "github.com/mailru/easyjson" %%>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
@ -15,7 +15,7 @@
|
|||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script id="env" type="application/json"><% json.NewEncoder(w).Encode(data) %></script>
|
||||
<script id="env" type="application/json"><% easyjson.MarshalToWriter(data, w) %></script>
|
||||
<script src="/<%== jsPath %>"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -4,14 +4,14 @@
|
|||
package server
|
||||
import (
|
||||
"io"
|
||||
"encoding/json"
|
||||
"github.com/mailru/easyjson"
|
||||
)
|
||||
|
||||
func IndexTemplate(w io.Writer, data *indexData, cssPath, jsPath string) error {
|
||||
io.WriteString(w, "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><title>Dispatch</title><link href=\"/")
|
||||
io.WriteString(w, cssPath )
|
||||
io.WriteString(w, "\" rel=\"stylesheet\"><link rel=\"icon\" href=\"data:;base64,=\"></head><body><div id=\"root\"></div><script id=\"env\" type=\"application/json\">")
|
||||
json.NewEncoder(w).Encode(data)
|
||||
easyjson.MarshalToWriter(data, w)
|
||||
io.WriteString(w, "</script><script src=\"/")
|
||||
io.WriteString(w, jsPath )
|
||||
io.WriteString(w, "\"></script></body></html>")
|
||||
|
|
479
server/index_data_easyjson.go
Normal file
479
server/index_data_easyjson.go
Normal file
|
@ -0,0 +1,479 @@
|
|||
// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT.
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
json "encoding/json"
|
||||
storage "github.com/khlieng/dispatch/storage"
|
||||
easyjson "github.com/mailru/easyjson"
|
||||
jlexer "github.com/mailru/easyjson/jlexer"
|
||||
jwriter "github.com/mailru/easyjson/jwriter"
|
||||
)
|
||||
|
||||
// suppress unused package warning
|
||||
var (
|
||||
_ *json.RawMessage
|
||||
_ *jlexer.Lexer
|
||||
_ *jwriter.Writer
|
||||
_ easyjson.Marshaler
|
||||
)
|
||||
|
||||
func easyjson7e607aefDecodeGithubComKhliengDispatchServer(in *jlexer.Lexer, out *indexData) {
|
||||
isTopLevel := in.IsStart()
|
||||
if in.IsNull() {
|
||||
if isTopLevel {
|
||||
in.Consumed()
|
||||
}
|
||||
in.Skip()
|
||||
return
|
||||
}
|
||||
in.Delim('{')
|
||||
for !in.IsDelim('}') {
|
||||
key := in.UnsafeString()
|
||||
in.WantColon()
|
||||
if in.IsNull() {
|
||||
in.Skip()
|
||||
in.WantComma()
|
||||
continue
|
||||
}
|
||||
switch key {
|
||||
case "defaults":
|
||||
if data := in.Raw(); in.Ok() {
|
||||
in.AddError((out.Defaults).UnmarshalJSON(data))
|
||||
}
|
||||
case "servers":
|
||||
if in.IsNull() {
|
||||
in.Skip()
|
||||
out.Servers = nil
|
||||
} else {
|
||||
in.Delim('[')
|
||||
if out.Servers == nil {
|
||||
if !in.IsDelim(']') {
|
||||
out.Servers = make([]Server, 0, 1)
|
||||
} else {
|
||||
out.Servers = []Server{}
|
||||
}
|
||||
} else {
|
||||
out.Servers = (out.Servers)[:0]
|
||||
}
|
||||
for !in.IsDelim(']') {
|
||||
var v1 Server
|
||||
if data := in.Raw(); in.Ok() {
|
||||
in.AddError((v1).UnmarshalJSON(data))
|
||||
}
|
||||
out.Servers = append(out.Servers, v1)
|
||||
in.WantComma()
|
||||
}
|
||||
in.Delim(']')
|
||||
}
|
||||
case "channels":
|
||||
if in.IsNull() {
|
||||
in.Skip()
|
||||
out.Channels = nil
|
||||
} else {
|
||||
in.Delim('[')
|
||||
if out.Channels == nil {
|
||||
if !in.IsDelim(']') {
|
||||
out.Channels = make([]storage.Channel, 0, 1)
|
||||
} else {
|
||||
out.Channels = []storage.Channel{}
|
||||
}
|
||||
} else {
|
||||
out.Channels = (out.Channels)[:0]
|
||||
}
|
||||
for !in.IsDelim(']') {
|
||||
var v2 storage.Channel
|
||||
easyjson7e607aefDecodeGithubComKhliengDispatchStorage(in, &v2)
|
||||
out.Channels = append(out.Channels, v2)
|
||||
in.WantComma()
|
||||
}
|
||||
in.Delim(']')
|
||||
}
|
||||
case "users":
|
||||
if in.IsNull() {
|
||||
in.Skip()
|
||||
out.Users = nil
|
||||
} else {
|
||||
if out.Users == nil {
|
||||
out.Users = new(Userlist)
|
||||
}
|
||||
if data := in.Raw(); in.Ok() {
|
||||
in.AddError((*out.Users).UnmarshalJSON(data))
|
||||
}
|
||||
}
|
||||
case "messages":
|
||||
if in.IsNull() {
|
||||
in.Skip()
|
||||
out.Messages = nil
|
||||
} else {
|
||||
if out.Messages == nil {
|
||||
out.Messages = new(Messages)
|
||||
}
|
||||
if data := in.Raw(); in.Ok() {
|
||||
in.AddError((*out.Messages).UnmarshalJSON(data))
|
||||
}
|
||||
}
|
||||
default:
|
||||
in.SkipRecursive()
|
||||
}
|
||||
in.WantComma()
|
||||
}
|
||||
in.Delim('}')
|
||||
if isTopLevel {
|
||||
in.Consumed()
|
||||
}
|
||||
}
|
||||
func easyjson7e607aefEncodeGithubComKhliengDispatchServer(out *jwriter.Writer, in indexData) {
|
||||
out.RawByte('{')
|
||||
first := true
|
||||
_ = first
|
||||
if true {
|
||||
const prefix string = ",\"defaults\":"
|
||||
if first {
|
||||
first = false
|
||||
out.RawString(prefix[1:])
|
||||
} else {
|
||||
out.RawString(prefix)
|
||||
}
|
||||
out.Raw((in.Defaults).MarshalJSON())
|
||||
}
|
||||
if len(in.Servers) != 0 {
|
||||
const prefix string = ",\"servers\":"
|
||||
if first {
|
||||
first = false
|
||||
out.RawString(prefix[1:])
|
||||
} else {
|
||||
out.RawString(prefix)
|
||||
}
|
||||
{
|
||||
out.RawByte('[')
|
||||
for v3, v4 := range in.Servers {
|
||||
if v3 > 0 {
|
||||
out.RawByte(',')
|
||||
}
|
||||
out.Raw((v4).MarshalJSON())
|
||||
}
|
||||
out.RawByte(']')
|
||||
}
|
||||
}
|
||||
if len(in.Channels) != 0 {
|
||||
const prefix string = ",\"channels\":"
|
||||
if first {
|
||||
first = false
|
||||
out.RawString(prefix[1:])
|
||||
} else {
|
||||
out.RawString(prefix)
|
||||
}
|
||||
{
|
||||
out.RawByte('[')
|
||||
for v5, v6 := range in.Channels {
|
||||
if v5 > 0 {
|
||||
out.RawByte(',')
|
||||
}
|
||||
easyjson7e607aefEncodeGithubComKhliengDispatchStorage(out, v6)
|
||||
}
|
||||
out.RawByte(']')
|
||||
}
|
||||
}
|
||||
if in.Users != nil {
|
||||
const prefix string = ",\"users\":"
|
||||
if first {
|
||||
first = false
|
||||
out.RawString(prefix[1:])
|
||||
} else {
|
||||
out.RawString(prefix)
|
||||
}
|
||||
out.Raw((*in.Users).MarshalJSON())
|
||||
}
|
||||
if in.Messages != nil {
|
||||
const prefix string = ",\"messages\":"
|
||||
if first {
|
||||
first = false
|
||||
out.RawString(prefix[1:])
|
||||
} else {
|
||||
out.RawString(prefix)
|
||||
}
|
||||
out.Raw((*in.Messages).MarshalJSON())
|
||||
}
|
||||
out.RawByte('}')
|
||||
}
|
||||
|
||||
// MarshalJSON supports json.Marshaler interface
|
||||
func (v indexData) MarshalJSON() ([]byte, error) {
|
||||
w := jwriter.Writer{}
|
||||
easyjson7e607aefEncodeGithubComKhliengDispatchServer(&w, v)
|
||||
return w.Buffer.BuildBytes(), w.Error
|
||||
}
|
||||
|
||||
// MarshalEasyJSON supports easyjson.Marshaler interface
|
||||
func (v indexData) MarshalEasyJSON(w *jwriter.Writer) {
|
||||
easyjson7e607aefEncodeGithubComKhliengDispatchServer(w, v)
|
||||
}
|
||||
|
||||
// UnmarshalJSON supports json.Unmarshaler interface
|
||||
func (v *indexData) UnmarshalJSON(data []byte) error {
|
||||
r := jlexer.Lexer{Data: data}
|
||||
easyjson7e607aefDecodeGithubComKhliengDispatchServer(&r, v)
|
||||
return r.Error()
|
||||
}
|
||||
|
||||
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
|
||||
func (v *indexData) UnmarshalEasyJSON(l *jlexer.Lexer) {
|
||||
easyjson7e607aefDecodeGithubComKhliengDispatchServer(l, v)
|
||||
}
|
||||
func easyjson7e607aefDecodeGithubComKhliengDispatchStorage(in *jlexer.Lexer, out *storage.Channel) {
|
||||
isTopLevel := in.IsStart()
|
||||
if in.IsNull() {
|
||||
if isTopLevel {
|
||||
in.Consumed()
|
||||
}
|
||||
in.Skip()
|
||||
return
|
||||
}
|
||||
in.Delim('{')
|
||||
for !in.IsDelim('}') {
|
||||
key := in.UnsafeString()
|
||||
in.WantColon()
|
||||
if in.IsNull() {
|
||||
in.Skip()
|
||||
in.WantComma()
|
||||
continue
|
||||
}
|
||||
switch key {
|
||||
case "server":
|
||||
out.Server = string(in.String())
|
||||
case "name":
|
||||
out.Name = string(in.String())
|
||||
case "topic":
|
||||
out.Topic = string(in.String())
|
||||
default:
|
||||
in.SkipRecursive()
|
||||
}
|
||||
in.WantComma()
|
||||
}
|
||||
in.Delim('}')
|
||||
if isTopLevel {
|
||||
in.Consumed()
|
||||
}
|
||||
}
|
||||
func easyjson7e607aefEncodeGithubComKhliengDispatchStorage(out *jwriter.Writer, in storage.Channel) {
|
||||
out.RawByte('{')
|
||||
first := true
|
||||
_ = first
|
||||
if in.Server != "" {
|
||||
const prefix string = ",\"server\":"
|
||||
if first {
|
||||
first = false
|
||||
out.RawString(prefix[1:])
|
||||
} else {
|
||||
out.RawString(prefix)
|
||||
}
|
||||
out.String(string(in.Server))
|
||||
}
|
||||
if in.Name != "" {
|
||||
const prefix string = ",\"name\":"
|
||||
if first {
|
||||
first = false
|
||||
out.RawString(prefix[1:])
|
||||
} else {
|
||||
out.RawString(prefix)
|
||||
}
|
||||
out.String(string(in.Name))
|
||||
}
|
||||
if in.Topic != "" {
|
||||
const prefix string = ",\"topic\":"
|
||||
if first {
|
||||
first = false
|
||||
out.RawString(prefix[1:])
|
||||
} else {
|
||||
out.RawString(prefix)
|
||||
}
|
||||
out.String(string(in.Topic))
|
||||
}
|
||||
out.RawByte('}')
|
||||
}
|
||||
func easyjson7e607aefDecodeGithubComKhliengDispatchServer1(in *jlexer.Lexer, out *connectDefaults) {
|
||||
isTopLevel := in.IsStart()
|
||||
if in.IsNull() {
|
||||
if isTopLevel {
|
||||
in.Consumed()
|
||||
}
|
||||
in.Skip()
|
||||
return
|
||||
}
|
||||
in.Delim('{')
|
||||
for !in.IsDelim('}') {
|
||||
key := in.UnsafeString()
|
||||
in.WantColon()
|
||||
if in.IsNull() {
|
||||
in.Skip()
|
||||
in.WantComma()
|
||||
continue
|
||||
}
|
||||
switch key {
|
||||
case "name":
|
||||
out.Name = string(in.String())
|
||||
case "host":
|
||||
out.Host = string(in.String())
|
||||
case "port":
|
||||
out.Port = int(in.Int())
|
||||
case "channels":
|
||||
if in.IsNull() {
|
||||
in.Skip()
|
||||
out.Channels = nil
|
||||
} else {
|
||||
in.Delim('[')
|
||||
if out.Channels == nil {
|
||||
if !in.IsDelim(']') {
|
||||
out.Channels = make([]string, 0, 4)
|
||||
} else {
|
||||
out.Channels = []string{}
|
||||
}
|
||||
} else {
|
||||
out.Channels = (out.Channels)[:0]
|
||||
}
|
||||
for !in.IsDelim(']') {
|
||||
var v7 string
|
||||
v7 = string(in.String())
|
||||
out.Channels = append(out.Channels, v7)
|
||||
in.WantComma()
|
||||
}
|
||||
in.Delim(']')
|
||||
}
|
||||
case "password":
|
||||
out.Password = bool(in.Bool())
|
||||
case "ssl":
|
||||
out.SSL = bool(in.Bool())
|
||||
case "readonly":
|
||||
out.ReadOnly = bool(in.Bool())
|
||||
case "showDetails":
|
||||
out.ShowDetails = bool(in.Bool())
|
||||
default:
|
||||
in.SkipRecursive()
|
||||
}
|
||||
in.WantComma()
|
||||
}
|
||||
in.Delim('}')
|
||||
if isTopLevel {
|
||||
in.Consumed()
|
||||
}
|
||||
}
|
||||
func easyjson7e607aefEncodeGithubComKhliengDispatchServer1(out *jwriter.Writer, in connectDefaults) {
|
||||
out.RawByte('{')
|
||||
first := true
|
||||
_ = first
|
||||
if in.Name != "" {
|
||||
const prefix string = ",\"name\":"
|
||||
if first {
|
||||
first = false
|
||||
out.RawString(prefix[1:])
|
||||
} else {
|
||||
out.RawString(prefix)
|
||||
}
|
||||
out.String(string(in.Name))
|
||||
}
|
||||
if in.Host != "" {
|
||||
const prefix string = ",\"host\":"
|
||||
if first {
|
||||
first = false
|
||||
out.RawString(prefix[1:])
|
||||
} else {
|
||||
out.RawString(prefix)
|
||||
}
|
||||
out.String(string(in.Host))
|
||||
}
|
||||
if in.Port != 0 {
|
||||
const prefix string = ",\"port\":"
|
||||
if first {
|
||||
first = false
|
||||
out.RawString(prefix[1:])
|
||||
} else {
|
||||
out.RawString(prefix)
|
||||
}
|
||||
out.Int(int(in.Port))
|
||||
}
|
||||
if len(in.Channels) != 0 {
|
||||
const prefix string = ",\"channels\":"
|
||||
if first {
|
||||
first = false
|
||||
out.RawString(prefix[1:])
|
||||
} else {
|
||||
out.RawString(prefix)
|
||||
}
|
||||
{
|
||||
out.RawByte('[')
|
||||
for v8, v9 := range in.Channels {
|
||||
if v8 > 0 {
|
||||
out.RawByte(',')
|
||||
}
|
||||
out.String(string(v9))
|
||||
}
|
||||
out.RawByte(']')
|
||||
}
|
||||
}
|
||||
if in.Password {
|
||||
const prefix string = ",\"password\":"
|
||||
if first {
|
||||
first = false
|
||||
out.RawString(prefix[1:])
|
||||
} else {
|
||||
out.RawString(prefix)
|
||||
}
|
||||
out.Bool(bool(in.Password))
|
||||
}
|
||||
if in.SSL {
|
||||
const prefix string = ",\"ssl\":"
|
||||
if first {
|
||||
first = false
|
||||
out.RawString(prefix[1:])
|
||||
} else {
|
||||
out.RawString(prefix)
|
||||
}
|
||||
out.Bool(bool(in.SSL))
|
||||
}
|
||||
if in.ReadOnly {
|
||||
const prefix string = ",\"readonly\":"
|
||||
if first {
|
||||
first = false
|
||||
out.RawString(prefix[1:])
|
||||
} else {
|
||||
out.RawString(prefix)
|
||||
}
|
||||
out.Bool(bool(in.ReadOnly))
|
||||
}
|
||||
if in.ShowDetails {
|
||||
const prefix string = ",\"showDetails\":"
|
||||
if first {
|
||||
first = false
|
||||
out.RawString(prefix[1:])
|
||||
} else {
|
||||
out.RawString(prefix)
|
||||
}
|
||||
out.Bool(bool(in.ShowDetails))
|
||||
}
|
||||
out.RawByte('}')
|
||||
}
|
||||
|
||||
// MarshalJSON supports json.Marshaler interface
|
||||
func (v connectDefaults) MarshalJSON() ([]byte, error) {
|
||||
w := jwriter.Writer{}
|
||||
easyjson7e607aefEncodeGithubComKhliengDispatchServer1(&w, v)
|
||||
return w.Buffer.BuildBytes(), w.Error
|
||||
}
|
||||
|
||||
// MarshalEasyJSON supports easyjson.Marshaler interface
|
||||
func (v connectDefaults) MarshalEasyJSON(w *jwriter.Writer) {
|
||||
easyjson7e607aefEncodeGithubComKhliengDispatchServer1(w, v)
|
||||
}
|
||||
|
||||
// UnmarshalJSON supports json.Unmarshaler interface
|
||||
func (v *connectDefaults) UnmarshalJSON(data []byte) error {
|
||||
r := jlexer.Lexer{Data: data}
|
||||
easyjson7e607aefDecodeGithubComKhliengDispatchServer1(&r, v)
|
||||
return r.Error()
|
||||
}
|
||||
|
||||
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
|
||||
func (v *connectDefaults) UnmarshalEasyJSON(l *jlexer.Lexer) {
|
||||
easyjson7e607aefDecodeGithubComKhliengDispatchServer1(l, v)
|
||||
}
|
173
server/json.go
173
server/json.go
|
@ -2,42 +2,43 @@ package server
|
|||
|
||||
import (
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/mailru/easyjson"
|
||||
|
||||
"github.com/khlieng/dispatch/irc"
|
||||
"github.com/khlieng/dispatch/storage"
|
||||
)
|
||||
|
||||
type WSRequest struct {
|
||||
Type string `json:"type"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
Type string
|
||||
Data easyjson.RawMessage
|
||||
}
|
||||
|
||||
type WSResponse struct {
|
||||
Type string `json:"type"`
|
||||
Data interface{} `json:"data"`
|
||||
Type string
|
||||
Data interface{}
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
storage.Server
|
||||
Status ConnectionUpdate `json:"status"`
|
||||
Status ConnectionUpdate
|
||||
}
|
||||
|
||||
type ServerName struct {
|
||||
Server string `json:"server"`
|
||||
Name string `json:"name"`
|
||||
Server string
|
||||
Name string
|
||||
}
|
||||
|
||||
type ReconnectSettings struct {
|
||||
Server string `json:"server"`
|
||||
SkipVerify bool `json:"skipVerify"`
|
||||
Server string
|
||||
SkipVerify bool
|
||||
}
|
||||
|
||||
type ConnectionUpdate struct {
|
||||
Server string `json:"server"`
|
||||
Connected bool `json:"connected"`
|
||||
Error string `json:"error,omitempty"`
|
||||
ErrorType string `json:"errorType,omitempty"`
|
||||
Server string
|
||||
Connected bool
|
||||
Error string
|
||||
ErrorType string
|
||||
}
|
||||
|
||||
func newConnectionUpdate(server string, state irc.ConnectionState) ConnectionUpdate {
|
||||
|
@ -55,139 +56,139 @@ func newConnectionUpdate(server string, state irc.ConnectionState) ConnectionUpd
|
|||
}
|
||||
|
||||
type Nick struct {
|
||||
Server string `json:"server"`
|
||||
Old string `json:"oldNick"`
|
||||
New string `json:"newNick"`
|
||||
Server string
|
||||
Old string `json:"oldNick,omitempty"`
|
||||
New string `json:"newNick,omitempty"`
|
||||
}
|
||||
|
||||
type NickFail struct {
|
||||
Server string `json:"server"`
|
||||
Server string
|
||||
}
|
||||
|
||||
type Join struct {
|
||||
Server string `json:"server"`
|
||||
User string `json:"user"`
|
||||
Channels []string `json:"channels"`
|
||||
Server string
|
||||
User string
|
||||
Channels []string
|
||||
}
|
||||
|
||||
type Part struct {
|
||||
Server string `json:"server"`
|
||||
User string `json:"user"`
|
||||
Channel string `json:"channel,omitempty"`
|
||||
Channels []string `json:"channels,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
Server string
|
||||
User string
|
||||
Channel string
|
||||
Channels []string
|
||||
Reason string
|
||||
}
|
||||
|
||||
type Mode struct {
|
||||
Server string `json:"server"`
|
||||
Channel string `json:"channel"`
|
||||
User string `json:"user"`
|
||||
Add string `json:"add"`
|
||||
Remove string `json:"remove"`
|
||||
Server string
|
||||
Channel string
|
||||
User string
|
||||
Add string
|
||||
Remove string
|
||||
}
|
||||
|
||||
type Quit struct {
|
||||
Server string `json:"server"`
|
||||
User string `json:"user"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
Server string
|
||||
User string
|
||||
Reason string
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Server string `json:"server,omitempty"`
|
||||
From string `json:"from,omitempty"`
|
||||
To string `json:"to,omitempty"`
|
||||
Content string `json:"content"`
|
||||
Type string `json:"type,omitempty"`
|
||||
ID string
|
||||
Server string
|
||||
From string
|
||||
To string
|
||||
Content string
|
||||
Type string
|
||||
}
|
||||
|
||||
type Messages struct {
|
||||
Server string `json:"server"`
|
||||
To string `json:"to"`
|
||||
Messages []storage.Message `json:"messages"`
|
||||
Prepend bool `json:"prepend,omitempty"`
|
||||
Next string `json:"next,omitempty"`
|
||||
Server string
|
||||
To string
|
||||
Messages []storage.Message
|
||||
Prepend bool
|
||||
Next string
|
||||
}
|
||||
|
||||
type Topic struct {
|
||||
Server string `json:"server"`
|
||||
Channel string `json:"channel"`
|
||||
Topic string `json:"topic,omitempty"`
|
||||
Nick string `json:"nick,omitempty"`
|
||||
Server string
|
||||
Channel string
|
||||
Topic string
|
||||
Nick string
|
||||
}
|
||||
|
||||
type Userlist struct {
|
||||
Server string `json:"server"`
|
||||
Channel string `json:"channel"`
|
||||
Users []string `json:"users"`
|
||||
Server string
|
||||
Channel string
|
||||
Users []string
|
||||
}
|
||||
|
||||
type MOTD struct {
|
||||
Server string `json:"server"`
|
||||
Title string `json:"title"`
|
||||
Content []string `json:"content"`
|
||||
Server string
|
||||
Title string
|
||||
Content []string
|
||||
}
|
||||
|
||||
type Invite struct {
|
||||
Server string `json:"server"`
|
||||
Channel string `json:"channel"`
|
||||
User string `json:"user"`
|
||||
Server string
|
||||
Channel string
|
||||
User string
|
||||
}
|
||||
|
||||
type Kick struct {
|
||||
Server string `json:"server"`
|
||||
Channel string `json:"channel"`
|
||||
User string `json:"user"`
|
||||
Server string
|
||||
Channel string
|
||||
User string
|
||||
}
|
||||
|
||||
type Whois struct {
|
||||
Server string `json:"server"`
|
||||
User string `json:"user"`
|
||||
Server string
|
||||
User string
|
||||
}
|
||||
|
||||
type WhoisReply struct {
|
||||
Nick string `json:"nick"`
|
||||
Username string `json:"username"`
|
||||
Host string `json:"host"`
|
||||
Realname string `json:"realname"`
|
||||
Server string `json:"server"`
|
||||
Channels []string `json:"channels"`
|
||||
Nick string
|
||||
Username string
|
||||
Host string
|
||||
Realname string
|
||||
Server string
|
||||
Channels []string
|
||||
}
|
||||
|
||||
type Away struct {
|
||||
Server string `json:"server"`
|
||||
Message string `json:"message"`
|
||||
Server string
|
||||
Message string
|
||||
}
|
||||
|
||||
type Raw struct {
|
||||
Server string `json:"server"`
|
||||
Message string `json:"message"`
|
||||
Server string
|
||||
Message string
|
||||
}
|
||||
|
||||
type SearchRequest struct {
|
||||
Server string `json:"server"`
|
||||
Channel string `json:"channel"`
|
||||
Phrase string `json:"phrase"`
|
||||
Server string
|
||||
Channel string
|
||||
Phrase string
|
||||
}
|
||||
|
||||
type SearchResult struct {
|
||||
Server string `json:"server"`
|
||||
Channel string `json:"channel"`
|
||||
Results []storage.Message `json:"results"`
|
||||
Server string
|
||||
Channel string
|
||||
Results []storage.Message
|
||||
}
|
||||
|
||||
type ClientCert struct {
|
||||
Cert []byte `json:"cert"`
|
||||
Key []byte `json:"key"`
|
||||
Cert []byte
|
||||
Key []byte
|
||||
}
|
||||
|
||||
type FetchMessages struct {
|
||||
Server string `json:"server"`
|
||||
Channel string `json:"channel"`
|
||||
Next string `json:"next"`
|
||||
Server string
|
||||
Channel string
|
||||
Next string
|
||||
}
|
||||
|
||||
type Error struct {
|
||||
Server string `json:"server"`
|
||||
Message string `json:"message"`
|
||||
Server string
|
||||
Message string
|
||||
}
|
||||
|
|
3095
server/json_easyjson.go
Normal file
3095
server/json_easyjson.go
Normal file
File diff suppressed because it is too large
Load diff
|
@ -4,6 +4,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/mailru/easyjson"
|
||||
)
|
||||
|
||||
type wsConn struct {
|
||||
|
@ -31,10 +32,10 @@ func (c *wsConn) send() {
|
|||
return
|
||||
}
|
||||
|
||||
err = c.conn.WriteJSON(res)
|
||||
err = c.writeJSON(res)
|
||||
|
||||
case <-ping:
|
||||
err = c.conn.WriteJSON(WSResponse{Type: "ping"})
|
||||
err = c.writeJSON(WSResponse{Type: "ping"})
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
@ -47,7 +48,7 @@ func (c *wsConn) recv() {
|
|||
var req WSRequest
|
||||
|
||||
for {
|
||||
err := c.conn.ReadJSON(&req)
|
||||
err := c.readJSON(&req)
|
||||
if err != nil {
|
||||
close(c.in)
|
||||
return
|
||||
|
@ -61,3 +62,25 @@ func (c *wsConn) close() {
|
|||
close(c.out)
|
||||
c.conn.Close()
|
||||
}
|
||||
|
||||
func (c *wsConn) readJSON(v easyjson.Unmarshaler) error {
|
||||
_, r, err := c.conn.NextReader()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return easyjson.UnmarshalFromReader(r, v)
|
||||
}
|
||||
|
||||
func (c *wsConn) writeJSON(v easyjson.Marshaler) error {
|
||||
w, err := c.conn.NextWriter(websocket.TextMessage)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err1 := easyjson.MarshalToWriter(v, w)
|
||||
err2 := w.Close()
|
||||
if err1 != nil {
|
||||
return err1
|
||||
}
|
||||
return err2
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
@ -82,7 +81,7 @@ func (h *wsHandler) init(r *http.Request) {
|
|||
|
||||
func (h *wsHandler) connect(b []byte) {
|
||||
var data Server
|
||||
json.Unmarshal(b, &data)
|
||||
data.UnmarshalJSON(b)
|
||||
|
||||
if _, ok := h.session.getIRC(data.Host); !ok {
|
||||
log.Println(h.addr, "[IRC] Add server", data.Host)
|
||||
|
@ -97,7 +96,7 @@ func (h *wsHandler) connect(b []byte) {
|
|||
|
||||
func (h *wsHandler) reconnect(b []byte) {
|
||||
var data ReconnectSettings
|
||||
json.Unmarshal(b, &data)
|
||||
data.UnmarshalJSON(b)
|
||||
|
||||
if i, ok := h.session.getIRC(data.Server); ok && !i.Connected() {
|
||||
if i.TLS {
|
||||
|
@ -109,7 +108,7 @@ func (h *wsHandler) reconnect(b []byte) {
|
|||
|
||||
func (h *wsHandler) join(b []byte) {
|
||||
var data Join
|
||||
json.Unmarshal(b, &data)
|
||||
data.UnmarshalJSON(b)
|
||||
|
||||
if i, ok := h.session.getIRC(data.Server); ok {
|
||||
i.Join(data.Channels...)
|
||||
|
@ -118,7 +117,7 @@ func (h *wsHandler) join(b []byte) {
|
|||
|
||||
func (h *wsHandler) part(b []byte) {
|
||||
var data Part
|
||||
json.Unmarshal(b, &data)
|
||||
data.UnmarshalJSON(b)
|
||||
|
||||
if i, ok := h.session.getIRC(data.Server); ok {
|
||||
i.Part(data.Channels...)
|
||||
|
@ -127,7 +126,7 @@ func (h *wsHandler) part(b []byte) {
|
|||
|
||||
func (h *wsHandler) quit(b []byte) {
|
||||
var data Quit
|
||||
json.Unmarshal(b, &data)
|
||||
data.UnmarshalJSON(b)
|
||||
|
||||
log.Println(h.addr, "[IRC] Remove server", data.Server)
|
||||
if i, ok := h.session.getIRC(data.Server); ok {
|
||||
|
@ -140,7 +139,7 @@ func (h *wsHandler) quit(b []byte) {
|
|||
|
||||
func (h *wsHandler) message(b []byte) {
|
||||
var data Message
|
||||
json.Unmarshal(b, &data)
|
||||
data.UnmarshalJSON(b)
|
||||
|
||||
if i, ok := h.session.getIRC(data.Server); ok {
|
||||
i.Privmsg(data.To, data.Content)
|
||||
|
@ -152,7 +151,7 @@ func (h *wsHandler) message(b []byte) {
|
|||
|
||||
func (h *wsHandler) nick(b []byte) {
|
||||
var data Nick
|
||||
json.Unmarshal(b, &data)
|
||||
data.UnmarshalJSON(b)
|
||||
|
||||
if i, ok := h.session.getIRC(data.Server); ok {
|
||||
i.Nick(data.New)
|
||||
|
@ -161,7 +160,7 @@ func (h *wsHandler) nick(b []byte) {
|
|||
|
||||
func (h *wsHandler) topic(b []byte) {
|
||||
var data Topic
|
||||
json.Unmarshal(b, &data)
|
||||
data.UnmarshalJSON(b)
|
||||
|
||||
if i, ok := h.session.getIRC(data.Server); ok {
|
||||
i.Topic(data.Channel, data.Topic)
|
||||
|
@ -170,7 +169,7 @@ func (h *wsHandler) topic(b []byte) {
|
|||
|
||||
func (h *wsHandler) invite(b []byte) {
|
||||
var data Invite
|
||||
json.Unmarshal(b, &data)
|
||||
data.UnmarshalJSON(b)
|
||||
|
||||
if i, ok := h.session.getIRC(data.Server); ok {
|
||||
i.Invite(data.User, data.Channel)
|
||||
|
@ -179,7 +178,7 @@ func (h *wsHandler) invite(b []byte) {
|
|||
|
||||
func (h *wsHandler) kick(b []byte) {
|
||||
var data Invite
|
||||
json.Unmarshal(b, &data)
|
||||
data.UnmarshalJSON(b)
|
||||
|
||||
if i, ok := h.session.getIRC(data.Server); ok {
|
||||
i.Kick(data.Channel, data.User)
|
||||
|
@ -188,7 +187,7 @@ func (h *wsHandler) kick(b []byte) {
|
|||
|
||||
func (h *wsHandler) whois(b []byte) {
|
||||
var data Whois
|
||||
json.Unmarshal(b, &data)
|
||||
data.UnmarshalJSON(b)
|
||||
|
||||
if i, ok := h.session.getIRC(data.Server); ok {
|
||||
i.Whois(data.User)
|
||||
|
@ -197,7 +196,7 @@ func (h *wsHandler) whois(b []byte) {
|
|||
|
||||
func (h *wsHandler) away(b []byte) {
|
||||
var data Away
|
||||
json.Unmarshal(b, &data)
|
||||
data.UnmarshalJSON(b)
|
||||
|
||||
if i, ok := h.session.getIRC(data.Server); ok {
|
||||
i.Away(data.Message)
|
||||
|
@ -206,7 +205,7 @@ func (h *wsHandler) away(b []byte) {
|
|||
|
||||
func (h *wsHandler) raw(b []byte) {
|
||||
var data Raw
|
||||
json.Unmarshal(b, &data)
|
||||
data.UnmarshalJSON(b)
|
||||
|
||||
if i, ok := h.session.getIRC(data.Server); ok {
|
||||
i.Write(data.Message)
|
||||
|
@ -216,7 +215,7 @@ func (h *wsHandler) raw(b []byte) {
|
|||
func (h *wsHandler) search(b []byte) {
|
||||
go func() {
|
||||
var data SearchRequest
|
||||
json.Unmarshal(b, &data)
|
||||
data.UnmarshalJSON(b)
|
||||
|
||||
results, err := h.session.user.SearchMessages(data.Server, data.Channel, data.Phrase)
|
||||
if err != nil {
|
||||
|
@ -234,7 +233,7 @@ func (h *wsHandler) search(b []byte) {
|
|||
|
||||
func (h *wsHandler) cert(b []byte) {
|
||||
var data ClientCert
|
||||
json.Unmarshal(b, &data)
|
||||
data.UnmarshalJSON(b)
|
||||
|
||||
err := h.session.user.SetCertificate(data.Cert, data.Key)
|
||||
if err != nil {
|
||||
|
@ -247,14 +246,14 @@ func (h *wsHandler) cert(b []byte) {
|
|||
|
||||
func (h *wsHandler) fetchMessages(b []byte) {
|
||||
var data FetchMessages
|
||||
json.Unmarshal(b, &data)
|
||||
data.UnmarshalJSON(b)
|
||||
|
||||
h.session.sendMessages(data.Server, data.Channel, 200, data.Next)
|
||||
}
|
||||
|
||||
func (h *wsHandler) setServerName(b []byte) {
|
||||
var data ServerName
|
||||
json.Unmarshal(b, &data)
|
||||
data.UnmarshalJSON(b)
|
||||
|
||||
if isValidServerName(data.Name) {
|
||||
h.session.user.SetServerName(data.Name, data.Server)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue