diff --git a/.gitignore b/.gitignore index 8f5fd87..6a09e2d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .history + +build diff --git a/handler/articles.go b/handler/articles.go new file mode 100644 index 0000000..1564f1f --- /dev/null +++ b/handler/articles.go @@ -0,0 +1,43 @@ +package handler + +import ( + "encoding/json" + "go-rest-api/model" + "log" + "net/http" + + "github.com/gorilla/mux" +) + +func getAllArticles(w http.ResponseWriter, r *http.Request) { + articles := model.Articles{ + model.Article{Title: "Test Title", Description: "Test Description", Content: "Hello content!"}, + } + + json.NewEncoder(w).Encode(articles) + log.Println("Served /articles") +} + +func getOneArticle(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + log.Println(vars) + log.Println("Get single article") +} + +func createArticle(w http.ResponseWriter, r *http.Request) { + log.Println("Create article") +} + +func updateArticle(w http.ResponseWriter, r *http.Request) { + log.Println("Update article") +} + +// AttachArticleHandlersToRouter func +func AttachArticleHandlersToRouter(root *mux.Router) { + router := root.PathPrefix("/articles").Subrouter() + router.HandleFunc("", getAllArticles).Methods(http.MethodGet) + router.HandleFunc("/{id:[0-9]+}", getOneArticle).Methods(http.MethodGet) + router.HandleFunc("", createArticle).Methods(http.MethodPost) + router.HandleFunc("/{id:[0-9]+}", updateArticle).Methods(http.MethodPut) +} diff --git a/handler_articles.go b/handler_articles.go deleted file mode 100644 index 5e165ce..0000000 --- a/handler_articles.go +++ /dev/null @@ -1,30 +0,0 @@ -package main - -import ( - "encoding/json" - "log" - "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) - log.Println("Served /articles") - } - - return http.HandlerFunc(fn) -} diff --git a/main.go b/main.go index e6c1944..bcd0a64 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "go-rest-api/server" "log" "os" "os/signal" @@ -12,12 +13,10 @@ import ( func main() { fmt.Println("Hello world!") - s := Server{ - port: ":9999", - } + s := server.NewServer(":9999") go func() { - s.serve() + s.Serve() }() signCh := make(chan os.Signal) @@ -28,5 +27,5 @@ func main() { log.Println("Received terminal, graceful shutdown", sig) tc, _ := context.WithTimeout(context.Background(), 30*time.Second) - s.shutdown(tc) + s.Shutdown(tc) } diff --git a/modd.conf b/modd.conf index 2d5bd4b..d76bc78 100644 --- a/modd.conf +++ b/modd.conf @@ -1,4 +1,4 @@ **/*.go !**/*_test.go { - prep: go build -o build main.go server.go handler_*.go + prep: go build -o build main.go daemon +sigterm: ./build } \ No newline at end of file diff --git a/model/article.go b/model/article.go new file mode 100644 index 0000000..17743b7 --- /dev/null +++ b/model/article.go @@ -0,0 +1,11 @@ +package model + +// Article type +type Article struct { + Title string `json:"title"` + Description string `json:"description"` + Content string `json:"content"` +} + +// Articles Collection +type Articles []Article diff --git a/server.go b/server.go deleted file mode 100644 index 331209b..0000000 --- a/server.go +++ /dev/null @@ -1,49 +0,0 @@ -package main - -import ( - "context" - "log" - "net/http" - "time" - - "github.com/gorilla/mux" -) - -// Server base struct -type Server struct { - port string - server http.Server -} - -func (s *Server) serve() { - sm := mux.NewRouter() - sm.Handle("/", s.handle()) - sm.Handle("/articles", getAllArticles()) - - serv := &http.Server{ - Addr: s.port, - Handler: sm, - IdleTimeout: 120 * time.Second, - ReadTimeout: 1 * time.Second, - WriteTimeout: 1 * time.Second, - } - s.server = *serv - log.Print("Listening on", s.port) - - err := serv.ListenAndServe() - if err != nil { - log.Fatal(err) - } -} - -func (s *Server) handle() http.Handler { - fn := func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(string("Hello World"))) - } - - return http.HandlerFunc(fn) -} - -func (s *Server) shutdown(tc context.Context) { - s.server.Shutdown(tc) -} diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..9427e31 --- /dev/null +++ b/server/server.go @@ -0,0 +1,56 @@ +package server + +import ( + "context" + "go-rest-api/handler" + "log" + "net/http" + "time" + + "github.com/gorilla/mux" +) + +// Server struct +type Server struct { + port string + server http.Server +} + +// NewServer func +func NewServer(port string) Server { + return Server{ + port: port, + } +} + +// Serve func +func (s *Server) Serve() { + r := mux.NewRouter() + + handler.AttachArticleHandlersToRouter(r) + + r.HandleFunc("/", s.handleDefault) + + s.server = http.Server{ + Addr: s.port, + Handler: r, + IdleTimeout: 120 * time.Second, + ReadTimeout: 1 * time.Second, + WriteTimeout: 1 * time.Second, + } + log.Print("Listening on", s.port) + + err := s.server.ListenAndServe() + if err != nil { + log.Fatal(err) + } +} + +// Shutdown func +func (s *Server) Shutdown(tc context.Context) { + s.server.Shutdown(tc) +} + +func (s *Server) handleDefault(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(string("REST API. Get the fuck out!"))) +}