From 15aeccdd4b3350a8aef9b1f4f7ed7e864e9ee59e Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Tue, 23 Apr 2019 00:30:12 +0300 Subject: [PATCH] move: wizards structs project to x-tba --- .../xxx-wizards-structs}/marshal/main.go | 19 +++++--- .../xxx-wizards-structs/server/list.tmpl.html | 5 ++ x-tba/xxx-wizards-structs/server/main.go | 46 +++++++++++++++++++ x-tba/xxx-wizards-structs/server/store.go | 32 +++++++++++++ x-tba/xxx-wizards-structs/server/templates.go | 11 +++++ .../xxx-wizards-structs}/unmarshal/main.go | 0 6 files changed, 107 insertions(+), 6 deletions(-) rename {24-structs/xxx-wizards => x-tba/xxx-wizards-structs}/marshal/main.go (52%) create mode 100644 x-tba/xxx-wizards-structs/server/list.tmpl.html create mode 100644 x-tba/xxx-wizards-structs/server/main.go create mode 100644 x-tba/xxx-wizards-structs/server/store.go create mode 100644 x-tba/xxx-wizards-structs/server/templates.go rename {24-structs/xxx-wizards => x-tba/xxx-wizards-structs}/unmarshal/main.go (100%) diff --git a/24-structs/xxx-wizards/marshal/main.go b/x-tba/xxx-wizards-structs/marshal/main.go similarity index 52% rename from 24-structs/xxx-wizards/marshal/main.go rename to x-tba/xxx-wizards-structs/marshal/main.go index a55f293..3958189 100644 --- a/24-structs/xxx-wizards/marshal/main.go +++ b/x-tba/xxx-wizards-structs/marshal/main.go @@ -1,3 +1,10 @@ +// For more tutorials: https://blog.learngoprogramming.com +// +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// + package main import ( @@ -8,9 +15,9 @@ import ( // Wizard is one of the greatest of people type Wizard struct { // name won't be marshalled (should be exported) - Name string `json:name` - Lastname string `json:"-"` - Nick string `json:"nick"` + Name string `json:"name,omitempty"` + Lastname string `json:"last_name"` + Nick string `json:"-"` } func main() { @@ -19,13 +26,13 @@ func main() { {Name: "Isaac", Lastname: "Newton", Nick: "apple"}, {Name: "Stephen", Lastname: "Hawking", Nick: "blackhole"}, {Name: "Marie", Lastname: "Curie", Nick: "radium"}, - {Name: "Charles", Lastname: "Darwin", Nick: "fittest"}, + {Name: "", Lastname: "Darwin", Nick: "fittest"}, } - bytes, err := json.Marshal(wizards) + bytes, err := json.MarshalIndent(wizards, "", "\t") if err != nil { panic(err) } - fmt.Print(string(bytes)) + fmt.Println(string(bytes)) } diff --git a/x-tba/xxx-wizards-structs/server/list.tmpl.html b/x-tba/xxx-wizards-structs/server/list.tmpl.html new file mode 100644 index 0000000..0343029 --- /dev/null +++ b/x-tba/xxx-wizards-structs/server/list.tmpl.html @@ -0,0 +1,5 @@ +

There are {{len .}} wizards.

+ +{{range .}} +👉 {{.Name}} {{.Lastname}} ({{.Nick}}) +{{end}} \ No newline at end of file diff --git a/x-tba/xxx-wizards-structs/server/main.go b/x-tba/xxx-wizards-structs/server/main.go new file mode 100644 index 0000000..6eab89a --- /dev/null +++ b/x-tba/xxx-wizards-structs/server/main.go @@ -0,0 +1,46 @@ +// For more tutorials: https://blog.learngoprogramming.com +// +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// + +package main + +import ( + "encoding/json" + "net/http" +) + +func main() { + http.HandleFunc("/add", onlyPost(add)) + http.HandleFunc("/list", list) + panic(http.ListenAndServe(":8000", nil)) +} + +func add(w http.ResponseWriter, r *http.Request) { + var wiz wizard + if err := json.NewDecoder(r.Body).Decode(&wiz); err != nil { + panic(err) + } + + db.add(wiz) + w.WriteHeader(http.StatusOK) +} + +func list(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html; charset=UTF-8") + w.WriteHeader(http.StatusOK) + tmpl.Execute(w, db.list()) +} + +func onlyPost(next http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if r.Method == "POST" { + next(w, r) + return + } + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("NOT OK")) + } +} diff --git a/x-tba/xxx-wizards-structs/server/store.go b/x-tba/xxx-wizards-structs/server/store.go new file mode 100644 index 0000000..96852d0 --- /dev/null +++ b/x-tba/xxx-wizards-structs/server/store.go @@ -0,0 +1,32 @@ +package main + +import "sync" + +type wizard struct { + Name string `json:"name"` + Lastname string `json:"last_name"` + Nick string `json:"nick"` +} + +type storage struct { + sync.RWMutex + wizards []wizard +} + +func (db *storage) add(w wizard) { + db.Lock() + defer db.Unlock() + db.wizards = append(db.wizards, w) +} + +func (db *storage) list() []wizard { + db.RLock() + defer db.RUnlock() + return db.wizards +} + +var db *storage + +func init() { + db = new(storage) +} diff --git a/x-tba/xxx-wizards-structs/server/templates.go b/x-tba/xxx-wizards-structs/server/templates.go new file mode 100644 index 0000000..9171f8d --- /dev/null +++ b/x-tba/xxx-wizards-structs/server/templates.go @@ -0,0 +1,11 @@ +package main + +import "html/template" + +var tmpl *template.Template + +func init() { + tmpl = template.Must( + template.New("list.tmpl"). + ParseFiles("list.tmpl")) +} diff --git a/24-structs/xxx-wizards/unmarshal/main.go b/x-tba/xxx-wizards-structs/unmarshal/main.go similarity index 100% rename from 24-structs/xxx-wizards/unmarshal/main.go rename to x-tba/xxx-wizards-structs/unmarshal/main.go