7 "github.com/go-chi/chi/v5"
8 "github.com/go-chi/chi/v5/middleware"
14 //Creation of the router:
15 //r main router for VSCP web application
17 //logging enables as a middleware
18 r.Use(middleware.Logger)
19 //a middleware to check if the server is alive
20 r.Use(middleware.Heartbeat("/ping"))
21 //a profiler to check healt of server
22 r.Mount("/debug", middleware.Profiler())
23 //recover from panics and send a 500 internal error
24 r.Use(middleware.Recoverer)
26 r.NotFound(genericHandler404)
28 r.MethodNotAllowed(genericHandler405)
31 //example of a get function and its handler
32 r.Get("/example/{first}/{second:[0-9]+}", exampleHandler)
34 //Creating subrouters:
35 //healthRouter check on the health of nodes or main server
36 healthRouter := chi.NewRouter()
37 //controllerRouter Agnostic openVswitch controller bridge
38 controllerRouter := chi.NewRouter()
39 //managementRouter Start, stop, monitoring of the core (mininet)
40 managementRouter := chi.NewRouter()
41 //mininetApiRouter Interface with the virtual environment inside the core (mininet)
42 mininetApiRouter := chi.NewRouter()
43 //might want to check https://go-chi.io/#/pages/routing?id=routing-groups
44 //in order to make groups where you have other middleware like authentication
46 //////////////////////////////////possible routing/////////////////////////////
47 //r.Connect(pattern string, h http.HandlerFunc)
48 //r.Delete(pattern string, h http.HandlerFunc)
49 //r.Get(pattern string, h http.HandlerFunc)
50 //r.Head(pattern string, h http.HandlerFunc)
51 //r.Options(pattern string, h http.HandlerFunc)
52 //r.Patch(pattern string, h http.HandlerFunc)
53 //r.Post(pattern string, h http.HandlerFunc)
54 //r.Put(pattern string, h http.HandlerFunc)
55 //r.Trace(pattern string, h http.HandlerFunc)
56 ///////////////////////////////////////////////////////////////////////////////
61 //r.Get("/topology", )
63 //r.Get("/free", )//probably do this with server side or sockets
64 //r.Get("/flowdel", )//this is an external request
67 //r.Get("/showtemp", )
70 //r.Get("/rpiping", )//workerping
72 //r.Get("/statusnodes", )
76 //r.Get("/placement", )
77 //r.Get("/getvsorcdata", )
78 //r.Get("/getcontrollerdata", )
79 //r.Get("/resetflows", )
80 //r.Get("/listswitch", )
82 //r.Get("/tablestatus", )
83 //r.Get("/portsdesc", )
84 //r.Get("/portsstat", )
85 //r.Get("/startcontroller", )
86 //r.Get("/startcontrollerrouter", )
87 //r.Get("/stopcontroller", )
88 //r.Get("/sendcommand", )
90 //r.Get("/startvsorc", )
91 //r.Get("/stopvsorc", )
94 //Mounting all of the subrouters:
95 r.Mount("/health", healthRouter)
96 r.Mount("/controller", controllerRouter)
97 r.Mount("/management", managementRouter)
98 r.Mount("/virtualAPI", mininetApiRouter)
100 //staring up the server
101 http.ListenAndServe(":8000", r)
112 //genericHandler404 is the universal 404 response of this front end
113 func genericHandler404(w http.ResponseWriter, r *http.Request){
115 w.Write([]byte("route does not exist"))
117 //genericHandler405 is the universal 405 response of this front end
118 func genericHandler405(w http.ResponseWriter, r *http.Request){
120 w.Write([]byte("Method not valid"))
122 func exampleHandler(w http.ResponseWriter, r *http.Request) {
123 var1 := chi.URLParam(r, "first")
124 var2 := chi.URLParam(r, "second")
125 w.WriteHeader(http.StatusOK)
126 fmt.Fprintf(w, "First: %v\nSecond: %v", var1, var2)