|
|
Package server contains implementation of REST API server (HTTPServer) for the
LDAP proxy.
Generated documentation is available at:
https://godoc.org/github.com/RedHatInsights/insights-operator-ldapauth/server
Documentation in literate-programming-style is available at:
https://redhatinsights.github.io/insights-operator-ldapauth/packages/server/server.html
|
package server
import (
"github.com/RedHatInsights/insights-operator-utils/env"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
)
|
APIPrefix is appended before all REST API endpoint addresses
|
var APIPrefix = env . GetEnv ( "CONTROLLER_PREFIX" , "/api/v1/" )
|
Server basic configuration of server
|
type Server struct {
Address string
LDAP string
Proxy string
ProxyPrefix string
Transport http . RoundTripper
}
|
Status structure for RestAPI response
|
type Status struct {
Status string `json:"status"`
}
|
OkStatus prepared successful response
|
var OkStatus = Status {
Status : "ok" ,
}
func logRequestHandler ( writer http . ResponseWriter , request * http . Request , nextHandler http . Handler ) {
log . Println ( "Request URI: " + request . RequestURI )
log . Println ( "Request method: " + request . Method )
nextHandler . ServeHTTP ( writer , request )
}
func logRequest ( nextHandler http . Handler ) http . Handler {
return http . HandlerFunc (
func ( writer http . ResponseWriter , request * http . Request ) {
logRequestHandler ( writer , request , nextHandler )
} )
}
func ( s * Server ) addDefaultHeaders ( nextHandler http . Handler ) http . Handler {
return http . HandlerFunc (
func ( writer http . ResponseWriter , request * http . Request ) {
writer . Header ( ) . Set ( "Content-Type" , "application/json; charset=utf-8" )
nextHandler . ServeHTTP ( writer , request )
} )
}
|
Initialize main function that start server
|
func ( s * Server ) Initialize ( ) {
log . Println ( "API Prefix: " , APIPrefix )
router := mux . NewRouter ( ) . StrictSlash ( true )
router . Use ( logRequest )
router . Use ( s . JWTAuthentication )
router . Use ( s . addDefaultHeaders )
router . HandleFunc ( APIPrefix + "login" , s . Login ) . Methods ( "POST" )
router . PathPrefix ( APIPrefix ) . Handler ( http . HandlerFunc ( s . HandleHTTP ) )
log . Println ( "Starting HTTP server at" , s . Address )
err := http . ListenAndServe ( s . Address , router )
if err != nil {
log . Fatal ( "Unable to initialize HTTP server" , err )
os . Exit ( 2 )
}
}
|