added a personalized port and tested some things around
[VSCPweb/.git] / vscpweb.go
index 23a07ab6cd6a8b914d258254b17eb393485ceafa..d4c3ad30d727ab80c57200e2450b0a83513ff242 100644 (file)
@@ -4,8 +4,12 @@
 package main
 
 import (
+       "flag"
        "fmt"
+       "html/template"
+       "math/rand"
        "net/http"
+       "strconv"
 
        hh "healthHandlers"
        //mh "managementHandlers"
@@ -17,13 +21,22 @@ import (
        "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
@@ -92,7 +106,6 @@ func (s *server) MountHandlers() {
        //healthRouter.Get("/", )//this renders the health page "/health"
        //healthRouter.Get("/ping", )// "/rpiping"
        healthRouter.Get("/masterHealth", hh.MasterHealthHandler) //return a JSON with healt of the master "/free"
-       healthRouter.Get("/", hh.MasterHealthHandler) //return a JSON with healt of the master "/free"
        //controllerRouter.Delete("/flow", )//this is an external request "/flowdel"
        //controllerRouter.Get("/resetflows", )//not sure if here or managementRouter
        //controllerRouter.Get("/listswitch", )
@@ -129,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
@@ -144,3 +160,11 @@ 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"))
+       indexTemplate.Execute(w, passarg)
+
+}