Compare commits

...

1 Commits

Author SHA1 Message Date
Jérémi N ‘EndMove’ 0ec205fe25
structure example 2022-08-03 22:49:52 +02:00
11 changed files with 75 additions and 174 deletions

View File

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

View File

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

1
cmd/server/main.go Normal file
View File

@ -0,0 +1 @@
package server

2
go.mod
View File

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

4
go.sum
View File

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

20
html/template.go Normal file
View File

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

66
main.go
View File

@ -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("<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...")
// }

View File

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

1
store/store.go Normal file
View File

@ -0,0 +1 @@
package store

17
web/middleware.go Normal file
View File

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

33
web/server.go Normal file
View File

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