Skip to content

Commit

Permalink
fix: correct hostname for gin sample and add compose file to run anyw…
Browse files Browse the repository at this point in the history
…here to test (#111)

* fix: zero missing in host

* feat: provide compose file to run example

* refactor: move env variables into file with examples filled in
  • Loading branch information
flohoss authored Mar 31, 2024
1 parent a7fef09 commit 14fbbe5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/build
.env
6 changes: 6 additions & 0 deletions gin-sample/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ENDPOINT=http://localhost:3001
APP_ID=
APP_SECRET=
RESOURCES=https://default.logto.app/api
REDIRECT_URI=http://localhost:8080/sign-in-callback
POST_SIGN_OUT_REDIRECT_URI=http://localhost:8080
17 changes: 17 additions & 0 deletions gin-sample/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
services:
logto-go:
build:
context: .
dockerfile_inline: |
FROM golang:1.22-alpine
RUN apk add git
WORKDIR /app
RUN git clone https://github.com/logto-io/go.git
WORKDIR /app/go/gin-sample
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux go build -o /logto-go
command: /logto-go
env_file:
- .env
ports:
- 8080:8080
21 changes: 14 additions & 7 deletions gin-sample/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"net/http"
"os"
"strings"

"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/memstore"
Expand All @@ -15,10 +17,15 @@ var (

func main() {
logtoConfig := &client.LogtoConfig{
Endpoint: "<your-logto-endpoint>", // E.g. "http://localhost:3001"
AppId: "<your-app-id>",
AppSecret: "<your-app-secret>",
Resources: []string{"your-resource"}, // E.g. "https://default.logto.app/api"
// see .env.example for more details and examples
Endpoint: os.Getenv("ENDPOINT"),
AppId: os.Getenv("APP_ID"),
AppSecret: os.Getenv("APP_SECRET"),
Resources: []string{},
}
resources, present := os.LookupEnv("RESOURCES")
if present {
logtoConfig.Resources = strings.Split(resources, ",")
}

store := memstore.NewStore([]byte("secret"))
Expand Down Expand Up @@ -51,7 +58,7 @@ func main() {
router.GET("/sign-in", func(ctx *gin.Context) {
session := sessions.Default(ctx)
logtoClient := client.NewLogtoClient(logtoConfig, &SessionStorage{session: session})
signInUri, err := logtoClient.SignIn("http://localhost:8080/sign-in-callback")
signInUri, err := logtoClient.SignIn(os.Getenv("REDIRECT_URI"))
if err != nil {
ctx.String(http.StatusInternalServerError, err.Error())
return
Expand Down Expand Up @@ -89,7 +96,7 @@ func main() {
session := sessions.Default(ctx)
logtoClient := client.NewLogtoClient(logtoConfig, &SessionStorage{session: session})

signOutUri, signOutErr := logtoClient.SignOut("http://localhost:8080")
signOutUri, signOutErr := logtoClient.SignOut(os.Getenv("POST_SIGN_OUT_REDIRECT_URI"))

if signOutErr != nil {
ctx.String(http.StatusOK, signOutErr.Error())
Expand Down Expand Up @@ -144,5 +151,5 @@ func main() {
ctx.Data(http.StatusOK, ContentTypeHtml, []byte(unauthorizedPage))
})

router.Run("0.0.0:8080")
router.Run("0.0.0.0:8080")
}

0 comments on commit 14fbbe5

Please sign in to comment.