Skip to content

Commit

Permalink
Add Authentication to NOT FOUND Page
Browse files Browse the repository at this point in the history
Signed-off-by: Jerrico Dela Cruz <[email protected]>
  • Loading branch information
jerricotandelacruz committed Oct 28, 2024
1 parent 05276d0 commit 2135040
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/goapp/router/mux-router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"net/http"
"os"

rtPages "main/routes/pages"

"github.com/gorilla/mux"
"github.com/rs/cors"
"github.com/unrolled/secure"
)

type muxRouter struct{}
type muxRouter struct {
NotFoundHandler http.HandlerFunc
}

func NewMuxRouter() Router {
return &muxRouter{}
Expand All @@ -38,7 +37,11 @@ func (*muxRouter) DELETE(uri string, f func(resp http.ResponseWriter, req *http.
muxDispatcher.HandleFunc(uri, f).Methods("DELETE")
}

func (*muxRouter) SERVE(port string) {
func (mr *muxRouter) NOTFOUND(f func(resp http.ResponseWriter, req *http.Request)) {
mr.NotFoundHandler = http.HandlerFunc(f)
}

func (mr *muxRouter) SERVE(port string) {
secureOptions := secure.Options{
SSLRedirect: true, // Strict-Transport-Security
SSLHost: os.Getenv("SSL_HOST"), // Strict-Transport-Security
Expand All @@ -58,22 +61,25 @@ func (*muxRouter) SERVE(port string) {
secureOptions.CustomFrameOptionsValue = fmt.Sprint("ALLOW-FROM ", os.Getenv("FRAME_EMBEDDOR"))
}

defaultCors := cors.New(cors.Options{
AllowedOrigins: []string{os.Getenv("APPROVAL_SYSTEM_APP_URL")},
AllowCredentials: true,
AllowedHeaders: []string{"Content-Type", "Authorization"},
Debug: os.Getenv("IS_DEVELOPMENT") == "true",
}).Handler
// defaultCors := cors.New(cors.Options{
// AllowedOrigins: []string{os.Getenv("APPROVAL_SYSTEM_APP_URL")},
// AllowCredentials: true,
// AllowedHeaders: []string{"Content-Type", "Authorization"},
// Debug: os.Getenv("IS_DEVELOPMENT") == "true",
// }).Handler

secureMiddleware := secure.New(secureOptions)
muxDispatcher.Use(
defaultCors,
// defaultCors,
secureMiddleware.Handler,
commonHeadersMiddleware,
)
http.Handle("/", muxDispatcher)

muxDispatcher.NotFoundHandler = http.HandlerFunc(rtPages.NotFoundHandler)
if mr.NotFoundHandler != nil {
muxDispatcher.NotFoundHandler = mr.NotFoundHandler
}

muxDispatcher.PathPrefix("/public/").Handler(http.StripPrefix("/public/", http.FileServer(http.Dir("./public/"))))

fmt.Printf("Mux HTTP server running on port %v", port)
Expand Down
1 change: 1 addition & 0 deletions src/goapp/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ type Router interface {
POST(uri string, f func(resp http.ResponseWriter, req *http.Request))
PUT(uri string, f func(resp http.ResponseWriter, req *http.Request))
DELETE(uri string, f func(resp http.ResponseWriter, req *http.Request))
NOTFOUND(f func(resp http.ResponseWriter, req *http.Request))
SERVE(port string)
}
2 changes: 2 additions & 0 deletions src/goapp/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
)

func setPageRoutes() {
httpRouter.NOTFOUND(m.Chain(rtPages.NotFoundHandler, m.AzureAuth()))

httpRouter.GET("/", m.Chain(rtPages.HomeHandler, m.AzureAuth()))
httpRouter.GET("/error/ghlogin", m.Chain(rtPages.GHLoginRequire, m.AzureAuth()))

Expand Down

0 comments on commit 2135040

Please sign in to comment.