-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (48 loc) · 1.35 KB
/
main.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
// Data Portal API.
//
// the purpose of this application is to provide an application
// that is using plain go code to define an API
//
// This should demonstrate all the possible comment annotations
// that are available to turn go code into a fully compliant swagger 2.0 spec
//
// Schemes: http, https
// Host: localhost:1323
// BasePath: /
// Version: 0.0.1
//
// swagger:meta
package main
import (
"log"
"net/http"
"github.com/go-chi/chi"
"github.com/go-openapi/runtime/middleware"
"github.com/rs/cors"
"github.com/shreeharsha-factly/chi-swagger-go/actions"
"github.com/shreeharsha-factly/chi-swagger-go/models"
)
func main() {
// db setup
models.SetupDB()
// db migrations
models.DB.AutoMigrate(&models.User{})
r := chi.NewRouter()
r.Route("/users", func(r chi.Router) {
r.Post("/", actions.CreateUser)
r.Route("/{userId}", func(r chi.Router) {
r.Get("/", actions.GetUser)
r.Delete("/", actions.DeleteUser)
r.Put("/", actions.UpdateUser)
})
})
opts := middleware.RedocOpts{SpecURL: "/swagger.yaml"}
sh := middleware.Redoc(opts, nil)
r.Handle("/docs", sh)
r.Handle("/swagger.yaml", http.FileServer(http.Dir("./")))
log.Fatal(http.ListenAndServe(":1323", setupGlobalMiddleware(r)))
}
func setupGlobalMiddleware(handler http.Handler) http.Handler {
handleCORS := cors.Default().Handler
return handleCORS(handler)
}