X-Git-Url: https://git.josue.xyz/?p=VSCPweb%2F.git;a=blobdiff_plain;f=vscpweb.go;h=4188e771bc9a65acb2dc806d7c270e11bf7a78a4;hp=a977ad8f10652aca5689b474aa8cc3af42d00424;hb=refs%2Fheads%2Fmaster;hpb=e811634060c856248c7c73f49f75df9a50efe0bc diff --git a/vscpweb.go b/vscpweb.go index a977ad8..4188e77 100644 --- a/vscpweb.go +++ b/vscpweb.go @@ -1,33 +1,47 @@ // This is a system for playing with SDN based openvswitches using a simple web // interface. This project will be combined with a CLI version or probably just // made compatible with a core version in the future. -package vscpweb +package main import ( + "flag" "fmt" + "html/template" + "math/rand" "net/http" + "strconv" hh "healthHandlers" - mh "managementHandlers" - vh "virtualAPIHandlers" - ch "controllerHandlers" - sh "staticHandlers" + //mh "managementHandlers" + //vh "virtualAPIHandlers" + //ch "controllerHandlers" + //sh "staticHandlers" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" ) - func main() { //create a new server and mount the handlers: s := CreateNewServer() s.MountHandlers() + + //Handle the static files + fs := http.FileServer(http.Dir("./static/")) + s.Router.Handle("/static/*", http.StripPrefix("/static/", fs)) + //staring up the server: - http.ListenAndServe(":8000", s.Router) + port := flag.Int("port", 8001, "Port in which the server will be started") + flag.Parse() + s.port = strconv.Itoa(*port) + s.port = ":" + s.port + + s.startUp() } type server struct { Router *chi.Mux + port string //suggestion to add config settings or DB in here } @@ -37,6 +51,12 @@ func CreateNewServer() *server { return s } +//startUp is the function for the server to start listening +func (s *server) startUp() { + //TODO(josuer08) add other things needed at startup + http.ListenAndServe(s.port,s.Router) +} + func (s *server) MountHandlers() { //Creation of the router: @@ -56,6 +76,7 @@ func (s *server) MountHandlers() { //example: //example of a get function and its handler s.Router.Get("/example/{first}/{second:[0-9]+}", ExampleHandler) + //s.Router.Get("/", indexHandler) //Creating subrouters: //healthRouter check on the health of nodes or main server @@ -82,10 +103,10 @@ func (s *server) MountHandlers() { /////////////////////////////////////////////////////////////////////////////// //generic ryu version at first... - //s.Router.Get("/", ) - //s.Router.Get("/access", ) - //s.Router.Get("/stats", ) - //s.Router.Get("/topology", ) + s.Router.Get("/", indexHandler) + s.Router.Get("/access", accessHandler) + s.Router.Get("/stats", statsHandler) + s.Router.Get("/topology", topologyHandler) //s.Router.Get("/mpstat", )//DEPRECATED see: /masterHealth //s.Router.Get("/ifstat", )//DEPRECATED see: /masterHealth //s.Router.Get("/showtemp", )//DEPRECATED see: /masterHealth @@ -128,8 +149,11 @@ func (s *server) MountHandlers() { // GenericHandler404 is the universal 404 response of this front end func GenericHandler404(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(404) - w.Write([]byte("route does not exist")) + messages := []string{"route does not exist", "page not found", "resource not found"} + randomIndex := rand.Intn(len(messages)) + w.Write([]byte(messages[randomIndex])) } // GenericHandler405 is the universal 405 response of this front end @@ -143,3 +167,33 @@ func ExampleHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) fmt.Fprintf(w, "First: %v\nSecond: %v", var1, var2) } + +/////////THIS BLOCK LOOKS LIKE IT IS NOT DRY BUT IT IS BECAUSE IT WILL LATER +//DIFFER WIDELY FROM ONE FUNCTION TO THE NEXT +//////////////////////////////////////////////////////////////////////////////// +func indexHandler(w http.ResponseWriter, r *http.Request) { + passarg := "some passing argument done" + viewsHandlerHelper(w, r, "templates/views/index.html", passarg) +} +func accessHandler(w http.ResponseWriter, r *http.Request) { + passarg := "some passing argument done" + viewsHandlerHelper(w, r, "templates/views/handler.html", passarg) +} +func statsHandler(w http.ResponseWriter, r *http.Request) { + passarg := "some passing argument done" + viewsHandlerHelper(w, r, "templates/views/stats.html", passarg) +} +func topologyHandler(w http.ResponseWriter, r *http.Request) { + passarg := "some passing argument done" + viewsHandlerHelper(w, r, "templates/views/topology.html", passarg) +} +//////////////////////////////////////////////////////////////////////////////// + +func viewsHandlerHelper(w http.ResponseWriter, r *http.Request, file string, argument any) { + w.WriteHeader(http.StatusOK) + + indexTemplate := template.Must(template.ParseFiles(file)) + //check for go partial templates so you can extract blocks you want to use + indexTemplate.Execute(w, argument) + +}