Skip to content

Commit

Permalink
feat: add gin basicauth
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradumnasaraf committed May 26, 2023
1 parent 4567fa4 commit f1ec398
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
19 changes: 19 additions & 0 deletions middleware/basicAuth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package middleware

import (
"os"

"github.com/gin-gonic/gin"
"github.com/pradumnasaraf/go-api/config"
)

func BasicAuth() gin.HandlerFunc {

config.Config()

username := os.Getenv("BASIC_AUTH_USERNAME")
password := os.Getenv("BASIC_AUTH_PASSWORD")
return gin.BasicAuth(gin.Accounts{
username: password,
})
}
6 changes: 4 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"log"

"github.com/gin-gonic/gin"
"github.com/pradumnasaraf/go-api/handler"
"github.com/pradumnasaraf/go-api/config"

"github.com/pradumnasaraf/go-api/handler"
"github.com/pradumnasaraf/go-api/middleware"
)

func main() {
Expand All @@ -15,8 +15,10 @@ func main() {

router := gin.Default()

router.Use(middleware.BasicAuth())
router.GET("/", handler.PlaygroundHandler())
router.POST("/query", handler.GraphqlHandler())

// Auto catch the PORT variable if it exists
log.Fatal(router.Run())
}

0 comments on commit f1ec398

Please sign in to comment.