Initial commit

This commit is contained in:
Piotr Biernat 2020-09-15 23:09:07 +02:00
commit 7536cde428
3 changed files with 70 additions and 0 deletions

28
articles.go Normal file
View File

@ -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)
}

14
main.go Normal file
View File

@ -0,0 +1,14 @@
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello world!")
s := Server{
port: ":3000",
}
s.serve()
}

28
server.go Normal file
View File

@ -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)
}