main.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package main
  2. import (
  3. "encoding/json"
  4. "flag"
  5. "log"
  6. "net/http"
  7. "github.com/gorilla/sessions"
  8. )
  9. var store = sessions.NewCookieStore([]byte("secret-key"))
  10. type Status struct {
  11. LoggedIn bool
  12. }
  13. type Password struct {
  14. Matched bool
  15. }
  16. func loginHandler(rw http.ResponseWriter, req *http.Request) {
  17. if req.Method == "GET" {
  18. password := req.FormValue("password")
  19. log.Println("The typed password is:" + password)
  20. var typed Password
  21. if password == "admin" {
  22. log.Println("Password is correct")
  23. session, err := store.Get(req, "session-name")
  24. if err != nil {
  25. http.Error(rw, err.Error(), http.StatusInternalServerError)
  26. return
  27. }
  28. session.Values["auth"] = true
  29. err = session.Save(req, rw)
  30. if err != nil {
  31. http.Error(rw, err.Error(), http.StatusInternalServerError)
  32. return
  33. }
  34. typed.Matched = true
  35. log.Println("Logged in")
  36. } else {
  37. typed.Matched = false
  38. log.Println("WRONG password!!")
  39. }
  40. send_message, _ := json.Marshal(typed)
  41. rw.Write(send_message)
  42. }
  43. }
  44. func logoutHandler(rw http.ResponseWriter, req *http.Request) {
  45. session, err := store.Get(req, "session-name")
  46. if err != nil {
  47. http.Error(rw, err.Error(), http.StatusInternalServerError)
  48. return
  49. }
  50. session.Values["auth"] = nil
  51. err = session.Save(req, rw)
  52. if err != nil {
  53. http.Error(rw, err.Error(), http.StatusInternalServerError)
  54. return
  55. }
  56. http.Redirect(rw, req, "/login.html", http.StatusSeeOther)
  57. log.Println("Logged out successfully")
  58. }
  59. func checkloginHandler(rw http.ResponseWriter, req *http.Request) {
  60. log.Println("Check button is clicked")
  61. session, err := store.Get(req, "session-name")
  62. if err != nil {
  63. http.Error(rw, err.Error(), http.StatusInternalServerError)
  64. return
  65. }
  66. var result Status
  67. if session.Values["auth"] == true {
  68. log.Println("Checked that auth = true")
  69. result.LoggedIn = true
  70. } else {
  71. log.Println("Checked that auth = false")
  72. result.LoggedIn = false
  73. }
  74. send_message, _ := json.Marshal(result)
  75. rw.Write(send_message)
  76. }
  77. func main() {
  78. portPointer := flag.String("port", "8000", "An integer")
  79. flag.Parse()
  80. log.Println("Port Number: " + *portPointer)
  81. httpFileServer := http.FileServer(http.Dir("./files"))
  82. http.Handle("/", httpFileServer)
  83. http.HandleFunc("/login", loginHandler)
  84. http.HandleFunc("/logout", logoutHandler)
  85. http.HandleFunc("/checklogin", checkloginHandler)
  86. log.Printf("Listening http://localhost:%s\n", *portPointer)
  87. if error := http.ListenAndServe(":"+*portPointer, nil); error != nil {
  88. log.Printf("Error: %s\n", error)
  89. }
  90. }