summaryrefslogtreecommitdiff
path: root/web_server/web_server.go
diff options
context:
space:
mode:
Diffstat (limited to 'web_server/web_server.go')
-rw-r--r--web_server/web_server.go45
1 files changed, 42 insertions, 3 deletions
diff --git a/web_server/web_server.go b/web_server/web_server.go
index 7f1201c..0bf9306 100644
--- a/web_server/web_server.go
+++ b/web_server/web_server.go
@@ -7,21 +7,43 @@ import (
"strings"
"net"
"bufio"
+ //"html"
)
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Expires", "0")
+ w.Header().Set("Access-Control-Allow-Origin", "*")
+ w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
+ w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
+
tmp := strings.Split(r.URL.Path, "/");
fmt.Println(tmp);
- if len(tmp[1]) == 6 {
+ if tmp[1] == "add" && len(tmp[2]) != 0 {
+ fmt.Println("Request add:");
+ fmt.Println("Sending a request to add: " + tmp[2] + "...");
+ conn, err := net.Dial("tcp", "localhost:5555");
+ if err != nil {
+ log.Fatal(err);
+ }
+ defer conn.Close();
+
+ fmt.Fprintf(conn, "{\"type\": 1, \"url\": \"%s\"}", tmp[2]);
+ res, _ := bufio.NewReader(conn).ReadBytes('\n');
+ fmt.Println(string(res));
+
+ w.Write(res);
+
+ return;
+ }else if len(tmp[1]) == 6 {
s := tmp[1];
fmt.Println(tmp[1]);
conn, err := net.Dial("tcp", "localhost:5555");
if err != nil {
log.Fatal(err);
}
+ defer conn.Close();
fmt.Println(s);
fmt.Fprintf(conn, "{\"type\": 2, \"url\": \"%s\"}", s);
@@ -34,14 +56,31 @@ func handler(w http.ResponseWriter, r *http.Request) {
}
}
+func add_page(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
+ w.Header().Set("Expires", "0")
+ tmp := strings.Split(r.URL.Path, "/");
+ fmt.Println(tmp);
+}
+
func main() {
-// page := &Page{Title: "Redirect", Body: []byte("Redirecting...")};
- fmt.Println("Port 5556");
+ /*fmt.Println("Port 5556");
server := &http.Server{
Addr: "localhost:5556",
Handler: http.HandlerFunc(handler),
};
+ server.SetKeepAlivesEnabled(false);*/
+
+ mux := http.NewServeMux();
+ mux.HandleFunc("/", handler);
+ //mux.HandleFunc("/add", add_page);
+ server := &http.Server{
+ Addr: "localhost:5556",
+ Handler: mux,
+ };
server.SetKeepAlivesEnabled(false);
+ fmt.Println("Listen on port 5556");
+
//http.HandleFunc("/", handler);
log.Fatal(server.ListenAndServe());
}