Created
July 27, 2018 20:01
-
-
Save nclundsten/46aafbe50d9decb1c59729e1311c6422 to your computer and use it in GitHub Desktop.
i suck at golang
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
package main | |
// localhost:8080/create?first=myfirstname | |
import ( | |
"net/http" | |
"fmt" | |
) | |
// ----person---- | |
type person struct { | |
id uint64 | |
first string | |
last string | |
} | |
// ----controller---- | |
type controller struct { | |
mapper mapper | |
} | |
func NewController(mapper mapper) controller { | |
var controller controller | |
controller.mapper = mapper | |
return controller | |
} | |
func (c controller) create (w http.ResponseWriter, r *http.Request) { | |
r.ParseForm() | |
var person person | |
person.first = fmt.Sprintf("%s", r.Form["first"]) | |
c.mapper.create(person) | |
} | |
func (c controller) read (w http.ResponseWriter, r *http.Request) { | |
} | |
func (c controller) update (w http.ResponseWriter, r *http.Request) { | |
r.ParseForm() | |
var person person | |
c.mapper.update(person) | |
} | |
func (c controller) delete (w http.ResponseWriter, r *http.Request) { | |
} | |
func (c controller) list (w http.ResponseWriter, r *http.Request) { | |
} | |
// -----main----- | |
func main() { | |
mapper := NewMapper("asdf") | |
controller := NewController(mapper) | |
// routing | |
http.HandleFunc("/create", func(w http.ResponseWriter, r *http.Request) { | |
controller.create(w,r) | |
}) | |
http.HandleFunc("/read", func(w http.ResponseWriter, r *http.Request) { | |
controller.read(w,r) | |
}) | |
http.HandleFunc("/update", func(w http.ResponseWriter, r *http.Request) { | |
controller.update(w,r) | |
}) | |
http.HandleFunc("/delete", func(w http.ResponseWriter, r *http.Request) { | |
controller.delete(w,r) | |
}) | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
controller.list(w,r) | |
}) | |
if err := http.ListenAndServe(":8080", nil); err != nil { | |
panic(err) | |
} | |
} | |
// ----mapper---- | |
type mapper struct { | |
} | |
func NewMapper(dsn string) mapper { | |
var mapper mapper | |
return mapper | |
} | |
func (m mapper) create(person person) { | |
fmt.Println("create") | |
fmt.Printf("%+v\n", person) | |
} | |
func (m mapper) read(id uint64) { | |
fmt.Println("read") | |
} | |
func (m mapper) update(person person) { | |
fmt.Printf("%+v\n", person) | |
fmt.Println("update") | |
} | |
func (m mapper) delete(id uint64) { | |
fmt.Println("delete") | |
} | |
func (m mapper) list() { | |
fmt.Println("list") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment