Skip to content

Instantly share code, notes, and snippets.

@forki
Created November 29, 2017 09:03
Show Gist options
  • Save forki/b7149e7cbd76726432e18a3b311188a6 to your computer and use it in GitHub Desktop.
Save forki/b7149e7cbd76726432e18a3b311188a6 to your computer and use it in GitHub Desktop.
open Giraffe.TokenRouter
....
let notfound = NOT_FOUND "Page not found"
choose [
GET >=>
router notfound [
route "/" => htmlFile (System.IO.Path.Combine(root,"index.html"))
subRoute "/api" [
routef "/customer/%s/" (fun (s:string) -> UserData.getCustomer db.GetCustomer s)
routef "/reservation/%s/%s/" (fun (x:string,y:string) -> ChargingPoints.getReservation db.GetReservationForConnector (x,y))
routef "/chargestations/%f/%f/%f/%f/" (fun (nelat:float,nelong:float,swlat:float,swlong:float) ->
let bounds = Coordinates.newBounds nelat nelong swlat swlong
ChargeStations.getChargeStations bounds chargeStationsCache.GetData)
routef "/chargingpoints/%s/" (fun (s:string) -> ChargingPoints.getChargingPoints chargingPointsCache.GetData)
routef "/chargingpoint/%s/%i/" (ChargingPoints.getChargingPointStatus chargingPointsCache.GetData db.GetReservationForConnector)
]
routef "/chargingpointssocket/token/%s" (fun (token:string) next (ctx: HttpContext) -> task {
match JsonWebToken.isValid token with
| None ->
return! BAD_REQUEST "websocket access failed" next ctx
| Some _ ->
if ctx.WebSockets.IsWebSocketRequest then
let! ws = ctx.WebSockets.AcceptWebSocketAsync()
do! chargingPointBroadcaster.RegisterClient(ws,CancellationToken.None)
return! text "Ok" next ctx
else
return! BAD_REQUEST "websocket access failed" next ctx
})
]
POST >=>
router notfound [
routef "/api/checkreservations/%s/" (fun (s:string) next ctx -> task {
let _noblocking = reservationsCache.Invalidate()
return! Successful.OK s next ctx
})
route "/api/users/login/" => Auth.login db.GetUser
route "/api/newpassword" => Auth.newPassword
route "/api/reserve" => ChargingPoints.reserveChargingPoint db.Connection
route "/api/cancelreservation" => ChargingPoints.cancelReservation db.Connection
route "/api/starttransaction" => ChargingPoints.startTransaction
route "/api/stoptransaction" => ChargingPoints.stopTransaction
route "/api/savecustomer" => UserData.saveCustomer
routef "/api/paypal/%s/%i/" (fun (chargingPointID:string,connectorID:int) next ctx -> Payment.postPayment (chargingPointID,connectorID) next ctx)
// TODO: Make it seperate service
CentralSystem.Program.ocpp15
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment