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