-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (84 loc) · 2.07 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"encoding/json"
"fmt"
_ "github.com/converge/my-bike-api/docs" // docs is generated by Swag CLI, you have to import it.
"github.com/go-chi/chi/v5"
httpSwagger "github.com/swaggo/http-swagger"
"io/ioutil"
"log"
"net/http"
)
type Bike struct {
Id string `json:"id"`
Type string `json:"type"`
ReleaseYear string `json:"release-year"`
}
var myBike Bike
// version godoc
// @Summary Get API version
// @Description Get API version
// @Tags version
// @Accept json
// @Produce json
// @Success 200 {object} string
// @Router / [get]
func version(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("1.0.0"))
}
// listMyBike
// @Summary List My Bike
// @Description List details of My Bike
// @Tags listMyBike
// @Accept json
// @Produce json
// @Success 200 {object} Bike
// @Router /my-bike [GET]
func listMyBike(w http.ResponseWriter, r *http.Request) {
err := json.NewEncoder(w).Encode(myBike)
if err != nil {
log.Println(err)
}
}
// createMyBike
// @Summary Create a Bike
// @Description Create a Bike
// @Tags createMyBike
// @Accept json
// @Produce json
// @param body body Bike true "Bike Object"
// @Success 201 {object} Bike
// @Router /my-bike [POST]
func createMyBike(w http.ResponseWriter, r *http.Request) {
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println(err)
}
defer r.Body.Close()
json.Unmarshal(reqBody, &myBike)
if err != nil {
log.Println(err)
}
w.Write([]byte("Bike created!"))
}
// @title My Bike API
// @version 1.0
// @description API para gerenciar sua Bike
// @termsOfService http://swagger.io/terms/
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:3001
// @BasePath /
func main() {
r := chi.NewRouter()
r.Get("/", version)
r.Post("/my-bike", createMyBike)
r.Get("/my-bike", listMyBike)
r.Get("/swagger/*", httpSwagger.WrapHandler)
fmt.Println("API Listening at: http://localhost:3001")
fmt.Println("Swagger docs at: http://localhost:3001/swagger/")
err := http.ListenAndServe(":3001", r)
if err != nil {
log.Fatalln(err)
}
}