You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.3 KiB
Go
77 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/md5"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/cgi"
|
|
|
|
"faculty_media_report/dbi"
|
|
"faculty_media_report/pages"
|
|
)
|
|
|
|
func writeHTML(w http.ResponseWriter, html string) {
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
fmt.Fprint(w, html)
|
|
}
|
|
|
|
func handleLoginGet(w http.ResponseWriter, r *http.Request) {
|
|
writeHTML(w, pages.LoginPage(""))
|
|
}
|
|
|
|
func handleLoginPost(w http.ResponseWriter, r *http.Request) {
|
|
username := r.FormValue("username")
|
|
password := r.FormValue("password")
|
|
|
|
fail := func() {
|
|
writeHTML(w, pages.LoginPage("Username or Password is incorrect."))
|
|
}
|
|
|
|
if username == "" || password == "" {
|
|
fail()
|
|
return
|
|
}
|
|
|
|
db, err := dbi.GetDbConn()
|
|
if err != nil {
|
|
fail()
|
|
return
|
|
}
|
|
defer db.Close()
|
|
|
|
conn, err := db.Conn(context.Background())
|
|
if err != nil {
|
|
fail()
|
|
return
|
|
}
|
|
defer conn.Close()
|
|
|
|
user, err := dbi.GetUser(conn, username)
|
|
if err != nil {
|
|
fail()
|
|
return
|
|
}
|
|
|
|
hash := md5.Sum([]byte(password))
|
|
if fmt.Sprintf("%x", hash) != user.Password {
|
|
fail()
|
|
return
|
|
}
|
|
|
|
token, err := dbi.GenJWT(user)
|
|
if err != nil {
|
|
fail()
|
|
return
|
|
}
|
|
|
|
writeHTML(w, pages.MainFormPage(token))
|
|
}
|
|
|
|
func main() {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("GET /faculty/activity/login", handleLoginGet)
|
|
mux.HandleFunc("POST /faculty/activity/login", handleLoginPost)
|
|
cgi.Serve(mux)
|
|
}
|