-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update feat/learning-hacking-with-golang-20240708 1148
- Loading branch information
1 parent
064d95e
commit e72f650
Showing
6 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Money Transfer</title> | ||
</head> | ||
<body> | ||
<h1>Money Transfer</h1> | ||
<form action="/" method="post"> | ||
<label for="amount">Amount:</label> | ||
<input type="text" id="amount" name="amount" required> | ||
<br> | ||
<label for="account">Account:</label> | ||
<input type="text" id="account" name="account" required> | ||
<br> | ||
<input type="submit" value="Transfer"> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"html/template" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
var ( | ||
templates = template.Must(template.ParseFiles("index.html")) | ||
) | ||
|
||
func main() { | ||
http.HandleFunc("/", indexHandler) | ||
http.HandleFunc("/transfer", transferHandler) | ||
log.Fatal(http.ListenAndServe(":8080", nil)) | ||
} | ||
|
||
func indexHandler(w http.ResponseWriter, r *http.Request) { | ||
if r.Method == http.MethodGet { | ||
templates.ExecuteTemplate(w, "index.html", nil) | ||
} else if r.Method == http.MethodPost { | ||
amount := r.FormValue("amount") | ||
account := r.FormValue("account") | ||
|
||
// Perform the money transfer | ||
if transferMoney(amount, account) { | ||
fmt.Fprintln(w, "Transfer successful!") | ||
} else { | ||
fmt.Fprintln(w, "Transfer failed!") | ||
} | ||
} | ||
} | ||
|
||
func transferHandler(w http.ResponseWriter, r *http.Request) { | ||
// Process transfer request | ||
// ... | ||
} | ||
|
||
func transferMoney(amount, account string) bool { | ||
// Perform money transfer logic | ||
// ... | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"html/template" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
var ( | ||
templates = template.Must(template.ParseFiles("index.html")) | ||
) | ||
|
||
func main() { | ||
http.HandleFunc("/", indexHandler) | ||
http.HandleFunc("/transfer", transferHandler) | ||
log.Fatal(http.ListenAndServe(":8080", nil)) | ||
} | ||
|
||
func indexHandler(w http.ResponseWriter, r *http.Request) { | ||
if r.Method == http.MethodGet { | ||
templates.ExecuteTemplate(w, "index.html", nil) | ||
} else if r.Method == http.MethodPost { | ||
amount := r.FormValue("amount") | ||
account := r.FormValue("account") | ||
|
||
// Perform the money transfer | ||
if transferMoney(amount, account) { | ||
fmt.Fprintln(w, "Transfer successful!") | ||
} else { | ||
fmt.Fprintln(w, "Transfer failed!") | ||
} | ||
} | ||
} | ||
|
||
func transferHandler(w http.ResponseWriter, r *http.Request) { | ||
// Additional transfer processing logic can be placed here if needed | ||
} | ||
|
||
func transferMoney(amount, account string) bool { | ||
// Simulate a successful transfer | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"html/template" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/gorilla/csrf" | ||
) | ||
|
||
var ( | ||
templates = template.Must(template.ParseFiles("index.html")) | ||
) | ||
|
||
func main() { | ||
http.HandleFunc("/", indexHandler) | ||
http.HandleFunc("/transfer", transferHandler) | ||
log.Fatal(http.ListenAndServe(":8080", csrf.Protect([]byte("32-byte-long-auth-key"))(nil))) | ||
} | ||
|
||
func indexHandler(w http.ResponseWriter, r *http.Request) { | ||
if r.Method == http.MethodGet { | ||
token := csrf.Token(r) | ||
data := struct { | ||
Token string | ||
}{ | ||
Token: token, | ||
} | ||
templates.ExecuteTemplate(w, "index.html", data) | ||
} else if r.Method == http.MethodPost { | ||
if err := r.ParseForm(); err != nil { | ||
http.Error(w, "Bad Request", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
// Validate CSRF token | ||
if err := csrf.Protect([]byte("32-byte-long-auth-key")).VerifyToken(csrf.Token(r)); err != nil { | ||
http.Error(w, "Invalid CSRF token", http.StatusForbidden) | ||
return | ||
} | ||
|
||
amount := r.FormValue("amount") | ||
account := r.FormValue("account") | ||
|
||
// Perform the money transfer | ||
if transferMoney(amount, account) { | ||
fmt.Fprintln(w, "Transfer successful!") | ||
} else { | ||
fmt.Fprintln(w, "Transfer failed!") | ||
} | ||
} | ||
} | ||
|
||
func transferHandler(w http.ResponseWriter, r *http.Request) { | ||
// Process transfer request | ||
// ... | ||
} | ||
|
||
func transferMoney(amount, account string) bool { | ||
// Perform money transfer logic | ||
// ... | ||
return false | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
### go run main.go | ||
``` | ||
go run main.go | ||
curl http://localhost:8080 | ||
curl -X POST -d "amount=100" -d "account=12345" http://localhost:8080 | ||
``` | ||
|
||
### go run main1.go | ||
``` | ||
go run main1.go | ||
curl http://localhost:8080 | ||
curl -X POST -d "amount=100" -d "account=12345" http://localhost:8080 | ||
``` | ||
### go run main2.go | ||
``` | ||
go run main2.go | ||
curl http://localhost:8080 | ||
curl -X POST -d "amount=100" -d "account=12345" http://localhost:8080 | ||
``` | ||
### go run main3.go | ||
``` | ||
go run main3.go | ||
curl http://localhost:8080 | ||
curl -X POST -d "amount=100" -d "account=12345" http://localhost:8080 | ||
``` |