Advertisement
connelblaze

Untitled

Jun 29th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.26 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "net/http"
  5.     "html/template"
  6.     "fmt"
  7.     "database/sql"
  8.     _ "github.com/go-sql-driver/mysql"
  9.     "golang.org/x/crypto/bcrypt"
  10. )
  11.  
  12. var (
  13.     templ *template.Template
  14.     err error
  15.     db *sql.DB
  16. )
  17.  
  18. func init() {
  19.     templ = template.Must(template.ParseGlob("templates/*.html"))
  20. }
  21.  
  22. func main() {
  23.     handleDbConnection()
  24.     http.Handle("/assets/", http.FileServer(http.Dir(".")))
  25.     http.HandleFunc("/", index)
  26.     http.HandleFunc("/login", login)
  27.     http.HandleFunc("/profile", profile)
  28.     fmt.Println("server running")
  29.     http.ListenAndServe(":8080", nil)
  30. }
  31.  
  32. func handleDbConnection()  {
  33.     // Create an sql.DB and check for errors
  34.     db, err := sql.Open("mysql", "root:password@/GoStaffManager")
  35.     if err != nil {
  36.         panic(err.Error())
  37.     }
  38.     // sql.DB should be long lived "defer" closes it once this function ends
  39.     defer db.Close()
  40.  
  41.     // Test the connection to the database
  42.     err = db.Ping()
  43.     if err != nil {
  44.         panic(err.Error())
  45.     } else {
  46.         fmt.Println("working...")
  47.     }
  48. }
  49.  
  50. func index(res http.ResponseWriter, req *http.Request)  {
  51.     err = templ.ExecuteTemplate(res, "index.html", nil)
  52.     if err != nil {
  53.         fmt.Sprint("error", err)
  54.     }
  55. }
  56.  
  57. func login(res http.ResponseWriter, req *http.Request)  {
  58.     err = templ.ExecuteTemplate(res, "login.html", nil)
  59.     if err != nil {
  60.         fmt.Sprint("error", err)
  61.     }
  62.  
  63.     // Grab the username/password from the submitted post form
  64.     username := req.FormValue("username")
  65.     password := req.FormValue("password")
  66.  
  67.     // Grab from the database
  68.     var (
  69.         uname string
  70.         pword string
  71.     )
  72.  
  73.     // Search the database for the username provided
  74.     // If it exists grab the password for validation
  75.     err := db.QueryRow("SELECT username, password FROM lecturers WHERE username=?", username).Scan(&uname, &pword)
  76.     // If not then redirect to the login page
  77.     if err != nil {
  78.         http.Redirect(res, req, "/login", 301)
  79.         return
  80.     }
  81.     // Validate the password
  82.     err = bcrypt.CompareHashAndPassword([]byte(pword), []byte(password))
  83.     // If wrong password redirect to the login
  84.     if err != nil {
  85.         http.Redirect(res, req, "/login", 301)
  86.         return
  87.     }
  88.  
  89.     http.Redirect(res, req, "/profile", 301)
  90. }
  91.  
  92. func profile(res http.ResponseWriter, req *http.Request)  {
  93.     err = templ.ExecuteTemplate(res, "profile.html", nil)
  94.     if err != nil {
  95.         fmt.Sprint("error", err)
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement