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