comm.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package main
  2. import (
  3. "2PPS/client"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "os"
  8. )
  9. var Port = ":3000"
  10. func main() {
  11. f, err := os.OpenFile("evalDataClient", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
  12. if err != nil {
  13. log.Fatalf("error opening file: %v", err)
  14. }
  15. defer f.Close()
  16. log.SetOutput(f)
  17. go client.Client(0, f)
  18. http.HandleFunc("/", ServeFiles)
  19. fmt.Println("Serving @ : ", "http://127.0.0.1"+Port)
  20. log.Fatal(http.ListenAndServe(Port, nil))
  21. }
  22. func ServeFiles(w http.ResponseWriter, r *http.Request) {
  23. switch r.Method {
  24. case "GET":
  25. path := r.URL.Path
  26. fmt.Println("path before: ", path)
  27. if path == "/" {
  28. path = "index.html"
  29. } else {
  30. path = "/home/simon/goCode/AnonymousTopicBasedPubSubCommunicationforMicroblogging" + path
  31. }
  32. fmt.Println("path after: ", path)
  33. http.ServeFile(w, r, path)
  34. case "POST":
  35. r.ParseMultipartForm(0)
  36. message := r.FormValue("message")
  37. fmt.Println("----------------------------------")
  38. fmt.Println("Message from Client: ", message)
  39. client.SetGuiTweet(message)
  40. // respond to client's request, dont think i need this
  41. //fmt.Fprintf(w, "Server: %s \n", message+" | "+time.Now().Format(time.RFC3339))
  42. default:
  43. fmt.Fprintf(w, "Request type other than GET or POST not supported")
  44. }
  45. }