Skip to content

Commit 3eecd1a

Browse files
authored
Session 5 (#16)
* introduction to echo * intro to echo Co-authored-by: Ibrahim Nurandita Isbandiputra <ibrahim.nurandita@wartek.belajar.id>
1 parent 6341ee1 commit 3eecd1a

File tree

5 files changed

+111
-3
lines changed

5 files changed

+111
-3
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ require (
2121
github.com/go-openapi/jsonreference v0.20.0 // indirect
2222
github.com/go-openapi/spec v0.20.7 // indirect
2323
github.com/go-openapi/swag v0.22.3 // indirect
24+
github.com/gorilla/securecookie v1.1.1 // indirect
25+
github.com/gorilla/sessions v1.2.1 // indirect
2426
github.com/josharian/intern v1.0.0 // indirect
2527
github.com/labstack/echo v3.3.10+incompatible // indirect
2628
github.com/labstack/gommon v0.4.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
2525
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
2626
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
2727
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
28+
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
29+
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
30+
github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI=
31+
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
2832
github.com/grpc-ecosystem/grpc-gateway/v2 v2.13.0 h1:fi9bGIUJOGzzrHBbP8NWbTfNC5fKO6X7kFw40TOqGB8=
2933
github.com/grpc-ecosystem/grpc-gateway/v2 v2.13.0/go.mod h1:uY3Aurq+SxwQCpdX91xZ9CgxIMT1EsYtcidljXufYIY=
3034
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=

session-4/ldap/ldap.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ import (
1010
)
1111

1212
const (
13-
ldapServer = "ldap.forumsys.com"
13+
//ldapServer = "ldap.forumsys.com"
14+
//ldapPort = 389
15+
//ldapBindDN = "cn=read-only-admin,dc=example,dc=com"
16+
//ldapPassword = "password"
17+
//ldapSearchDN = "dc=example,dc=com"
18+
ldapServer = "localhost"
1419
ldapPort = 389
15-
ldapBindDN = "cn=read-only-admin,dc=example,dc=com"
20+
ldapBindDN = "cn=admin,dc=ibrahimker,dc=id"
1621
ldapPassword = "password"
17-
ldapSearchDN = "dc=example,dc=com"
22+
ldapSearchDN = "dc=ibrahimker,dc=id"
1823
)
1924

2025
type UserLDAPData struct {

session-5/main.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

33
import (
4+
"fmt"
45
"net/http"
6+
"strings"
57

68
"github.com/labstack/echo"
79
)
@@ -35,5 +37,41 @@ func main() {
3537
return ctx.JSON(http.StatusOK, data)
3638
})
3739

40+
r.GET("/page1", func(ctx echo.Context) error {
41+
name := ctx.QueryParam("name")
42+
data := fmt.Sprintf("Hello %s", name)
43+
44+
return ctx.String(http.StatusOK, data)
45+
})
46+
47+
r.GET("/page2/:name", func(ctx echo.Context) error {
48+
name := ctx.Param("name")
49+
data := fmt.Sprintf("Hello %s", name)
50+
51+
return ctx.String(http.StatusOK, data)
52+
})
53+
54+
r.GET("/page3/:name/*", func(ctx echo.Context) error {
55+
name := ctx.Param("name")
56+
message := ctx.Param("*")
57+
58+
data := fmt.Sprintf("Hello %s, I have message for you: %s", name, message)
59+
60+
return ctx.String(http.StatusOK, data)
61+
})
62+
63+
r.POST("/page4", func(ctx echo.Context) error {
64+
name := ctx.FormValue("name")
65+
message := ctx.FormValue("message")
66+
67+
data := fmt.Sprintf(
68+
"Hello %s, I have message for you: %s",
69+
name,
70+
strings.Replace(message, "/", "", 1),
71+
)
72+
73+
return ctx.String(http.StatusOK, data)
74+
})
75+
3876
r.Start(":9000")
3977
}

session-6/main.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/gorilla/sessions"
8+
"github.com/labstack/echo"
9+
)
10+
11+
const SESSION_ID = "test-session-id"
12+
13+
func main() {
14+
e := echo.New()
15+
store := sessions.NewCookieStore([]byte("test-session-key"))
16+
17+
e.GET("/", func(ctx echo.Context) error {
18+
data := "Hello from /index"
19+
return ctx.String(http.StatusOK, data)
20+
})
21+
22+
e.GET("/set", func(c echo.Context) error {
23+
session, err := store.Get(c.Request(), SESSION_ID)
24+
if err != nil {
25+
return c.String(http.StatusInternalServerError, err.Error())
26+
}
27+
session.Values["message1"] = "hello"
28+
session.Values["message2"] = "world"
29+
if err := session.Save(c.Request(), c.Response()); err != nil {
30+
return c.String(http.StatusInternalServerError, err.Error())
31+
}
32+
33+
return c.Redirect(http.StatusTemporaryRedirect, "/get")
34+
})
35+
36+
e.GET("/get", func(c echo.Context) error {
37+
session, _ := store.Get(c.Request(), SESSION_ID)
38+
39+
if len(session.Values) == 0 {
40+
return c.String(http.StatusOK, "empty result")
41+
}
42+
43+
return c.String(http.StatusOK, fmt.Sprintf(
44+
"%s %s",
45+
session.Values["message1"],
46+
session.Values["message2"],
47+
))
48+
})
49+
50+
e.GET("/delete", func(c echo.Context) error {
51+
session, _ := store.Get(c.Request(), SESSION_ID)
52+
session.Options.MaxAge = -1
53+
session.Save(c.Request(), c.Response())
54+
55+
return c.Redirect(http.StatusTemporaryRedirect, "/get")
56+
})
57+
58+
e.Start(":9000")
59+
}

0 commit comments

Comments
 (0)