Merge branch 'master' of https://github.com/josuer08/VSCPweb
[VSCPweb/.git] / vscpweb.go
index a977ad8f10652aca5689b474aa8cc3af42d00424..a304d5ad90ac0062d291b4a52a0f22d93d791432 100644 (file)
@@ -1,29 +1,42 @@
 // This is a system for playing with SDN based openvswitches using a simple web
 // interface. This project will be combined with a CLI version or probably just
 // made compatible with a core version in the future.
-package vscpweb
+package main
 
 import (
+       "flag"
        "fmt"
+       "html/template"
+       "math/rand"
        "net/http"
+       "strconv"
 
        hh "healthHandlers"
-       mh "managementHandlers"
-       vh "virtualAPIHandlers"
-       ch "controllerHandlers"
-       sh "staticHandlers"
+       //mh "managementHandlers"
+       //vh "virtualAPIHandlers"
+       //ch "controllerHandlers"
+       //sh "staticHandlers"
 
        "github.com/go-chi/chi/v5"
        "github.com/go-chi/chi/v5/middleware"
 )
 
-
 func main() {
        //create a new server and mount the handlers:
        s := CreateNewServer()
        s.MountHandlers()
+
+       //Handle the static files
+       fs := http.FileServer(http.Dir("./static/"))
+       s.Router.Handle("/static/*", http.StripPrefix("/static/", fs))
+
        //staring up the server:
-       http.ListenAndServe(":8000", s.Router)
+       port := flag.Int("port", 8001, "Port in which the server will be started")
+       flag.Parse()
+       setPort := strconv.Itoa(*port)
+       setPort = ":"+setPort
+
+       http.ListenAndServe(setPort , s.Router)
 }
 
 type server struct {
@@ -56,6 +69,7 @@ func (s *server) MountHandlers() {
        //example:
        //example of a get function and its handler
        s.Router.Get("/example/{first}/{second:[0-9]+}", ExampleHandler)
+       s.Router.Get("/", indexHandler)
 
        //Creating subrouters:
        //healthRouter check on the health of nodes or main server
@@ -128,8 +142,11 @@ func (s *server) MountHandlers() {
 
 // GenericHandler404 is the universal 404 response of this front end
 func GenericHandler404(w http.ResponseWriter, r *http.Request) {
-       w.WriteHeader(404)
-       w.Write([]byte("route does not exist"))
+       
+w.WriteHeader(404)
+       messages := []string{"route does not exist", "page not found", "resource not found"}
+       randomIndex := rand.Intn(len(messages))
+       w.Write([]byte(messages[randomIndex]))
 }
 
 // GenericHandler405 is the universal 405 response of this front end
@@ -143,3 +160,12 @@ func ExampleHandler(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(http.StatusOK)
        fmt.Fprintf(w, "First: %v\nSecond: %v", var1, var2)
 }
+
+func indexHandler(w http.ResponseWriter, r *http.Request) {
+       w.WriteHeader(http.StatusOK)
+       passarg := "some passing argument"
+       indexTemplate := template.Must(template.ParseFiles("templates/views/index.html"))
+    //check for go partial templates so you can extract blocks you want to use
+       indexTemplate.Execute(w, passarg)
+
+}