init
This commit is contained in:
commit
278302e552
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# 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.
|
89
api/server.go
Normal file
89
api/server.go
Normal file
@ -0,0 +1,89 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
8
go.mod
Normal file
8
go.mod
Normal file
@ -0,0 +1,8 @@
|
||||
module endmove/webserverlearning
|
||||
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/gorilla/mux v1.8.0
|
||||
)
|
4
go.sum
Normal file
4
go.sum
Normal file
@ -0,0 +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=
|
66
main.go
Normal file
66
main.go
Normal file
@ -0,0 +1,66 @@
|
||||
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("<h1>Hello World !</h1>"))
|
||||
// })
|
||||
// go http.ListenAndServe(":8080", nil)
|
||||
// fmt.Println("Server listening on port :8080 at http://localhost:8080")
|
||||
// time.Sleep(1 * time.Minute)
|
||||
// fmt.Println("Quit...")
|
||||
// }
|
13
shopping.rest
Normal file
13
shopping.rest
Normal file
@ -0,0 +1,13 @@
|
||||
### 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
|
Loading…
Reference in New Issue
Block a user