-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
226 lines (179 loc) · 5.77 KB
/
main.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"encoding/base64"
"fmt"
// "bufio"
// "os"
// "strings"
"io"
"encoding/json"
"time"
"log"
"net/http"
"github.com/gorilla/websocket"
)
// FetchCall data struct
type FetchCall struct {
Content string `json:"content"`
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool { return true },
}
// define a reader which will listen for
// new messages being sent to our WebSocket
// endpoint
func reader(conn *websocket.Conn) {
for {
// read in a message
messageType, p, err := conn.ReadMessage()
if err != nil {
log.Println(err)
return
}
// print out that message for clarity
fmt.Println(string(p))
if err := conn.WriteMessage(messageType, p); err != nil {
log.Println(err)
return
}
}
}
// define our WebSocket endpoint
func serveWs(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Host)
// upgrade this connection to a WebSocket
// connection
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
}
// listen indefinitely for new messages coming
// through on our WebSocket connection
reader(ws)
}
func setupRoutes() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Simple Server")
})
// make our `/ws` endpoint to the `serveWs` function
http.HandleFunc("/ws", serveWs)
http.HandleFunc("/encode", encodeREST)
}
func encodeREST(w http.ResponseWriter, r *http.Request) {
AddCors(&w)
decoder := json.NewDecoder(r.Body)
var data FetchCall
err := decoder.Decode(&data)
if err != nil {
panic(err)
}
text := oldMainCode(data.Content)
// wring the text to the response writer
io.WriteString(w, string(text))
// below is for if we want to return as a JSON, going to just send as string for now
//json.NewEncoder(w).Encode(text)
// Commenting out this below for now since we're not sending the response as a JSON
/*pagesJson, err := json.Marshal(text)
if err != nil {
log.Fatal("Cannot encode to JSON ", err)
}
fmt.Printf("%s", pagesJson)*/
}
func main() {
setupRoutes()
log.Fatal(http.ListenAndServe(":8080", nil))
}
func oldMainCode(input string) string{
// Ingest data through console
// -new- will have to get user value from UI
// reader := bufio.NewReader(os.Stdin)
// fmt.Print("Enter text to Encode to Base64: ")
// text, _ := reader.ReadString('\n')
// Remove Carriage Return from ingested data
// text = strings.TrimSuffix(text, "\n")
text := input
// Begin clock for golang library
startLib := time.Now()
// Encode string in Base64 with Golang Library for comparison to our implementation
libraryEncode := libraryEncode(text)
// Capturing golang library runtime
elapsedLib := time.Since(startLib)
// Begin clock for golang library
startImp := time.Now()
// Comparing our implementation with golang's for validation
valid := compareImplementations(implementedEncode(text), libraryEncode)
if(!valid) {
fmt.Println("Failed case in implementedEncode")
}
// Capturing golang library runtime
elapsedImp := time.Since(startImp)
// Printing run time for statistics
fmt.Printf("Encoding to Base64 with Golang library took %s. While encoding to Base64 with Implemented Algorithm took %s .", elapsedLib, elapsedImp)
fmt.Println()
return libraryEncode
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}
func libraryEncode(msg string) string{
encoded := base64.StdEncoding.EncodeToString([]byte(msg))
fmt.Println("Golang Libray Base64 Encode: ")
fmt.Println(encoded)
fmt.Println()
return encoded
}
func implementedEncode(msg string) string{
overage := len(msg) % 3
padding := 0
if (overage > 0) {
// Add empty characters to our string based on how much padding is needed
for i := overage; i < 3; i++ {
padding++
msg = msg + "\000"
}
}
// String codex to map 6 bit base64 character values to
base64Codex := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
encodedString := ""
/* Iterate through the string and convert 3 chars at a time for conversion from
3 8bit numbers to 4 6bit numbers */
for i := 0; i < len(msg); i+=3 {
// Adding three 8 bit numbers into one 24 bit number
n := (int(msg[i]) << 16) + (int(msg[i+1]) << 8) + int(msg[i+2])
// Parsing the 24 bit number into four 6 bit numbers we can map to our codex
n1 := (n >> 18) & 63
n2 := (n >> 12) & 63
n3 := (n >> 6) & 63
n4 := (n) & 63
// Mapping the 6 bit values to our codex string
encodedString += "" + string(base64Codex[n1]) + string(base64Codex[n2]) + string(base64Codex[n3]) + string(base64Codex[n4])
}
// Removing chars from end of string based on how much padding was needed
encodedStringFinal := encodedString[:len(encodedString)-(padding)]
// Adding '=' based on how many characters were removed
for i := 0; i < padding; i++ {
encodedStringFinal = encodedStringFinal + "="
}
fmt.Println("Implemented Base64 Encode: ")
fmt.Println(encodedStringFinal)
fmt.Println()
return encodedStringFinal
}
func compareImplementations(implemented string, library string ) bool{
flag := false
if(implemented == library) {
fmt.Println("Test against Golang library implementation passed")
flag = true
} else {
fmt.Println("Implemented Encode failed in the check against Golang Library Encode.")
}
return flag
}
func AddCors(w *http.ResponseWriter) {
//Allow CORS here By * or specific origin
(*w).Header().Set("Access-Control-Allow-Origin", "*")
(*w).Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
(*w).Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
}