commit 7536cde4282c9672ab0ca892c100dfa217297178 Author: Piotr Biernat Date: Tue Sep 15 23:09:07 2020 +0200 Initial commit diff --git a/articles.go b/articles.go new file mode 100644 index 0000000..edada5f --- /dev/null +++ b/articles.go @@ -0,0 +1,28 @@ +package main + +import ( + "encoding/json" + "net/http" +) + +// Article type +type Article struct { + Title string `json:"title"` + Description string `json:"description"` + Content string `json:"content"` +} + +// Articles Collection +type Articles []Article + +func getAllArticles() http.Handler { + fn := func(w http.ResponseWriter, r *http.Request) { + articles := Articles{ + Article{Title: "Test Title", Description: "Test Description", Content: "Hello content!"}, + } + + json.NewEncoder(w).Encode(articles) + } + + return http.HandlerFunc(fn) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..b789894 --- /dev/null +++ b/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("Hello world!") + + s := Server{ + port: ":3000", + } + s.serve() +} diff --git a/server.go b/server.go new file mode 100644 index 0000000..47cebaf --- /dev/null +++ b/server.go @@ -0,0 +1,28 @@ +package main + +import ( + "log" + "net/http" +) + +// Server base struct +type Server struct { + port string +} + +func (s *Server) serve() { + mux := http.NewServeMux() + mux.Handle("/", s.handle()) + mux.Handle("/articles", getAllArticles()) + + log.Print("Listening on", s.port) + log.Fatal(http.ListenAndServe(s.port, mux)) +} + +func (s *Server) handle() http.Handler { + fn := func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(string("Hello World"))) + } + + return http.HandlerFunc(fn) +}