Refactor. Splitted code into subdirectories. Updated modd conf

This commit is contained in:
Piotr Biernat 2020-09-17 20:33:58 +02:00
parent 494b5a22bc
commit 5aa9c4dfdb
8 changed files with 117 additions and 85 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
.history
build

43
handler/articles.go Normal file
View File

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

View File

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

View File

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

View File

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

11
model/article.go Normal file
View File

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

View File

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

56
server/server.go Normal file
View File

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