Skip to content

Commit

Permalink
Embed static files
Browse files Browse the repository at this point in the history
  • Loading branch information
Kashkovsky committed Apr 14, 2022
1 parent e1ebdcf commit 19738d5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 46 deletions.
52 changes: 6 additions & 46 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ THE SOFTWARE.
package cmd

import (
"log"
"net/http"
"sync"

"github.com/Kashkovsky/hostmonitor/core"
"github.com/gorilla/websocket"
"github.com/Kashkovsky/hostmonitor/web"
"github.com/spf13/cobra"
)

Expand All @@ -38,49 +33,14 @@ var serveCmd = &cobra.Command{
Run: runServe,
}

var port int

func init() {
rootCmd.AddCommand(serveCmd)
serveCmd.Flags().StringVarP(&address, "address", "a", "0.0.0.0:8080", "Server address")
serveCmd.Flags().IntVarP(&port, "port", "p", 8080, "Server port")
}

var upgrader = websocket.Upgrader{}
var address string

func runServe(cmd *cobra.Command, args []string) {
http.HandleFunc("/ws", serveWs)
http.HandleFunc("/", serveHome)
log.Fatal(http.ListenAndServe(address, nil))
}

func serveWs(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}

defer c.Close()
watcher := core.NewWatcher(&watchConfig)
mu := sync.Mutex{}

watcher.Watch(func(res core.TestResult) {
mu.Lock()
defer mu.Unlock()
err = c.WriteJSON(res)
if err != nil {
log.Println("write:", err)
}
})
}

func serveHome(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, "Not found", http.StatusNotFound)
return
}
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
http.ServeFile(w, r, "public/home.html")
s := web.NewServer(&watchConfig, port)
s.Run()
}
Binary file modified hostmonitor
Binary file not shown.
71 changes: 71 additions & 0 deletions web/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package web

import (
"embed"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"sync"

"github.com/Kashkovsky/hostmonitor/core"
"github.com/gorilla/websocket"
)

//go:embed static
var embededFiles embed.FS

type Server struct {
port int
upgrader *websocket.Upgrader
config *core.WatchConfig
}

func NewServer(config *core.WatchConfig, port int) Server {
var upgrader = websocket.Upgrader{}
return Server{port: port, upgrader: &upgrader, config: config}
}

func (s *Server) Run() {
http.HandleFunc("/ws", s.serveWs)
useOS := len(os.Args) > 1 && os.Args[1] == "live"
http.Handle("/", http.FileServer(getFileSystem(useOS)))
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", s.port), nil))
}

func (s *Server) serveWs(w http.ResponseWriter, r *http.Request) {
c, err := s.upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}

defer c.Close()
watcher := core.NewWatcher(s.config)
mu := sync.Mutex{}

watcher.Watch(func(res core.TestResult) {
mu.Lock()
defer mu.Unlock()
err = c.WriteJSON(res)
if err != nil {
log.Println("write:", err)
}
})
}

func getFileSystem(useOS bool) http.FileSystem {
if useOS {
log.Print("using live mode")
return http.FS(os.DirFS("static"))
}

log.Print("using embed mode")
fsys, err := fs.Sub(embededFiles, "static")
if err != nil {
panic(err)
}

return http.FS(fsys)
}
File renamed without changes.

0 comments on commit 19738d5

Please sign in to comment.