nmdc-webfrontend/main.go

273 lines
6.1 KiB
Go
Raw Normal View History

2016-10-08 01:58:37 +00:00
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"mime"
2016-10-08 01:58:37 +00:00
"net/http"
"path/filepath"
2016-10-08 01:58:37 +00:00
"code.ivysaur.me/libnmdc"
2016-10-08 01:58:37 +00:00
"github.com/googollee/go-socket.io"
)
var VERSION string = `nmdc-webfrontend/devel-unreleased`
2016-10-08 01:58:37 +00:00
type App struct {
cfg *Config
}
func NewApp(ConfigFilePath string) (*App, error) {
cfgData, err := ioutil.ReadFile(ConfigFilePath)
if err != nil {
return nil, err
}
cfg := Config{}
err = json.Unmarshal(cfgData, &cfg)
if err != nil {
return nil, err
}
return &App{cfg: &cfg}, nil
}
type UserMessageStruct struct {
User string `json:"user"`
Message string `json:"message,omitempty"`
}
func (this *App) HubWorker(Nick, Pass string, so socketio.Socket, done chan struct{}) {
log.Printf("[%s] Connecting to hub\n", so.Id())
selfUser := libnmdc.NewUserInfo(Nick)
selfUser.ClientTag = this.cfg.Hub.Tag
2016-11-29 07:12:49 +00:00
selfUser.ClientVersion = libnmdc.DEFAULT_CLIENT_VERSION
2016-10-08 01:58:37 +00:00
url := this.cfg.Hub.Address
if this.cfg.Hub.Port == 0 {
url = fmt.Sprintf("%s:%d", this.cfg.Hub.Address, this.cfg.Hub.Port)
}
2016-10-08 01:58:37 +00:00
hco := libnmdc.HubConnectionOptions{
Address: libnmdc.HubAddress(url),
2017-11-26 06:51:10 +00:00
Self: selfUser,
NickPassword: Pass,
2016-10-08 01:58:37 +00:00
}
2017-11-26 06:51:10 +00:00
hubEvents := make(chan libnmdc.HubEvent, 10)
hub := libnmdc.ConnectAsync(&hco, hubEvents)
2016-10-08 01:58:37 +00:00
defer func() {
hub.Disconnect()
so.Emit("disconnect") // necessary? https://github.com/googollee/go-socket.io/issues/117
log.Printf("[%s] Leaving worker\n", so.Id())
}()
// Register after-connected SIO handlers
so.On("pub", func(data map[string]string) {
hub.SayPublic(data["message"])
})
so.On("priv", func(data map[string]string) {
hub.SayPrivate(data["user"], data["message"])
})
so.On("raw", func(data map[string]string) {
hub.SayRaw(data["message"])
})
// Loop hub connection
2017-02-05 03:47:39 +00:00
serveUserInfo := func(nick string) {
props := ""
hub.Users(func(users *map[string]libnmdc.UserInfo) error {
uinfo, ok := (*users)[nick]
if !ok {
return nil // just skip
}
bProps, err := json.Marshal(uinfo)
if err != nil {
return nil // just skip
}
props = string(bProps)
return nil
})
// 'Message' is a json-encoded param with user properties
if len(props) > 0 {
so.Emit("info", UserMessageStruct{User: nick, Message: props})
}
}
2016-10-08 01:58:37 +00:00
for {
select {
2017-11-26 06:51:10 +00:00
case hev, ok := <-hubEvents:
2016-10-08 01:58:37 +00:00
if !ok {
log.Printf("[%s] hub chan closed\n", so.Id())
return // abandon
}
switch hev.EventType {
case libnmdc.EVENT_SYSTEM_MESSAGE_FROM_CONN, libnmdc.EVENT_SYSTEM_MESSAGE_FROM_HUB:
so.Emit("sys", hev.Message)
case libnmdc.EVENT_PUBLIC:
so.Emit("pub", UserMessageStruct{User: hev.Nick, Message: hev.Message})
case libnmdc.EVENT_PRIVATE:
so.Emit("priv", UserMessageStruct{User: hev.Nick, Message: hev.Message})
2017-02-05 03:47:39 +00:00
case libnmdc.EVENT_USER_UPDATED_INFO:
serveUserInfo(hev.Nick)
2016-10-08 01:58:37 +00:00
case libnmdc.EVENT_USER_JOINED:
so.Emit("join", UserMessageStruct{User: hev.Nick})
2017-02-05 03:47:39 +00:00
serveUserInfo(hev.Nick)
2016-10-08 01:58:37 +00:00
case libnmdc.EVENT_USER_PART:
so.Emit("part", UserMessageStruct{User: hev.Nick})
case libnmdc.EVENT_CONNECTION_STATE_CHANGED:
if hev.StateChange == libnmdc.CONNECTIONSTATE_CONNECTED {
log.Printf("[%s] Connected to hub\n", so.Id())
2016-10-08 01:58:37 +00:00
so.Emit("hello")
2016-10-08 02:19:47 +00:00
} else if hev.StateChange == libnmdc.CONNECTIONSTATE_DISCONNECTED {
2016-10-08 01:58:37 +00:00
so.Emit("close")
2016-10-08 02:19:47 +00:00
} else {
so.Emit("sys", hev.StateChange.String())
2016-10-08 01:58:37 +00:00
}
case libnmdc.EVENT_HUBNAME_CHANGED:
so.Emit("hubname", hev.Nick)
2016-10-08 02:19:53 +00:00
case libnmdc.EVENT_USERCOMMAND:
so.Emit("usercommand", map[string]interface{}{
"type": hev.UserCommand.Type,
"context": hev.UserCommand.Context,
"title": hev.UserCommand.Message,
"raw": hev.UserCommand.Command,
})
2016-10-08 01:58:37 +00:00
}
case <-done:
log.Printf("[%s] done chan closed\n", so.Id())
return // abandon
}
}
}
func (this *App) SocketIOServer(so socketio.Socket) {
log.Printf("[%s] Client connected", so.Id())
so.Emit("cls")
2017-02-06 03:25:49 +00:00
so.Emit("hubname", this.cfg.Web.Title)
2016-10-08 01:58:37 +00:00
so.Emit("raw", this.cfg.App.MotdHTML+"<br>")
so.Emit("sys", "Enter a name to connect as (or name:pass for a registered nick)")
if len(this.cfg.App.ContentedServer) > 0 {
so.Emit("contented", this.cfg.App.ContentedServer)
}
2016-10-08 01:58:37 +00:00
doneChan := make(chan struct{}, 0)
so.On("hello", func(data map[string]string) {
log.Printf("[%s] Connection request", so.Id())
go this.HubWorker(data["nick"], data["pass"], so, doneChan)
})
so.On("disconnection", func() { // n.b. not 'disconnect' (??)
log.Printf("[%s] Client dropped", so.Id())
close(doneChan)
})
}
2017-02-05 23:18:21 +00:00
func (this *App) customFaviconHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "favicon.ico")
}
2016-10-08 01:58:37 +00:00
func (this *App) StaticRequestHandler(w http.ResponseWriter, r *http.Request) {
fileName := r.URL.Path[1:]
2016-10-08 02:19:58 +00:00
if fileName == "" {
fileName = "index.htm"
}
2016-10-08 01:58:37 +00:00
data, err := Asset(fileName)
if err != nil {
w.WriteHeader(404)
return
}
w.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(fileName)))
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(data)))
2016-10-08 01:58:37 +00:00
w.Write(data)
}
func (this *App) RunServer() {
// Inner mux {{
innerMux := http.NewServeMux()
2016-10-08 01:58:37 +00:00
// Socket.io handler
server, err := socketio.NewServer(nil)
if err != nil {
log.Fatal(err)
}
server.On("connection", this.SocketIOServer)
server.On("error", func(so socketio.Socket, err error) {
log.Println("error:", err)
})
innerMux.Handle("/socket.io/", server)
2016-10-08 01:58:37 +00:00
// Custom favicon handler
2017-02-05 23:18:21 +00:00
if this.cfg.Web.CustomFavicon {
innerMux.HandleFunc("/favicon.ico", this.customFaviconHandler)
2017-02-05 23:18:21 +00:00
}
// Asset handler
if this.cfg.Web.ExternalWebroot {
innerMux.Handle("/", http.FileServer(http.Dir("client")))
} else {
innerMux.HandleFunc("/", this.StaticRequestHandler)
}
// }}
// Wrapper mux {{
outerMux := http.NewServeMux()
outerMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", VERSION)
innerMux.ServeHTTP(w, r)
})
// }}
2016-10-08 01:58:37 +00:00
// Listen and serve
bindAddr := fmt.Sprintf("%s:%d", this.cfg.Web.BindTo, this.cfg.Web.Port)
log.Printf("Serving at %s...", bindAddr)
log.Fatal(http.ListenAndServe(bindAddr, outerMux))
2016-10-08 01:58:37 +00:00
}
func main() {
log.Println(VERSION)
2016-10-08 01:58:37 +00:00
a, err := NewApp("nmdc-webfrontend.conf")
if err != nil {
log.Fatal(err.Error())
return
}
a.RunServer()
}