From 0ec205fe256640f8edebe0b9163eabbeba60499f Mon Sep 17 00:00:00 2001 From: EndMove Date: Wed, 3 Aug 2022 22:49:52 +0200 Subject: [PATCH] structure example --- README.md | 3 -- api/server.go | 89 ---------------------------------------------- cmd/server/main.go | 1 + go.mod | 2 +- go.sum | 4 +-- html/template.go | 20 +++++++++++ main.go | 66 ---------------------------------- shopping.rest | 13 ------- store/store.go | 1 + web/middleware.go | 17 +++++++++ web/server.go | 33 +++++++++++++++++ 11 files changed, 75 insertions(+), 174 deletions(-) delete mode 100644 README.md delete mode 100644 api/server.go create mode 100644 cmd/server/main.go create mode 100644 html/template.go delete mode 100644 main.go delete mode 100644 shopping.rest create mode 100644 store/store.go create mode 100644 web/middleware.go create mode 100644 web/server.go diff --git a/README.md b/README.md deleted file mode 100644 index 7b609e9..0000000 --- a/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# GoAsWebServer - -Repository d'entrainement au développement de solutions web en Go. Chaque branche est axée sur un sujet différent et ou un test quelconque. \ No newline at end of file diff --git a/api/server.go b/api/server.go deleted file mode 100644 index 81a69e8..0000000 --- a/api/server.go +++ /dev/null @@ -1,89 +0,0 @@ -package api - -import ( - "encoding/json" - "net/http" - - "github.com/google/uuid" - "github.com/gorilla/mux" -) - -// Item data structure SUB -type Item struct { - ID uuid.UUID `json:"id"` - Name string `json:"name"` -} - -// Server data structure MAIN -type Server struct { - *mux.Router // Merge mux router comportement with Server - ShoppingItems []Item -} - -// Server Constructor -func NewServer() *Server { - server := &Server{ - Router: mux.NewRouter(), - ShoppingItems: []Item{}, - } - server.InitRoutes() - return server -} - -// Init mux router -func (server *Server) InitRoutes() { - server.HandleFunc("/shopping-items", server.ListShoppingItems()).Methods("GET") - server.HandleFunc("/shopping-items", server.CreateShoppingItem()).Methods("POST") - server.HandleFunc("/shopping-items/{id}", server.RemoveShoppingItem()).Methods("DELETE") -} - -// Handler create item -func (server *Server) CreateShoppingItem() http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - var item Item - if err := json.NewDecoder(r.Body).Decode(&item); err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - return - } - - item.ID = uuid.New() - server.ShoppingItems = append(server.ShoppingItems, item) - - w.Header().Set("Content-Type", "application/json") - if err := json.NewEncoder(w).Encode(item); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } -} - -// Handler retrieve items -func (server *Server) ListShoppingItems() http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - if err := json.NewEncoder(w).Encode(server.ShoppingItems); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } -} - -// Hndler remove item -func (server *Server) RemoveShoppingItem() http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - rawID := mux.Vars(r)["id"] - - itemID, err := uuid.Parse(rawID) - if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - return - } - - for i, item := range server.ShoppingItems { - if item.ID == itemID { - server.ShoppingItems = append(server.ShoppingItems[:i], server.ShoppingItems[i+1:]...) - break - } - } - } -} diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 0000000..f275077 --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1 @@ +package server \ No newline at end of file diff --git a/go.mod b/go.mod index 9115bd2..f31fc23 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module endmove/webserverlearning go 1.18 require ( - github.com/google/uuid v1.3.0 github.com/gorilla/mux v1.8.0 + github.com/russross/blackfriday/v2 v2.1.0 ) diff --git a/go.sum b/go.sum index fee0108..4ce779b 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,4 @@ -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= diff --git a/html/template.go b/html/template.go new file mode 100644 index 0000000..e198e80 --- /dev/null +++ b/html/template.go @@ -0,0 +1,20 @@ +package html + +import ( + "html/template" + "strings" + + "github.com/russross/blackfriday/v2" +) + +func Parse(templates ...string) *template.Template { + return template.Must(template.New("ALL").Funcs(FuncMap()).ParseFiles(templates...)) +} + +func FuncMap() template.FuncMap { + return template.FuncMap{ + "lowercase": strings.ToLower, + "uppercase": strings.ToUpper, + "markdown": blackfriday.Run, + } +} \ No newline at end of file diff --git a/main.go b/main.go deleted file mode 100644 index ac3285c..0000000 --- a/main.go +++ /dev/null @@ -1,66 +0,0 @@ -package main - -import ( - "context" - "endmove/webserverlearning/api" - "flag" - "fmt" - "net/http" - "os" - "os/signal" - "time" -) - -func main() { - // define graceful time for webserver shutdown - not required - var wait time.Duration - - flag.DurationVar(&wait, "graceful-timeout", time.Second*30, "The time needed to close the connections") - flag.Parse() // apply flags one shot function - - // Config webserver - webserver := http.Server{ - Addr: ":8080", - // Timeout (stop overloading and attack) - WriteTimeout: time.Second * 15, - ReadTimeout: time.Second * 15, - IdleTimeout: time.Second * 60, - // Mux instance - Handler: api.NewServer(), - } - - // Start webserver in routine - go func() { - if err := webserver.ListenAndServe(); err != nil { - fmt.Println(err.Error()) - } - }() - fmt.Println("Server started on : http://localhost:8080") - - // setuping sig to catch ctrl+c and throw a graceful stop - sig := make(chan os.Signal, 1) - signal.Notify(sig, os.Interrupt) - - // waiting sig - <-sig - - // defining graceful timeout - ctx, cancel := context.WithTimeout(context.Background(), wait) - defer cancel() - - // shut down webserver with countdown - webserver.Shutdown(ctx) - - fmt.Println("program has stopped") - os.Exit(0) -} - -// func main() { -// http.HandleFunc("/hello-world", func(w http.ResponseWriter, r *http.Request) { -// w.Write([]byte("

