1 // This is a system for playing with SDN based openvswitches using a simple web
2 // interface. This project will be combined with a CLI version or probably just
3 // made compatible with a core version in the future.
15 //mh "managementHandlers"
16 //vh "virtualAPIHandlers"
17 //ch "controllerHandlers"
20 "github.com/go-chi/chi/v5"
21 "github.com/go-chi/chi/v5/middleware"
25 //create a new server and mount the handlers:
26 s := CreateNewServer()
29 //Handle the static files
30 fs := http.FileServer(http.Dir("./static/"))
31 s.Router.Handle("/static/*", http.StripPrefix("/static/", fs))
33 //staring up the server:
34 port := flag.Int("port", 8001, "Port in which the server will be started")
36 setPort := strconv.Itoa(*port)
37 setPort = ":" + setPort
39 http.ListenAndServe(setPort, s.Router)
44 //suggestion to add config settings or DB in here
47 func CreateNewServer() *server {
49 s.Router = chi.NewRouter()
53 func (s *server) MountHandlers() {
55 //Creation of the router:
56 //logging enables as a middleware
57 s.Router.Use(middleware.Logger)
58 //recover from panics and send a 500 internal error
59 s.Router.Use(middleware.Recoverer)
60 //a middleware to check if the server is alive
61 s.Router.Use(middleware.Heartbeat("/ping"))
62 //a profiler to check healt of server
63 s.Router.Mount("/debug", middleware.Profiler())
65 s.Router.NotFound(GenericHandler404)
67 s.Router.MethodNotAllowed(GenericHandler405)
70 //example of a get function and its handler
71 s.Router.Get("/example/{first}/{second:[0-9]+}", ExampleHandler)
72 s.Router.Get("/", indexHandler)
74 //Creating subrouters:
75 //healthRouter check on the health of nodes or main server
76 healthRouter := chi.NewRouter()
77 //controllerRouter Agnostic openVswitch controller bridge
78 controllerRouter := chi.NewRouter()
79 //managementRouter Start, stop, monitoring of the core (mininet)
80 managementRouter := chi.NewRouter()
81 //mininetApiRouter Interface with the virtual environment inside the core (mininet)
82 mininetApiRouter := chi.NewRouter()
83 //might want to check https://go-chi.io/#/pages/routing?id=routing-groups
84 //in order to make groups where you have other middleware like authentication
86 //////////////////////////////////possible routing/////////////////////////////
87 //s.Router.Connect(pattern string, h http.HandlerFunc)
88 //s.Router.Delete(pattern string, h http.HandlerFunc)
89 //s.Router.Get(pattern string, h http.HandlerFunc)
90 //s.Router.Head(pattern string, h http.HandlerFunc)
91 //s.Router.Options(pattern string, h http.HandlerFunc)
92 //s.Router.Patch(pattern string, h http.HandlerFunc)
93 //s.Router.Post(pattern string, h http.HandlerFunc)
94 //s.Router.Put(pattern string, h http.HandlerFunc)
95 //s.Router.Trace(pattern string, h http.HandlerFunc)
96 ///////////////////////////////////////////////////////////////////////////////
98 //generic ryu version at first...
100 //s.Router.Get("/access", )
101 //s.Router.Get("/stats", )
102 //s.Router.Get("/topology", )
103 //s.Router.Get("/mpstat", )//DEPRECATED see: /masterHealth
104 //s.Router.Get("/ifstat", )//DEPRECATED see: /masterHealth
105 //s.Router.Get("/showtemp", )//DEPRECATED see: /masterHealth
106 //healthRouter.Get("/", )//this renders the health page "/health"
107 //healthRouter.Get("/ping", )// "/rpiping"
108 healthRouter.Get("/masterHealth", hh.MasterHealthHandler) //return a JSON with healt of the master "/free"
109 //controllerRouter.Delete("/flow", )//this is an external request "/flowdel"
110 //controllerRouter.Get("/resetflows", )//not sure if here or managementRouter
111 //controllerRouter.Get("/listswitch", )
112 //controllerRouter.Get("/portDescription", )// "/portsdesc"
113 //controllerRouter.Get("/portStatus", )// "/portsstat"
114 //controllerRouter.Get("/tablestatus", )
115 //controllerRouter.Get("/topology", )// "/gettopo"
116 //controllerRouter.Get("/status", )
117 //mininetApiRouter.Get("/net", )
118 //mininetApiRouter.Get("/nodes", )
119 //mininetApiRouter.Get("/status", )// "/statusnodes"
120 //mininetApiRouter.Get("/intfs", )
121 //mininetApiRouter.Get("/iperf", )
122 //mininetApiRouter.Get("/pingall", )
123 //mininetApiRouter.Get("/sendCommand", )// "/sendcommand"
124 //mininetApiRouter.Get("/placement", )
125 //managementRouter.Get("/controllerData", ) // "/getcontrollerdata"
126 //managementRouter.Get("/vscpData", )// "/getvsorcdata"
127 //managementRouter.Get("/startController", )// "/startcontroller"
128 //managementRouter.Get("/startcontrollerAPI", )// "/startcontrollerrouter"
129 //managementRouter.Get("/stopController", )// "/stopcontroller"
130 //managementRouter.Get("/cancel", )// "/cancel"
131 //managementRouter.Get("/startVsorc", )// "/startvsorc"
132 //managementRouter.Get("/stopVsorc", )// "/stopvsorc"
133 //http.Handle("/", r)
135 //Mounting all of the subrouters:
136 s.Router.Mount("/health", healthRouter)
137 s.Router.Mount("/controller", controllerRouter)
138 s.Router.Mount("/management", managementRouter)
139 s.Router.Mount("/virtualAPI", mininetApiRouter)
143 // GenericHandler404 is the universal 404 response of this front end
144 func GenericHandler404(w http.ResponseWriter, r *http.Request) {
147 messages := []string{"route does not exist", "page not found", "resource not found"}
148 randomIndex := rand.Intn(len(messages))
149 w.Write([]byte(messages[randomIndex]))
152 // GenericHandler405 is the universal 405 response of this front end
153 func GenericHandler405(w http.ResponseWriter, r *http.Request) {
155 w.Write([]byte("Method not valid"))
157 func ExampleHandler(w http.ResponseWriter, r *http.Request) {
158 var1 := chi.URLParam(r, "first")
159 var2 := chi.URLParam(r, "second")
160 w.WriteHeader(http.StatusOK)
161 fmt.Fprintf(w, "First: %v\nSecond: %v", var1, var2)
164 func indexHandler(w http.ResponseWriter, r *http.Request) {
165 w.WriteHeader(http.StatusOK)
166 passarg := "some passing argument done"
167 indexTemplate := template.Must(template.ParseFiles("templates/views/index.html"))
168 //check for go partial templates so you can extract blocks you want to use
169 indexTemplate.Execute(w, passarg)