package main import ( "fmt" "log" "net/http" "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") fmt.Println(r.URL.Path); tmp := strings.Split(r.URL.Path, "/"); fmt.Println(tmp); if tmp[1] == "add" && len(tmp[2]) != 0 { add_url := tmp[2]; fmt.Println(tmp[2]); for i := 3; i < len(tmp); i++ { fmt.Println(tmp[3]); if tmp[i] == "" { break } add_url += "/" + tmp[i] } fmt.Println("Request add:"); fmt.Println("Sending a request to add: " + add_url + "..."); conn, err := net.Dial("tcp", "localhost:5555"); if err != nil { log.Fatal(err); } defer conn.Close(); fmt.Fprintf(conn, "{\"type\": 1, \"url\": \"%s\"}", add_url); 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); res, _ := bufio.NewReader(conn).ReadBytes('\n'); fmt.Println(string(res)); http.Redirect(w, r, string(res), http.StatusPermanentRedirect); }else{ //fmt.Fprintf(w, "oi", r.URL.Path[1:]); fs := http.FileServer(http.Dir("../web_page")) http.StripPrefix("/", fs).ServeHTTP(w, r) } } 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() { /*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()); }