started working on the templating system
[VSCPweb/.git] / main.go
1 package main
2
3 import (
4         "fmt"
5         "net/http"
6
7     "github.com/go-chi/chi/v5"
8     "github.com/go-chi/chi/v5/middleware"
9 )
10
11 func main() {
12 //create a new server and mount the handlers:
13     s := CreateNewServer()
14     s.MountHandlers()
15 //staring up the server:
16     http.ListenAndServe(":8000", s.Router)
17 }
18
19
20 type server struct {
21     Router *chi.Mux
22     //suggestion to add config settings or DB in here
23 }
24
25 func CreateNewServer() *server {
26     s := &server{}
27     s.Router = chi.NewRouter()
28     return s
29 }
30
31 func (s *server) MountHandlers() {
32
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())
42     //personalized 404
43     s.Router.NotFound(GenericHandler404)
44     //personalized 405
45     s.Router.MethodNotAllowed(GenericHandler405)
46
47 //example:
48     //example of a get function and its handler
49     s.Router.Get("/example/{first}/{second:[0-9]+}", ExampleHandler)
50
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
62
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 ///////////////////////////////////////////////////////////////////////////////
74
75 //generic ryu version at first...
76     //s.Router.Get("/", )
77     //s.Router.Get("/access", )
78     //s.Router.Get("/stats", )
79     //s.Router.Get("/topology", )
80     //s.Router.Get("/mpstat", )//DEPRECATED see: /masterHealth
81     //s.Router.Get("/ifstat", )//DEPRECATED see: /masterHealth
82     //s.Router.Get("/showtemp", )//DEPRECATED see: /masterHealth
83     //healthRouter.Get("/", )//this renders the health page "/health"
84     //healthRouter.Get("/masterHealth", )//return a JSON with healt of the master "/free"
85     //controllerRouter.Delete("/flow", )//this is an external request "/flowdel"
86     //controllerRouter.Get("/topology", )// "/gettopo"
87     //mininetApiRouter.Get("/net", )
88     //healthRouter.Get("/ping", )// "/rpiping"
89     //mininetApiRouter.Get("/nodes", )
90     //mininetApiRouter.Get("/status", )// "/statusnodes"
91     //mininetApiRouter.Get("/intfs", )
92     //mininetApiRouter.Get("/iperf", )
93     //mininetApiRouter.Get("/pingall", )
94     //mininetApiRouter.Get("/placement", )
95     //managementRouter.Get("/vscpData", )// "/getvsorcdata"
96     //managementRouter.Get("/controllerData", ) // "/getcontrollerdata"
97     //controllerRouter.Get("/resetflows", )//not sure if here or managementRouter
98     //controllerRouter.Get("/listswitch", )
99     //controllerRouter.Get("/status", )
100     //controllerRouter.Get("/tablestatus", )
101     //controllerRouter.Get("/portDescription", )// "/portsdesc"
102     //controllerRouter.Get("/portStatus", )// "/portsstat"
103     //managementRouter.Get("/startController", )// "/startcontroller"
104     //managementRouter.Get("/startcontrollerAPI", )// "/startcontrollerrouter"
105     //managementRouter.Get("/stopController", )// "/stopcontroller"
106     //mininetApiRouter.Get("/sendCommand", )// "/sendcommand"
107     //managementRouter.Get("/cancel", )// "/cancel"
108     //managementRouter.Get("/startVsorc", )// "/startvsorc"
109     //managementRouter.Get("/stopVsorc", )// "/stopvsorc"
110     //http.Handle("/", r)
111
112 //Mounting all of the subrouters:
113     s.Router.Mount("/health", healthRouter)
114     s.Router.Mount("/controller", controllerRouter)
115     s.Router.Mount("/management", managementRouter)
116     s.Router.Mount("/virtualAPI", mininetApiRouter)
117
118 }
119
120
121
122 //GenericHandler404 is the universal 404 response of this front end
123 func GenericHandler404(w http.ResponseWriter, r *http.Request){
124     w.WriteHeader(404)
125     w.Write([]byte("route does not exist"))
126 }
127 //GenericHandler405 is the universal 405 response of this front end
128 func GenericHandler405(w http.ResponseWriter, r *http.Request){
129     w.WriteHeader(405)
130     w.Write([]byte("Method not valid"))
131 }
132 func ExampleHandler(w http.ResponseWriter, r *http.Request) {
133     var1 := chi.URLParam(r, "first")
134     var2 := chi.URLParam(r, "second")
135     w.WriteHeader(http.StatusOK)
136     fmt.Fprintf(w, "First: %v\nSecond: %v", var1, var2)
137 }