structure example

This commit is contained in:
2022-08-03 22:49:52 +02:00
parent 278302e552
commit 0ec205fe25
11 changed files with 75 additions and 174 deletions

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
}