forked from mjonescase/skeletor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiki.go
143 lines (122 loc) · 3.17 KB
/
wiki.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"database/sql"
"encoding/json"
"flag"
"github.com/gorilla/websocket"
_ "github.com/lib/pq"
"io/ioutil"
"log"
"net/http"
"skeletor/utils"
)
var (
clients = make(map[*websocket.Conn]bool) // connected clients
broadcast = make(chan Message) // broadcast channel
upgrader = websocket.Upgrader{} // configure the upgrader
config = map[string]string{}
configFile *string = flag.String("config", "config", "Path to config file")
DefaultError = map[string]string{"ErrorReason": "You sent in a request with invalid json"}
session = &sql.DB{}
)
// define our message object
type Message struct {
Email string `json:"email"`
Username string `json:"username"`
Message string `json:"message"`
}
type Profile struct {
Id string `json:"id"`
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Username string `json:"username"`
Title string `json:"title"`
Password string `json:"-"`
MobileNumber string `json:"mobile"`
}
func handleConnections(writer http.ResponseWriter, request *http.Request) {
ws, err := upgrader.Upgrade(writer, request, nil)
if err != nil {
log.Fatal(err)
}
// Make sure we close the connection when the function returns
defer ws.Close()
// Register our new client
clients[ws] = true
for {
var msg Message
// Read in a new message as JSON and map it to a Message object
err := ws.ReadJSON(&msg)
if err != nil {
log.Printf("error: %v", err)
delete(clients, ws)
break
}
// Send the newly received message to the broadcast channel
broadcast <- msg
}
}
func handleRegistration(rw http.ResponseWriter, req *http.Request) {
request := Profile{}
decoder := json.NewDecoder(req.Body)
err := decoder.Decode(&request)
if err != nil {
utils.MustEncode(rw, DefaultError)
return
}
saveUserProfile(&request)
utils.MustEncode(rw, request)
}
func handleMessages() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast
// Send it out to every client that is currently connected
for client := range clients {
err := client.WriteJSON(msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients, client)
}
}
}
}
func initDb() {
var err error
session, err = sql.Open(
"postgres", "host="+config["dbhost"]+
"user="+config["dbuser"]+
"dbname="+config["dbname"]+
"sslmode="+config["dbsslmode"])
if err != nil {
panic(err)
}
}
func initConfig() {
conf, err := ioutil.ReadFile(*configFile)
if err != nil {
panic(err)
}
err = json.Unmarshal(conf, &config)
if err != nil {
panic(err)
}
}
func main() {
initConfig()
initDb()
// Create a simple file server
fs := http.FileServer(http.Dir("./public"))
http.Handle("/", fs)
http.HandleFunc("/ws", handleConnections)
http.HandleFunc("/register/", handleRegistration)
// Start listening for incoming chat messages
go handleMessages()
// Start the server on localhost port 5000 and log any errors
log.Println("http server started on :5000")
err := http.ListenAndServe(":5000", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}