Hello World !

")) -// }) -// go http.ListenAndServe(":8080", nil) -// fmt.Println("Server listening on port :8080 at http://localhost:8080") -// time.Sleep(1 * time.Minute) -// fmt.Println("Quit...") -// } diff --git a/shopping.rest b/shopping.rest deleted file mode 100644 index b37fc10..0000000 --- a/shopping.rest +++ /dev/null @@ -1,13 +0,0 @@ -### List shopping items -GET http://localhost:8080/shopping-items - -### Create new shopping item -POST http://localhost:8080/shopping-items -Content-Type: application/json - -{ - "name": "MDR" -} - -### Remove shopping item -DELETE http://localhost:8080/shopping-items/uuid \ No newline at end of file diff --git a/store/store.go b/store/store.go new file mode 100644 index 0000000..ef0086d --- /dev/null +++ b/store/store.go @@ -0,0 +1 @@ +package store \ No newline at end of file diff --git a/web/middleware.go b/web/middleware.go new file mode 100644 index 0000000..f66e03b --- /dev/null +++ b/web/middleware.go @@ -0,0 +1,17 @@ +package web + +import "net/http" + +func WithAuth(f http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + // TODO check request auth + f(w, r) + } +} + +func WithCompression(f http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + // TODO wrap writter in compressor + f(w, r) + } +} diff --git a/web/server.go b/web/server.go new file mode 100644 index 0000000..5d66428 --- /dev/null +++ b/web/server.go @@ -0,0 +1,33 @@ +package web + +import ( + "net/http" + + "github.com/gorilla/mux" +) + +func NewServer() *Server { + server := &Server{ + Router: mux.NewRouter(), + } + server.HandleFunc("/todos", server.todos).Methods("GET") + server.HandleFunc("/todos", WithAuth(server.todoCreate)).Methods("POST") + server.HandleFunc("/todos", WithAuth(server.todoDelete)).Methods("DELETE") + return server +} + +type Server struct { + *mux.Router +} + +func (server *Server) todos(w http.ResponseWriter, r *http.Request) { + // TODO get +} + +func (server *Server) todoCreate(w http.ResponseWriter, r *http.Request) { + // TODO get +} + +func (server *Server) todoDelete(w http.ResponseWriter, r *http.Request) { + // TODO get +}