Archived
0
0
Fork 0
This repository has been archived on 2024-05-09. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
nextrip/handlers.go
2018-05-27 22:53:53 -05:00

34 lines
941 B
Go

package main
import (
"time"
log "github.com/Sirupsen/logrus"
"github.com/valyala/fasthttp"
)
var filesHandler = fasthttp.FSHandler(staticFilePath, 0)
func routeHandler(ctx *fasthttp.RequestCtx) {
// 127.0.0.1 user-identifier frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326
userIP := ctx.RemoteIP().String()
if tempIP := string(ctx.Request.Header.Peek("X-Forwarded-For")); tempIP != "" {
userIP = tempIP
}
ctx.Response.Header.Add("Access-Control-Allow-Origin", cors)
log.Infof("%s %s [%s] %s \"%s\"", userIP, ctx.UserAgent(), time.Now().Format("02/01/2006 15:04:05 -0700"), ctx.Method(), ctx.Path())
switch string(ctx.Path()) {
case "/departures":
ctx.SetContentType("application/json")
busHandler(ctx)
case "/graphql":
ctx.SetContentType("application/json")
graphqlHandler(ctx)
case "/graphiql":
graphiqlHandler(ctx)
default:
if staticFilePath != "" {
filesHandler(ctx)
}
}
}