7 "github.com/go-chi/chi/v5"
8 "github.com/go-chi/chi/v5/middleware"
12 //create a new server and mount the handlers:
13 s := CreateNewServer()
15 //staring up the server:
16 http.ListenAndServe(":8000", s.Router)
22 //suggestion to add config settings or DB in here
25 func CreateNewServer() *server {
27 s.Router = chi.NewRouter()
31 func (s *server) MountHandlers() {
33 //Creation of the router:
34 //logging enables as a middleware
35 s.Router.Use(middleware.Logger)
36 //recover from panics and send a 500 internal error
37 s.Router.Use(middleware.Recoverer)
38 //a middleware to check if the server is alive
39 s.Router.Use(middleware.Heartbeat("/ping"))
40 //a profiler to check healt of server
41 s.Router.Mount("/debug", middleware.Profiler())
43 s.Router.NotFound(GenericHandler404)
45 s.Router.MethodNotAllowed(GenericHandler405)
48 //example of a get function and its handler
49 s.Router.Get("/example/{first}/{second:[0-9]+}", ExampleHandler)
51 //Creating subrouters:
52 //healthRouter check on the health of nodes or main server
53 healthRouter := chi.NewRouter()
54 //controllerRouter Agnostic openVswitch controller bridge
55 controllerRouter := chi.NewRouter()
56 //managementRouter Start, stop, monitoring of the core (mininet)
57 managementRouter := chi.NewRouter()
58 //mininetApiRouter Interface with the virtual environment inside the core (mininet)
59 mininetApiRouter := chi.NewRouter()
60 //might want to check https://go-chi.io/#/pages/routing?id=routing-groups
61 //in order to make groups where you have other middleware like authentication
63 //////////////////////////////////possible routing/////////////////////////////
64 //s.Router.Connect(pattern string, h http.HandlerFunc)
65 //s.Router.Delete(pattern string, h http.HandlerFunc)
66 //s.Router.Get(pattern string, h http.HandlerFunc)
67 //s.Router.Head(pattern string, h http.HandlerFunc)
68 //s.Router.Options(pattern string, h http.HandlerFunc)
69 //s.Router.Patch(pattern string, h http.HandlerFunc)
70 //s.Router.Post(pattern string, h http.HandlerFunc)
71 //s.Router.Put(pattern string, h http.HandlerFunc)
72 //s.Router.Trace(pattern string, h http.HandlerFunc)
73 ///////////////////////////////////////////////////////////////////////////////
76 //s.Router.Get("/access", )
77 //s.Router.Get("/stats", )
78 //s.Router.Get("/topology", )
79 //s.Router.Get("/health", )
80 //s.Router.Get("/free", )//probably do this with server side or sockets
81 //s.Router.Get("/flowdel", )//this is an external request
82 //s.Router.Get("/mpstat", )
83 //s.Router.Get("/ifstat", )
84 //s.Router.Get("/showtemp", )
85 //s.Router.Get("/gettopo", )
86 //s.Router.Get("/net", )
87 //s.Router.Get("/rpiping", )//workerping
88 //s.Router.Get("/nodes", )
89 //s.Router.Get("/statusnodes", )
90 //s.Router.Get("/intfs", )
91 //s.Router.Get("/iperf", )
92 //s.Router.Get("/pingall", )
93 //s.Router.Get("/placement", )
94 //s.Router.Get("/getvsorcdata", )
95 //s.Router.Get("/getcontrollerdata", )
96 //s.Router.Get("/resetflows", )
97 //s.Router.Get("/listswitch", )
98 //s.Router.Get("/status", )
99 //s.Router.Get("/tablestatus", )
100 //s.Router.Get("/portsdesc", )
101 //s.Router.Get("/portsstat", )
102 //s.Router.Get("/startcontroller", )
103 //s.Router.Get("/startcontrollerrouter", )
104 //s.Router.Get("/stopcontroller", )
105 //s.Router.Get("/sendcommand", )
106 //s.Router.Get("/cancel", )
107 //s.Router.Get("/startvsorc", )
108 //s.Router.Get("/stopvsorc", )
109 //http.Handle("/", r)
111 //Mounting all of the subrouters:
112 s.Router.Mount("/health", healthRouter)
113 s.Router.Mount("/controller", controllerRouter)
114 s.Router.Mount("/management", managementRouter)
115 s.Router.Mount("/virtualAPI", mininetApiRouter)
121 //GenericHandler404 is the universal 404 response of this front end
122 func GenericHandler404(w http.ResponseWriter, r *http.Request){
124 w.Write([]byte("route does not exist"))
126 //GenericHandler405 is the universal 405 response of this front end
127 func GenericHandler405(w http.ResponseWriter, r *http.Request){
129 w.Write([]byte("Method not valid"))
131 func ExampleHandler(w http.ResponseWriter, r *http.Request) {
132 var1 := chi.URLParam(r, "first")
133 var2 := chi.URLParam(r, "second")
134 w.WriteHeader(http.StatusOK)
135 fmt.Fprintf(w, "First: %v\nSecond: %v", var1, var2)