catching up and managing some of the future packages
[VSCPweb/.git] / vscpweb.go
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.
4 package vscpweb
5
6 import (
7         "fmt"
8         "net/http"
9
10         hh "healthHandlers"
11
12         "github.com/go-chi/chi/v5"
13         "github.com/go-chi/chi/v5/middleware"
14 )
15
16 // mh "managementHandlers"
17 // sh "staticHandlers"
18 // vh "virtualAPIHandlers"
19 // ch "controllerHandlers"
20 func main() {
21         //create a new server and mount the handlers:
22         s := CreateNewServer()
23         s.MountHandlers()
24         //staring up the server:
25         http.ListenAndServe(":8000", s.Router)
26 }
27
28 type server struct {
29         Router *chi.Mux
30         //suggestion to add config settings or DB in here
31 }
32
33 func CreateNewServer() *server {
34         s := &server{}
35         s.Router = chi.NewRouter()
36         return s
37 }
38
39 func (s *server) MountHandlers() {
40
41         //Creation of the router:
42         //logging enables as a middleware
43         s.Router.Use(middleware.Logger)
44         //recover from panics and send a 500 internal error
45         s.Router.Use(middleware.Recoverer)
46         //a middleware to check if the server is alive
47         s.Router.Use(middleware.Heartbeat("/ping"))
48         //a profiler to check healt of server
49         s.Router.Mount("/debug", middleware.Profiler())
50         //personalized 404
51         s.Router.NotFound(GenericHandler404)
52         //personalized 405
53         s.Router.MethodNotAllowed(GenericHandler405)
54
55         //example:
56         //example of a get function and its handler
57         s.Router.Get("/example/{first}/{second:[0-9]+}", ExampleHandler)
58
59         //Creating subrouters:
60         //healthRouter check on the health of nodes or main server
61         healthRouter := chi.NewRouter()
62         //controllerRouter Agnostic openVswitch controller bridge
63         controllerRouter := chi.NewRouter()
64         //managementRouter Start, stop, monitoring of the core (mininet)
65         managementRouter := chi.NewRouter()
66         //mininetApiRouter Interface with the virtual environment inside the core (mininet)
67         mininetApiRouter := chi.NewRouter()
68         //might want to check https://go-chi.io/#/pages/routing?id=routing-groups
69         //in order to make groups where you have other middleware like authentication
70
71         //////////////////////////////////possible routing/////////////////////////////
72         //s.Router.Connect(pattern string, h http.HandlerFunc)
73         //s.Router.Delete(pattern string, h http.HandlerFunc)
74         //s.Router.Get(pattern string, h http.HandlerFunc)
75         //s.Router.Head(pattern string, h http.HandlerFunc)
76         //s.Router.Options(pattern string, h http.HandlerFunc)
77         //s.Router.Patch(pattern string, h http.HandlerFunc)
78         //s.Router.Post(pattern string, h http.HandlerFunc)
79         //s.Router.Put(pattern string, h http.HandlerFunc)
80         //s.Router.Trace(pattern string, h http.HandlerFunc)
81         ///////////////////////////////////////////////////////////////////////////////
82
83         //generic ryu version at first...
84         //s.Router.Get("/", )
85         //s.Router.Get("/access", )
86         //s.Router.Get("/stats", )
87         //s.Router.Get("/topology", )
88         //s.Router.Get("/mpstat", )//DEPRECATED see: /masterHealth
89         //s.Router.Get("/ifstat", )//DEPRECATED see: /masterHealth
90         //s.Router.Get("/showtemp", )//DEPRECATED see: /masterHealth
91         //healthRouter.Get("/", )//this renders the health page "/health"
92         //healthRouter.Get("/ping", )// "/rpiping"
93         healthRouter.Get("/masterHealth", hh.MasterHealthHandler) //return a JSON with healt of the master "/free"
94         //controllerRouter.Delete("/flow", )//this is an external request "/flowdel"
95         //controllerRouter.Get("/resetflows", )//not sure if here or managementRouter
96         //controllerRouter.Get("/listswitch", )
97         //controllerRouter.Get("/portDescription", )// "/portsdesc"
98         //controllerRouter.Get("/portStatus", )// "/portsstat"
99         //controllerRouter.Get("/tablestatus", )
100         //controllerRouter.Get("/topology", )// "/gettopo"
101         //controllerRouter.Get("/status", )
102         //mininetApiRouter.Get("/net", )
103         //mininetApiRouter.Get("/nodes", )
104         //mininetApiRouter.Get("/status", )// "/statusnodes"
105         //mininetApiRouter.Get("/intfs", )
106         //mininetApiRouter.Get("/iperf", )
107         //mininetApiRouter.Get("/pingall", )
108         //mininetApiRouter.Get("/sendCommand", )// "/sendcommand"
109         //mininetApiRouter.Get("/placement", )
110         //managementRouter.Get("/controllerData", ) // "/getcontrollerdata"
111         //managementRouter.Get("/vscpData", )// "/getvsorcdata"
112         //managementRouter.Get("/startController", )// "/startcontroller"
113         //managementRouter.Get("/startcontrollerAPI", )// "/startcontrollerrouter"
114         //managementRouter.Get("/stopController", )// "/stopcontroller"
115         //managementRouter.Get("/cancel", )// "/cancel"
116         //managementRouter.Get("/startVsorc", )// "/startvsorc"
117         //managementRouter.Get("/stopVsorc", )// "/stopvsorc"
118         //http.Handle("/", r)
119
120         //Mounting all of the subrouters:
121         s.Router.Mount("/health", healthRouter)
122         s.Router.Mount("/controller", controllerRouter)
123         s.Router.Mount("/management", managementRouter)
124         s.Router.Mount("/virtualAPI", mininetApiRouter)
125
126 }
127
128 // GenericHandler404 is the universal 404 response of this front end
129 func GenericHandler404(w http.ResponseWriter, r *http.Request) {
130         w.WriteHeader(404)
131         w.Write([]byte("route does not exist"))
132 }
133
134 // GenericHandler405 is the universal 405 response of this front end
135 func GenericHandler405(w http.ResponseWriter, r *http.Request) {
136         w.WriteHeader(405)
137         w.Write([]byte("Method not valid"))
138 }
139 func ExampleHandler(w http.ResponseWriter, r *http.Request) {
140         var1 := chi.URLParam(r, "first")
141         var2 := chi.URLParam(r, "second")
142         w.WriteHeader(http.StatusOK)
143         fmt.Fprintf(w, "First: %v\nSecond: %v", var1, var2)
144 }