-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
79 lines (70 loc) · 2.06 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package server
import (
"database/sql"
"log"
"net/http"
"time"
"github.com/chirst/go-todo/auth"
"github.com/chirst/go-todo/http/rest"
"github.com/chirst/go-todo/todo"
"github.com/chirst/go-todo/user"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
"github.com/go-chi/httprate"
redoc "github.com/go-openapi/runtime/middleware"
)
// GetRouter configures the application router
func GetRouter(db *sql.DB) http.Handler {
var todosRepo todo.Repository
var usersRepo user.Repository
if db != nil {
todosRepo = &todo.PostgresRepository{DB: db}
usersRepo = &user.PostgresRepository{DB: db}
} else {
log.Println("using in memory db")
todosRepo = new(todo.MemoryRepository)
usersRepo = new(user.MemoryRepository)
}
todoService := todo.NewService(todosRepo)
usersService := user.NewService(usersRepo)
router := chi.NewRouter()
router.Use(middleware.Logger)
router.Use(httprate.LimitByIP(100, time.Minute))
router.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"https://*", "http://*"},
AllowedMethods: []string{"GET", "POST", "PATCH", "DELETE", "OPTIONS"},
AllowedHeaders: []string{
"Accept",
"Authorization",
"Content-Type",
"X-CSRF-Token",
},
ExposedHeaders: []string{"Link"},
AllowCredentials: false,
MaxAge: 300,
}))
// Protected routes
router.Group(func(r chi.Router) {
r.Use(auth.Verifier)
r.Use(auth.Authenticator)
r.Get("/todos", rest.GetTodos(todoService))
r.Post("/todos", rest.AddTodo(todoService))
r.Patch("/todos/{todoID}", rest.PatchTodo(todoService))
r.Delete("/todos/{todoID}", rest.DeleteTodo(todoService))
r.Get("/priorities", rest.GetPriorities(todoService))
})
// Unprotected routes
router.Group(func(r chi.Router) {
r.Post("/users", rest.AddUser(usersService))
r.Post("/login", rest.Login(usersService))
// Docs
sh := redoc.Redoc(redoc.RedocOpts{
SpecURL: "/swagger.yaml",
Title: "Todo API Documentation",
}, nil)
r.Handle("/docs", sh)
r.Handle("/swagger.yaml", http.FileServer(http.Dir("./")))
})
return (http.Handler)(router)
}