123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- package main
- import (
- "flag"
- "log"
- "net/http"
- )
- var loggedIn bool = false
- func checklogin() []byte {
- if loggedIn == true {
- return []byte(`{"loggedIn": true}`)
- } else {
- return []byte(`{"loggedIn": false}`)
- }
- }
- func loginHandler(rw http.ResponseWriter, req *http.Request) {
- if req.Method == "GET" {
- password := req.FormValue("password")
- log.Println("The typed password is:" + password)
- if password == "admin" {
- log.Println("Password is correct")
- rw.Write([]byte("true"))
- loggedIn = true
- log.Println("Logged in")
- } else {
- log.Println("WRONG password!!")
- }
- }
- }
- func logoutHandler(rw http.ResponseWriter, req *http.Request) {
- loggedIn = false
- http.Redirect(rw, req, "/login.html", http.StatusSeeOther)
- log.Println("Logged out successfully")
- }
- func checkloginHandler(rw http.ResponseWriter, req *http.Request) {
- rw.Write(checklogin())
- log.Println("Check button is clicked")
- }
- func main() {
- portPointer := flag.String("port", "8000", "An integer")
- flag.Parse()
- log.Println("Port Number: " + *portPointer)
- httpFileServer := http.FileServer(http.Dir("./files"))
- http.Handle("/", httpFileServer)
- http.HandleFunc("/login", loginHandler)
- http.HandleFunc("/logout", logoutHandler)
- http.HandleFunc("/checklogin", checkloginHandler)
- log.Printf("Listening http://localhost:%s\n", *portPointer)
- if error := http.ListenAndServe(":"+*portPointer, nil); error != nil {
- log.Printf("Error: %s\n", error)
- }
- }
|