Last active
March 17, 2024 12:23
-
-
Save flrdv/a1e58acf2b1e333c329046f5c7c0ecfd to your computer and use it in GitHub Desktop.
CRUD using indigo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module github.com/Brom95/chi-crud | |
| go 1.21 | |
| require ( | |
| github.com/indigo-web/chunkedbody v0.1.0 // indirect | |
| github.com/indigo-web/indigo v0.15.9 // indirect | |
| github.com/indigo-web/iter v0.1.0 // indirect | |
| github.com/indigo-web/utils v0.6.2 // indirect | |
| github.com/json-iterator/go v1.1.12 // indirect | |
| github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | |
| github.com/modern-go/reflect2 v1.0.2 // indirect | |
| golang.org/x/crypto v0.21.0 // indirect | |
| golang.org/x/net v0.22.0 // indirect | |
| golang.org/x/text v0.14.0 // indirect | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "github.com/indigo-web/indigo" | |
| "github.com/indigo-web/indigo/http" | |
| "github.com/indigo-web/indigo/http/status" | |
| "github.com/indigo-web/indigo/router/inbuilt" | |
| "log" | |
| "strconv" | |
| ) | |
| type CrudItem struct { | |
| Id int | |
| Name string | |
| Description string | |
| internal string | |
| } | |
| type CRUD struct { | |
| storage map[int]CrudItem | |
| currentID int | |
| } | |
| func (c *CRUD) getItems(request *http.Request) *http.Response { | |
| result := make([]CrudItem, 0, len(c.storage)) | |
| for _, item := range c.storage { | |
| result = append(result, item) | |
| } | |
| return http.JSON(request, result) | |
| } | |
| func (c *CRUD) getItem(request *http.Request) *http.Response { | |
| id, err := strconv.Atoi(request.Params.Value("id")) | |
| if err != nil { | |
| return request.Respond(). | |
| Code(status.BadRequest). | |
| String("bad or missing item ID") | |
| } | |
| if _, ok := c.storage[id]; !ok { | |
| return http.Error(request, status.ErrNotFound) | |
| } | |
| return http.JSON(request, c.storage[id]) | |
| } | |
| func (c *CRUD) postItem(request *http.Request) *http.Response { | |
| id, err := strconv.Atoi(request.Params.Value("id")) | |
| if err != nil { | |
| return request.Respond(). | |
| Code(status.BadRequest). | |
| String("bad or missing item ID") | |
| } | |
| item.Id = c.currentID | |
| c.storage[c.currentID] = item | |
| c.currentID++ | |
| return http.JSON(request, item) | |
| } | |
| func (c *CRUD) putItem(request *http.Request) *http.Response { | |
| id, err := strconv.Atoi(request.Params.Value("id")) | |
| if err != nil { | |
| return request.Respond(). | |
| Code(status.BadRequest). | |
| String("bad or missing item ID") | |
| } | |
| if _, ok := c.storage[id]; !ok { | |
| return http.Error(request, status.ErrNotFound) | |
| } | |
| var item CrudItem | |
| err = request.JSON(item) | |
| if err != nil { | |
| return request.Respond(). | |
| Code(status.BadRequest). | |
| String(err.Error()) | |
| } | |
| item.Id = id | |
| c.storage[id] = item | |
| return http.JSON(request, item) | |
| } | |
| func (c *CRUD) deleteItem(request *http.Request) *http.Response { | |
| id, err := strconv.Atoi(request.Params.Value("id")) | |
| if err != nil { | |
| return request.Respond(). | |
| Code(status.BadRequest). | |
| String("bad or missing item ID") | |
| } | |
| if _, ok := c.storage[id]; !ok { | |
| return http.Error(request, status.ErrNotFound) | |
| } | |
| delete(c.storage, id) | |
| return http.Respond(request) | |
| } | |
| func main() { | |
| crud := &CRUD{ | |
| storage: make(map[int]CrudItem), | |
| currentID: 1, | |
| } | |
| r := inbuilt.New(). | |
| Get("/crud-items", crud.getItems). | |
| Get("/crud-items/{id}", crud.getItem). | |
| Post("/crud-items", crud.postItem). | |
| Put("/crud-items/{id}", crud.postItem). | |
| Delete("/crud-items/{id}", crud.deleteItem) | |
| log.Fatal(indigo.New(":8080").Serve(r)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment