Skip to content

Instantly share code, notes, and snippets.

@masa-x
Created February 1, 2018 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masa-x/06a3293e56cc804699e8af3fa074d19e to your computer and use it in GitHub Desktop.
Save masa-x/06a3293e56cc804699e8af3fa074d19e to your computer and use it in GitHub Desktop.
go web sample
<html>
<head></head>
<body>
{{.DisplayArea}}
<form method="POST" action="/">
<input type="text" name="amount_of_money" />
<input type="submit" value="Deposit" name="action" />
<input type="submit" value="Withdrawal" name="action" />
<input type="submit" value="Balance Inquiry" name="action" />
</form>
</body>
</html>
package externalinterface
import (
"log"
"sync"
"text/template"
"path/filepath"
"net/http"
"../interfaceadapter"
)
type Web struct{}
func(r Web)Start(){
http.Handle("/", &homeHandler{})
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}
type homeHandler struct{
mOnce sync.Once
mTemplate *template.Template
}
func (r homeHandler) ServeHTTP(aWriter http.ResponseWriter, aRequest *http.Request){
r.mOnce.Do(func(){
r.mTemplate = template.Must(template.ParseFiles(filepath.Join("externalinterface", "templates", "home.html")))
})
displayArea := resultDisplayArea{}
var controller = interfaceadapter.CreateAccountsController(createBalanceDataAccessor(".\\balance.txt"),
interfaceadapter.NewWebPresenter(&displayArea))
aRequest.ParseForm()
action := aRequest.Form.Get("action")
amountOfMoney := aRequest.Form.Get("amount_of_money")
if action == "Deposit"{
controller.Deposit(amountOfMoney)
} else if action == "Withdrawal" {
controller.Withdrawal(amountOfMoney)
} else if action == "Balance Inquiry" {
controller.BalanceInquiry()
}
r.mTemplate.Execute(aWriter, displayArea)
}
type resultDisplayArea struct{
DisplayArea string
}
func(r *resultDisplayArea)Display(aData string){
r.DisplayArea += aData + "\n"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment