Skip to content

Commit ad01910

Browse files
authored
Merge pull request #2 from CoderVlogger/fiber-get-started
First steps with Echo, Iris, and Fiber
2 parents 6a629da + ce49b61 commit ad01910

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+680
-139
lines changed

echo/Notes.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Open topics:
2+
3+
- How to use model without adding tags?
4+
- How to redirect with custom HTTP Method (defaults to "POST")?
5+
- Org url structure when BE API and FE served from the same app
6+
- Refactor API urls (maybe group them)
7+
8+
9+
API:
10+
11+
/api
12+
13+
Static / Assets:
14+
15+
/static
16+
17+
Front-end / Website:
18+
19+
/
20+
21+

echo/api.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"strconv"
6+
7+
"github.com/CoderVlogger/go-web-frameworks/pkg"
8+
"github.com/labstack/echo/v4"
9+
)
10+
11+
func (wa *WebApplication) initAPIRoutes() {
12+
wa.echoApp.POST(apiPrefix+"entities", wa.addEntity)
13+
wa.echoApp.PUT(apiPrefix+"entities", wa.updateEntity)
14+
wa.echoApp.GET(apiPrefix+"entities", wa.listEntities)
15+
wa.echoApp.GET(apiPrefix+"entities/:id", wa.getEntity)
16+
wa.echoApp.DELETE(apiPrefix+"entities/:id", wa.deleteEntity)
17+
}
18+
19+
func (wa *WebApplication) addEntity(ctx echo.Context) error {
20+
entity := pkg.Entity{}
21+
22+
if err := ctx.Bind(&entity); err != nil {
23+
return ctx.JSON(http.StatusBadRequest, pkg.TextResponse{Message: err.Error()})
24+
}
25+
26+
if err := wa.entityRepository.Add(&entity); err != nil {
27+
return ctx.JSON(http.StatusInternalServerError, pkg.TextResponse{Message: err.Error()})
28+
}
29+
30+
return ctx.JSON(http.StatusCreated, entity)
31+
}
32+
33+
func (wa *WebApplication) updateEntity(ctx echo.Context) error {
34+
entity := pkg.Entity{}
35+
36+
if err := ctx.Bind(&entity); err != nil {
37+
return ctx.JSON(http.StatusBadRequest, pkg.TextResponse{Message: err.Error()})
38+
}
39+
40+
if err := wa.entityRepository.Update(&entity); err != nil {
41+
return ctx.JSON(http.StatusInternalServerError, pkg.TextResponse{Message: err.Error()})
42+
}
43+
44+
return ctx.JSON(http.StatusOK, entity)
45+
}
46+
47+
func (wa *WebApplication) getEntity(ctx echo.Context) error {
48+
entityID := ctx.Param("id")
49+
50+
entity, err := wa.entityRepository.Get(entityID)
51+
if err != nil {
52+
return ctx.JSON(http.StatusNotFound, pkg.TextResponse{Message: err.Error()})
53+
}
54+
55+
if entity == nil {
56+
return ctx.JSON(http.StatusNotFound, pkg.TextResponse{Message: "entity not found"})
57+
}
58+
59+
return ctx.JSON(http.StatusOK, entity)
60+
}
61+
62+
func (wa *WebApplication) listEntities(ctx echo.Context) error {
63+
var err error
64+
65+
page := 1
66+
67+
pageStr := ctx.QueryParam("page")
68+
if pageStr != "" {
69+
page, err = strconv.Atoi(pageStr)
70+
if err != nil {
71+
return ctx.JSON(http.StatusBadRequest, pkg.TextResponse{Message: err.Error()})
72+
}
73+
}
74+
75+
entities, err := wa.entityRepository.List(page, pageSize)
76+
if err != nil {
77+
return ctx.JSON(http.StatusBadRequest, pkg.TextResponse{Message: err.Error()})
78+
}
79+
80+
return ctx.JSON(http.StatusOK, entities)
81+
}
82+
83+
func (wa *WebApplication) deleteEntity(ctx echo.Context) error {
84+
entityID := ctx.Param("id")
85+
86+
if err := wa.entityRepository.Delete(entityID); err != nil {
87+
return ctx.JSON(http.StatusInternalServerError, pkg.TextResponse{Message: err.Error()})
88+
}
89+
90+
return ctx.JSON(http.StatusOK, pkg.TextResponse{Message: "entity deleted"})
91+
}

echo/assets/js/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("demo")

echo/assets/pages/index.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
<body>
3+
<h1>Echo</h1>
4+
5+
<h2>Submit a new entity:</h2>
6+
7+
<!-- action defaults to the same page -->
8+
<form method="post">
9+
<label for="type">Type:</label>
10+
<input type="text" id="type" name="type" />
11+
<br /><br />
12+
13+
<label for="name">Name:</label>
14+
<input type="text" id="name" name="name" />
15+
<br /><br />
16+
17+
<label for="description">Description:</label>
18+
<input type="text" id="description" name="description" />
19+
<br /><br />
20+
21+
<input type="submit" value="Send!" />
22+
</form>
23+
24+
<script src="/static/js/index.js"></script>
25+
</body>

echo/go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.17
55
replace github.com/CoderVlogger/go-web-frameworks/pkg => ../pkg
66

77
require (
8-
github.com/CoderVlogger/go-web-frameworks/pkg v0.0.0-00010101000000-000000000000
8+
github.com/CoderVlogger/go-web-frameworks/pkg v0.0.0-20220207162710-24ad69459d6b
99
github.com/labstack/echo/v4 v4.6.3
1010
)
1111

@@ -15,8 +15,8 @@ require (
1515
github.com/mattn/go-isatty v0.0.14 // indirect
1616
github.com/valyala/bytebufferpool v1.0.0 // indirect
1717
github.com/valyala/fasttemplate v1.2.1 // indirect
18-
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 // indirect
18+
golang.org/x/crypto v0.0.0-20220208050332-20e1d8d225ab // indirect
1919
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
20-
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 // indirect
20+
golang.org/x/sys v0.0.0-20220207234003-57398862261d // indirect
2121
golang.org/x/text v0.3.7 // indirect
2222
)

echo/go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
2121
github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4=
2222
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
2323
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
24-
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 h1:71vQrMauZZhcTVK6KdYM+rklehEEwb3E+ZhaE5jrPrE=
25-
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
24+
golang.org/x/crypto v0.0.0-20220208050332-20e1d8d225ab h1:lnZ4LoV0UMdibeCUfIB2a4uFwRu491WX/VB2reB8xNc=
25+
golang.org/x/crypto v0.0.0-20220208050332-20e1d8d225ab/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
2626
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
2727
golang.org/x/net v0.0.0-20210913180222-943fd674d43e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
2828
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
@@ -35,8 +35,8 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
3535
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3636
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3737
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
38-
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 h1:XDXtA5hveEEV8JB2l7nhMTp3t3cHp9ZpwcdjqyEWLlo=
39-
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
38+
golang.org/x/sys v0.0.0-20220207234003-57398862261d h1:Bm7BNOQt2Qv7ZqysjeLjgCBanX+88Z/OtdvsrEv1Djc=
39+
golang.org/x/sys v0.0.0-20220207234003-57398862261d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4040
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
4141
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
4242
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

echo/server.go

Lines changed: 20 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,43 @@
11
package main
22

33
import (
4-
"net/http"
5-
"strconv"
6-
74
"github.com/CoderVlogger/go-web-frameworks/pkg"
85

96
"github.com/labstack/echo/v4"
107
)
118

129
var (
13-
pageSize = 4
14-
entityStorage pkg.EntityRepository = pkg.NewEntityMemoryRepository()
10+
pageSize = 4
11+
apiPrefix = "/api/"
1512
)
1613

17-
func main() {
18-
app := echo.New()
19-
entityStorage.Init()
20-
21-
app.GET("/", func(c echo.Context) error {
22-
return c.String(http.StatusOK, "Hello, Echo!")
23-
})
24-
25-
app.POST("/entities", addEntity)
26-
app.PUT("/entities", updateEntity)
27-
app.GET("/entities", listEntities)
28-
app.GET("/entities/:id", getEntity)
29-
app.DELETE("/entities/:id", deleteEntity)
30-
31-
app.Logger.Fatal(app.Start(":8080"))
14+
type WebApplication struct {
15+
echoApp *echo.Echo
16+
entityRepository pkg.EntityRepository
3217
}
3318

34-
func addEntity(ctx echo.Context) error {
35-
entity := pkg.Entity{}
36-
37-
if err := ctx.Bind(&entity); err != nil {
38-
return ctx.JSON(http.StatusBadRequest, pkg.TextResponse{Message: err.Error()})
39-
}
19+
func NewWebApplication() *WebApplication {
20+
er := pkg.NewEntityMemoryRepository()
21+
er.Init()
4022

41-
if err := entityStorage.Add(&entity); err != nil {
42-
return ctx.JSON(http.StatusInternalServerError, pkg.TextResponse{Message: err.Error()})
23+
return &WebApplication{
24+
echoApp: echo.New(),
25+
entityRepository: er,
4326
}
44-
45-
return ctx.JSON(http.StatusCreated, entity)
4627
}
4728

48-
func updateEntity(ctx echo.Context) error {
49-
entity := pkg.Entity{}
50-
51-
if err := ctx.Bind(&entity); err != nil {
52-
return ctx.JSON(http.StatusBadRequest, pkg.TextResponse{Message: err.Error()})
53-
}
54-
55-
if err := entityStorage.Update(&entity); err != nil {
56-
return ctx.JSON(http.StatusInternalServerError, pkg.TextResponse{Message: err.Error()})
57-
}
58-
59-
return ctx.JSON(http.StatusOK, entity)
29+
func (wa *WebApplication) Init() {
30+
wa.initAPIRoutes()
31+
wa.initWebsite()
6032
}
6133

62-
func getEntity(ctx echo.Context) error {
63-
entityID := ctx.Param("id")
64-
65-
entity, err := entityStorage.Get(entityID)
66-
if err != nil {
67-
return ctx.JSON(http.StatusNotFound, pkg.TextResponse{Message: err.Error()})
68-
}
69-
70-
if entity == nil {
71-
return ctx.JSON(http.StatusNotFound, pkg.TextResponse{Message: "entity not found"})
72-
}
73-
74-
return ctx.JSON(http.StatusOK, entity)
75-
}
76-
77-
func listEntities(ctx echo.Context) error {
78-
var err error
79-
80-
page := 1
81-
82-
pageStr := ctx.QueryParam("page")
83-
if pageStr != "" {
84-
page, err = strconv.Atoi(pageStr)
85-
if err != nil {
86-
return ctx.JSON(http.StatusBadRequest, pkg.TextResponse{Message: err.Error()})
87-
}
88-
}
89-
90-
entities, err := entityStorage.List(page, pageSize)
91-
if err != nil {
92-
return ctx.JSON(http.StatusBadRequest, pkg.TextResponse{Message: err.Error()})
93-
}
94-
95-
return ctx.JSON(http.StatusOK, entities)
34+
func (wa *WebApplication) Start(address string) {
35+
wa.echoApp.Logger.Fatal(wa.echoApp.Start(address))
9636
}
9737

98-
func deleteEntity(ctx echo.Context) error {
99-
entityID := ctx.Param("id")
100-
101-
if err := entityStorage.Delete(entityID); err != nil {
102-
return ctx.JSON(http.StatusInternalServerError, pkg.TextResponse{Message: err.Error()})
103-
}
38+
func main() {
39+
wa := NewWebApplication()
10440

105-
return ctx.JSON(http.StatusOK, pkg.TextResponse{Message: "entity deleted"})
41+
wa.Init()
42+
wa.Start(":8080")
10643
}

echo/vendor/github.com/CoderVlogger/go-web-frameworks/pkg/model.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

echo/vendor/golang.org/x/sys/unix/syscall_linux.go

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

echo/vendor/golang.org/x/sys/unix/syscall_linux_alarm.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)