1
0
mirror of https://github.com/mgerb/mywebsite synced 2026-01-10 09:52:51 +00:00
Files
mywebsite/server/route/route.go

55 lines
1.3 KiB
Go

package route
import (
"github.com/julienschmidt/httprouter"
"log"
"net/http"
"server/controller"
"server/controller/api"
)
func Routes() *httprouter.Router {
log.Println("Server Started")
r := httprouter.New()
r.GET("/api/storedata", api.HandleSensorRequest)
r.GET("/api/allsensors", api.HandleAllSensors)
r.GET("/api/sensor/:location", api.HandleSensorByLocation)
r.GET("/api/sensor/:location/:year", api.HandleSensorByLocationYear)
r.GET("/api/sensor/:location/:year/:monthname", api.HandleSensorByLocationMonth)
r.GET("/discord", controller.DiscordRedirect)
r.GET("/vpn", controller.VPNRedirect)
r.GET("/camera", controller.CameraRedirect)
//set up public folder path
r.ServeFiles("/public/*filepath", http.Dir("./public/"))
//route every invalid request to template file
//routing is all handled on the client side with angular
r.NotFound = http.HandlerFunc(fileHandler("./public/index.html"))
return r
}
//route requests to static files
func routerFileHandler(path string) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
http.ServeFile(w, r, path)
}
}
//function to serve files with standard net/http library
func fileHandler(path string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, path)
}
}