comm.go 983 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "time"
  7. )
  8. var Port = ":3000"
  9. func main() {
  10. http.HandleFunc("/", ServeFiles)
  11. fmt.Println("Serving @ : ", "http://127.0.0.1"+Port)
  12. log.Fatal(http.ListenAndServe(Port, nil))
  13. }
  14. func ServeFiles(w http.ResponseWriter, r *http.Request) {
  15. switch r.Method {
  16. case "GET":
  17. path := r.URL.Path
  18. fmt.Println("path before: ", path)
  19. if path == "/" {
  20. path = "index.html"
  21. } else {
  22. path = "/home/simon/goCode/AnonymousTopicBasedPubSubCommunicationforMicroblogging/main.js"
  23. }
  24. fmt.Println("path after: ", path)
  25. http.ServeFile(w, r, path)
  26. case "POST":
  27. r.ParseMultipartForm(0)
  28. message := r.FormValue("message")
  29. fmt.Println("----------------------------------")
  30. fmt.Println("Message from Client: ", message)
  31. // respond to client's request
  32. fmt.Fprintf(w, "Server: %s \n", message+" | "+time.Now().Format(time.RFC3339))
  33. default:
  34. fmt.Fprintf(w, "Request type other than GET or POST not supported")
  35. }
  36. }