generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from Avanade/sprint-19
Sprint 19
- Loading branch information
Showing
11 changed files
with
185 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package router | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
rtPages "main/routes/pages" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/unrolled/secure" | ||
) | ||
|
||
type muxRouter struct{} | ||
|
||
func NewMuxRouter() Router { | ||
return &muxRouter{} | ||
} | ||
|
||
var ( | ||
muxDispatcher = mux.NewRouter() | ||
) | ||
|
||
func (*muxRouter) GET(uri string, f func(resp http.ResponseWriter, req *http.Request)) { | ||
muxDispatcher.HandleFunc(uri, f).Methods("GET") | ||
} | ||
|
||
func (*muxRouter) POST(uri string, f func(resp http.ResponseWriter, req *http.Request)) { | ||
muxDispatcher.HandleFunc(uri, f).Methods("POST") | ||
} | ||
|
||
func (*muxRouter) SERVE(port string) { | ||
secureOptions := secure.Options{ | ||
SSLRedirect: true, // Strict-Transport-Security | ||
SSLHost: os.Getenv("SSL_HOST"), // Strict-Transport-Security | ||
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, // Strict-Transport-Security | ||
FrameDeny: true, | ||
ContentTypeNosniff: true, // X-Content-Type-Options | ||
BrowserXssFilter: true, | ||
ReferrerPolicy: "strict-origin", // Referrer-Policy | ||
ContentSecurityPolicy: os.Getenv("CONTENT_SECURITY_POLICY"), | ||
PermissionsPolicy: "fullscreen=(), geolocation=()", // Permissions-Policy | ||
STSSeconds: 31536000, // Strict-Transport-Security | ||
STSIncludeSubdomains: true, // Strict-Transport-Security | ||
IsDevelopment: os.Getenv("IS_DEVELOPMENT") == "true", | ||
} | ||
|
||
secureMiddleware := secure.New(secureOptions) | ||
muxDispatcher.Use(secureMiddleware.Handler) | ||
http.Handle("/", muxDispatcher) | ||
|
||
muxDispatcher.NotFoundHandler = http.HandlerFunc(rtPages.NotFoundHandler) | ||
muxDispatcher.PathPrefix("/public/").Handler(http.StripPrefix("/public/", http.FileServer(http.Dir("./public/")))) | ||
|
||
fmt.Printf("Mux HTTP server running on port %v", port) | ||
http.ListenAndServe(fmt.Sprintf(":%v", port), muxDispatcher) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package router | ||
|
||
import "net/http" | ||
|
||
type Router interface { | ||
GET(uri string, f func(resp http.ResponseWriter, req *http.Request)) | ||
POST(uri string, f func(resp http.ResponseWriter, req *http.Request)) | ||
SERVE(port string) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package middleware | ||
|
||
import ( | ||
"main/pkg/session" | ||
"net/http" | ||
) | ||
|
||
type Middleware func(http.HandlerFunc) http.HandlerFunc | ||
|
||
func AzureAuth() Middleware { | ||
return func(f http.HandlerFunc) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
isAuth := session.IsAuthenticated(w, r) | ||
if isAuth { | ||
f(w, r) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func ManagedIdentityAuth() Middleware { | ||
return func(f http.HandlerFunc) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
session.IsGuidAuthenticated(w, r) | ||
f(w, r) | ||
} | ||
} | ||
} | ||
|
||
// Chain applies middlewares to a http.HandlerFunc | ||
func Chain(f http.HandlerFunc, middlewares ...Middleware) http.HandlerFunc { | ||
for _, m := range middlewares { | ||
f = m(f) | ||
} | ||
return f | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.