Made a server struct with a handler implementation and some other stuff
[VSCPweb/.git] / main.go
diff --git a/main.go b/main.go
index 24bb2448bc132ac1969c798d1aebb630ace8fad0..328f36ca7ba6d038c51961127e4ea1ee85c8297d 100644 (file)
--- a/main.go
+++ b/main.go
@@ -4,64 +4,133 @@ import (
        "fmt"
        "net/http"
 
-       "github.com/gorilla/mux"
+    "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()
+//staring up the server:
+    http.ListenAndServe(":8000", s.Router)
+}
+
+
+type server struct {
+    Router *chi.Mux
+    //suggestion to add config settings or DB in here
+}
+
+func CreateNewServer() *server {
+    s := &server{}
+    s.Router = chi.NewRouter()
+    return s
+}
+
+func (s *server) MountHandlers() {
 
-    //example of simple http router, fased out in favor of gorilla mux
-       //http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
-               //fmt.Fprintf(w, "Hello World!")
-       //})
-       //http.ListenAndServe(":8000", nil)
-
-
-
-    r := mux.NewRouter()
-    r.HandleFunc("/example/{first}/{second:[0-9]+}", exampleHandler)
-    //r.HandleFunc("/", )
-    //r.HandleFunc("/access", )
-    //r.HandleFunc("/stats", )
-    //r.HandleFunc("/topology", )
-    //r.HandleFunc("/health", )
-    //r.HandleFunc("/free", )//probably do this with server side or sockets
-    //r.HandleFunc("/flowdel", )//this is an external request
-    //r.HandleFunc("/mpstat", )
-    //r.HandleFunc("/ifstat", )
-    //r.HandleFunc("/showtemp", )
-    //r.HandleFunc("/gettopo", )
-    //r.HandleFunc("/net", )
-    //r.HandleFunc("/rpiping", )//workerping
-    //r.HandleFunc("/nodes", )
-    //r.HandleFunc("/statusnodes", )
-    //r.HandleFunc("/intfs", )
-    //r.HandleFunc("/iperf", )
-    //r.HandleFunc("/pingall", )
-    //r.HandleFunc("/placement", )
-    //r.HandleFunc("/getvsorcdata", )
-    //r.HandleFunc("/getcontrollerdata", )
-    //r.HandleFunc("/resetflows", )
-    //r.HandleFunc("/listswitch", )
-    //r.HandleFunc("/status", )
-    //r.HandleFunc("/tablestatus", )
-    //r.HandleFunc("/portsdesc", )
-    //r.HandleFunc("/portsstat", )
-    //r.HandleFunc("/startcontroller", )
-    //r.HandleFunc("/startcontrollerrouter", )
-    //r.HandleFunc("/stopcontroller", )
-    //r.HandleFunc("/sendcommand", )
-    //r.HandleFunc("/cancel", )
-    //r.HandleFunc("/startvsorc", )
-    //r.HandleFunc("/stopvsorc", )
-    http.Handle("/", r)
-    http.ListenAndServe(":8000", nil)
+//Creation of the router:
+    //logging enables as a middleware
+    s.Router.Use(middleware.Logger)
+    //recover from panics and send a 500 internal error
+    s.Router.Use(middleware.Recoverer)
+    //a middleware to check if the server is alive
+    s.Router.Use(middleware.Heartbeat("/ping"))
+    //a profiler to check healt of server
+    s.Router.Mount("/debug", middleware.Profiler())
+    //personalized 404
+    s.Router.NotFound(GenericHandler404)
+    //personalized 405
+    s.Router.MethodNotAllowed(GenericHandler405)
 
+//example:
+    //example of a get function and its handler
+    s.Router.Get("/example/{first}/{second:[0-9]+}", ExampleHandler)
 
+//Creating subrouters:
+    //healthRouter check on the health of nodes or main server
+    healthRouter := chi.NewRouter()
+    //controllerRouter Agnostic openVswitch controller bridge
+    controllerRouter := chi.NewRouter()
+    //managementRouter Start, stop, monitoring of the core (mininet)
+    managementRouter := chi.NewRouter()
+    //mininetApiRouter Interface with the virtual environment inside the core (mininet)
+    mininetApiRouter := chi.NewRouter()
+    //might want to check https://go-chi.io/#/pages/routing?id=routing-groups
+    //in order to make groups where you have other middleware like authentication
+
+//////////////////////////////////possible routing/////////////////////////////
+    //s.Router.Connect(pattern string, h http.HandlerFunc)
+    //s.Router.Delete(pattern string, h http.HandlerFunc)
+    //s.Router.Get(pattern string, h http.HandlerFunc)
+    //s.Router.Head(pattern string, h http.HandlerFunc)
+    //s.Router.Options(pattern string, h http.HandlerFunc)
+    //s.Router.Patch(pattern string, h http.HandlerFunc)
+    //s.Router.Post(pattern string, h http.HandlerFunc)
+    //s.Router.Put(pattern string, h http.HandlerFunc)
+    //s.Router.Trace(pattern string, h http.HandlerFunc)
+///////////////////////////////////////////////////////////////////////////////
+
+    //s.Router.Get("/", )
+    //s.Router.Get("/access", )
+    //s.Router.Get("/stats", )
+    //s.Router.Get("/topology", )
+    //s.Router.Get("/health", )
+    //s.Router.Get("/free", )//probably do this with server side or sockets
+    //s.Router.Get("/flowdel", )//this is an external request
+    //s.Router.Get("/mpstat", )
+    //s.Router.Get("/ifstat", )
+    //s.Router.Get("/showtemp", )
+    //s.Router.Get("/gettopo", )
+    //s.Router.Get("/net", )
+    //s.Router.Get("/rpiping", )//workerping
+    //s.Router.Get("/nodes", )
+    //s.Router.Get("/statusnodes", )
+    //s.Router.Get("/intfs", )
+    //s.Router.Get("/iperf", )
+    //s.Router.Get("/pingall", )
+    //s.Router.Get("/placement", )
+    //s.Router.Get("/getvsorcdata", )
+    //s.Router.Get("/getcontrollerdata", )
+    //s.Router.Get("/resetflows", )
+    //s.Router.Get("/listswitch", )
+    //s.Router.Get("/status", )
+    //s.Router.Get("/tablestatus", )
+    //s.Router.Get("/portsdesc", )
+    //s.Router.Get("/portsstat", )
+    //s.Router.Get("/startcontroller", )
+    //s.Router.Get("/startcontrollerrouter", )
+    //s.Router.Get("/stopcontroller", )
+    //s.Router.Get("/sendcommand", )
+    //s.Router.Get("/cancel", )
+    //s.Router.Get("/startvsorc", )
+    //s.Router.Get("/stopvsorc", )
+    //http.Handle("/", r)
+
+//Mounting all of the subrouters:
+    s.Router.Mount("/health", healthRouter)
+    s.Router.Mount("/controller", controllerRouter)
+    s.Router.Mount("/management", managementRouter)
+    s.Router.Mount("/virtualAPI", mininetApiRouter)
 
 }
 
-func exampleHandler(w http.ResponseWriter, r *http.Request) {
-    vars := mux.Vars(r)
+
+
+//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"))
+}
+//GenericHandler405 is the universal 405 response of this front end
+func GenericHandler405(w http.ResponseWriter, r *http.Request){
+    w.WriteHeader(405)
+    w.Write([]byte("Method not valid"))
+}
+func ExampleHandler(w http.ResponseWriter, r *http.Request) {
+    var1 := chi.URLParam(r, "first")
+    var2 := chi.URLParam(r, "second")
     w.WriteHeader(http.StatusOK)
-    fmt.Fprintf(w, "First: %v\nSecond: %v", vars["first"], vars["second"])
+    fmt.Fprintf(w, "First: %v\nSecond: %v", var1, var2)
 }