Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "html/template"
  6. "log"
  7. "net/http"
  8. "strings"
  9. )
  10.  
  11. func sayhelloName(w http.ResponseWriter, r *http.Request) {
  12. r.ParseForm() //Parse url parameters passed, then parse the response packet for the POST body (request body)
  13. // attention: If you do not call ParseForm method, the following data can not be obtained form
  14. fmt.Println(r.Form) // print information on server side.
  15. fmt.Println("path", r.URL.Path)
  16. fmt.Println("scheme", r.URL.Scheme)
  17. fmt.Println(r.Form["url_long"])
  18. for k, v := range r.Form {
  19. fmt.Println("key:", k)
  20. fmt.Println("val:", strings.Join(v, ""))
  21. }
  22. fmt.Fprintf(w, "Hello astaxie!") // write data to response
  23. }
  24.  
  25. func login(w http.ResponseWriter, r *http.Request) {
  26. fmt.Println("method:", r.Method) //get request method
  27. if r.Method == "GET" {
  28. t, _ := template.ParseFiles("login.gtpl")
  29. t.Execute(w, nil)
  30. } else {
  31. r.ParseForm()
  32. // logic part of log in
  33. fmt.Println("username:", r.Form["username"])
  34. fmt.Println("password:", r.Form["password"])
  35. }
  36. }
  37.  
  38. func main() {
  39. http.HandleFunc("/", sayhelloName) // setting router rule
  40. http.HandleFunc("/login", login)
  41. err := http.ListenAndServe(":9090", nil) // setting listening port
  42. if err != nil {
  43. log.Fatal("ListenAndServe: ", err)
  44. }
  45. }
  46.  
  47. <html>
  48. <head>
  49. <title></title>
  50. </head>
  51. <body>
  52. <form action="/login" method="post">
  53. Username:<input type="text" name="username">
  54. Password:<input type="password" name="password">
  55. <input type="submit" value="Login">
  56. </form>
  57. </body>
  58. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement