Skip to content

Commit

Permalink
refactor move assets into separate subdir
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasf committed Feb 6, 2022
1 parent 40f1736 commit 05180fd
Show file tree
Hide file tree
Showing 20 changed files with 44 additions and 43 deletions.
18 changes: 18 additions & 0 deletions assets/assets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package assets

import (
"embed"

"github.com/benbjohnson/hashfs"
)

//go:embed static
var StaticFS embed.FS

var StaticHashFS = hashfs.NewFS(StaticFS)

//go:embed template
var TemplateFS embed.FS

//go:embed manuf
var ManufTxt []byte
File renamed without changes.
2 changes: 1 addition & 1 deletion static/clients.js → assets/static/clients.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 2 additions & 4 deletions natbwmon.go → main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ import (

"github.com/go-pa/flagutil"
"github.com/peterbourgon/ff/v3"
"github.com/some-programs/natbwmon/assets"
"github.com/some-programs/natbwmon/internal/log"
"github.com/some-programs/natbwmon/internal/mon"
"github.com/tomruk/oui"
)

//go:embed manuf
var manufTxt []byte

// Flags contains the top level program configuration.
type Flags struct {
chain string
Expand Down Expand Up @@ -120,7 +118,7 @@ func main() {
log.Fatal().Err(err).Msg("")
}

ouiDB, err := oui.NewDB(manufTxt)
ouiDB, err := oui.NewDB(assets.ManufTxt)
if err != nil {
log.Fatal().Err(err).Msg("")
}
Expand Down
26 changes: 21 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/benbjohnson/hashfs"
"github.com/justinas/alice"
"github.com/rs/zerolog/hlog"
"github.com/some-programs/natbwmon/assets"
"github.com/some-programs/natbwmon/internal/clientstats"
"github.com/some-programs/natbwmon/internal/log"
"github.com/some-programs/natbwmon/internal/mon"
Expand Down Expand Up @@ -68,17 +69,22 @@ func (s *Server) Routes() *http.ServeMux {
if s.nmapEnabled {
mux.Handle("/v0/nmap/", c.Then(s.NmapV0()))
}
mux.Handle("/static/", c.Then(hashfs.FileServer(StaticHashFS)))
mux.Handle("/static/", c.Then(hashfs.FileServer(assets.StaticHashFS)))

return mux
}

// clientsTemplateData .
type clientsTemplateData struct {
Title string
}

func (s *Server) Clients() AppHandler {
tmpl, err := template.New("base.html").Funcs(
template.FuncMap{
"static": StaticHashFS.HashName,
"static": assets.StaticHashFS.HashName,
},
).ParseFS(TemplateFS, "template/base.html", "template/clients.html")
).ParseFS(assets.TemplateFS, "template/base.html", "template/clients.html")
if err != nil {
log.Fatal().Err(err).Msg("parse templates")
}
Expand All @@ -94,11 +100,21 @@ func (s *Server) Clients() AppHandler {
}
}

// conntrackTemplateData .
type conntrackTemplateData struct {
Title string
FS mon.FlowSlice
IPFilter string
OrderFilter string
NMAP bool
IP string
}

// Conntrack displays a page with the
func (s *Server) Conntrack() AppHandler {
templ, err := template.New("base.html").Funcs(
template.FuncMap{
"static": StaticHashFS.HashName,
"static": assets.StaticHashFS.HashName,
"ipclass": func(ip net.IP) string {
if ip.IsLoopback() || ip.IsMulticast() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
return "blue"
Expand All @@ -112,7 +128,7 @@ func (s *Server) Conntrack() AppHandler {
return clientstats.FmtBytes(float64(n), "")
},
},
).ParseFS(TemplateFS, "template/base.html", "template/conntrack.html")
).ParseFS(assets.TemplateFS, "template/base.html", "template/conntrack.html")
if err != nil {
log.Fatal().Err(err).Msg("")
}
Expand Down
31 changes: 0 additions & 31 deletions template.go

This file was deleted.

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./static/" /* Redirect output structure to the directory. */,
"outDir": "./assets/static/" /* Redirect output structure to the directory. */,
"rootDir": "./ts/" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
Expand Down
2 changes: 1 addition & 1 deletion update-oui.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

set -e

exec curl -o manuf https://gitlab.com/wireshark/wireshark/-/raw/master/manuf
exec curl -o assets/manuf https://gitlab.com/wireshark/wireshark/-/raw/master/manuf

0 comments on commit 05180fd

Please sign in to comment.