added multiple subrouters and middlewares
[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
13
14 //Creation of the router:
15     //r main router for VSCP web application
16     r := chi.NewRouter()
17     //logging enables as a middleware
18     r.Use(middleware.Logger)
19     //a middleware to check if the server is alive
20     r.Use(middleware.Heartbeat("/ping"))
21     //a profiler to check healt of server
22     r.Mount("/debug", middleware.Profiler())
23     //recover from panics and send a 500 internal error
24     r.Use(middleware.Recoverer)
25     //personalized 404
26     r.NotFound(genericHandler404)
27     //personalized 405
28     r.MethodNotAllowed(genericHandler405)
29
30 //example:
31     //example of a get function and its handler
32     r.Get("/example/{first}/{second:[0-9]+}", exampleHandler)
33
34 //Creating subrouters:
35     //healthRouter check on the health of nodes or main server
36     healthRouter := chi.NewRouter()
37     //controllerRouter Agnostic openVswitch controller bridge
38     controllerRouter := chi.NewRouter()
39     //managementRouter Start, stop, monitoring of the core (mininet)
40     managementRouter := chi.NewRouter()
41     //mininetApiRouter Interface with the virtual environment inside the core (mininet)
42     mininetApiRouter := chi.NewRouter()
43     //might want to check https://go-chi.io/#/pages/routing?id=routing-groups
44     //in order to make groups where you have other middleware like authentication
45
46 //////////////////////////////////possible routing/////////////////////////////
47     //r.Connect(pattern string, h http.HandlerFunc)
48     //r.Delete(pattern string, h http.HandlerFunc)
49     //r.Get(pattern string, h http.HandlerFunc)
50     //r.Head(pattern string, h http.HandlerFunc)
51     //r.Options(pattern string, h http.HandlerFunc)
52     //r.Patch(pattern string, h http.HandlerFunc)
53     //r.Post(pattern string, h http.HandlerFunc)
54     //r.Put(pattern string, h http.HandlerFunc)
55     //r.Trace(pattern string, h http.HandlerFunc)
56 ///////////////////////////////////////////////////////////////////////////////
57
58     //r.Get("/", )
59     //r.Get("/access", )
60     //r.Get("/stats", )
61     //r.Get("/topology", )
62     //r.Get("/health", )
63     //r.Get("/free", )//probably do this with server side or sockets
64     //r.Get("/flowdel", )//this is an external request
65     //r.Get("/mpstat", )
66     //r.Get("/ifstat", )
67     //r.Get("/showtemp", )
68     //r.Get("/gettopo", )
69     //r.Get("/net", )
70     //r.Get("/rpiping", )//workerping
71     //r.Get("/nodes", )
72     //r.Get("/statusnodes", )
73     //r.Get("/intfs", )
74     //r.Get("/iperf", )
75     //r.Get("/pingall", )
76     //r.Get("/placement", )
77     //r.Get("/getvsorcdata", )
78     //r.Get("/getcontrollerdata", )
79     //r.Get("/resetflows", )
80     //r.Get("/listswitch", )
81     //r.Get("/status", )
82     //r.Get("/tablestatus", )
83     //r.Get("/portsdesc", )
84     //r.Get("/portsstat", )
85     //r.Get("/startcontroller", )
86     //r.Get("/startcontrollerrouter", )
87     //r.Get("/stopcontroller", )
88     //r.Get("/sendcommand", )
89     //r.Get("/cancel", )
90     //r.Get("/startvsorc", )
91     //r.Get("/stopvsorc", )
92     //http.Handle("/", r)
93
94 //Mounting all of the subrouters:
95     r.Mount("/health", healthRouter)
96     r.Mount("/controller", controllerRouter)
97     r.Mount("/management", managementRouter)
98     r.Mount("/virtualAPI", mininetApiRouter)
99
100 //staring up the server
101     http.ListenAndServe(":8000", r)
102 }
103
104
105
106
107
108
109
110
111
112 //genericHandler404 is the universal 404 response of this front end
113 func genericHandler404(w http.ResponseWriter, r *http.Request){
114     w.WriteHeader(404)
115     w.Write([]byte("route does not exist"))
116 }
117 //genericHandler405 is the universal 405 response of this front end
118 func genericHandler405(w http.ResponseWriter, r *http.Request){
119     w.WriteHeader(405)
120     w.Write([]byte("Method not valid"))
121 }
122 func exampleHandler(w http.ResponseWriter, r *http.Request) {
123     var1 := chi.URLParam(r, "first")
124     var2 := chi.URLParam(r, "second")
125     w.WriteHeader(http.StatusOK)
126     fmt.Fprintf(w, "First: %v\nSecond: %v", var1, var2)
127 }