Skip to content

Commit

Permalink
More work on making the webservice expandable, and fixed an iteration…
Browse files Browse the repository at this point in the history
… problem for attributes that are set on an object
  • Loading branch information
lkarlslund committed Nov 25, 2021
1 parent 4eb78ae commit 1023340
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 47 deletions.
4 changes: 2 additions & 2 deletions modules/analyze/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (

bind = Command.Flags().String("bind", "127.0.0.1:8080", "Address and port of webservice to bind to")
nobrowser = Command.Flags().Bool("nobrowser", false, "Don't launch browser after starting webservice")
localhtml = Command.Flags().String("localhtml", "", "Override embedded HTML and use a local folder for webservice (for development)")
localhtml = Command.Flags().StringSlice("localhtml", nil, "Override embedded HTML and use a local folders for webservice (for development)")

WebService = NewWebservice()
)
Expand Down Expand Up @@ -156,7 +156,7 @@ func Execute(cmd *cobra.Command, args []string) error {
}
*/

err = WebService.Start(*bind, objs)
err = WebService.Start(*bind, objs, *localhtml)
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions modules/analyze/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
<!-- we put everything custom here, so we can override earlier variables -->
<link rel="stylesheet" href="adalanche.css" />



<script src="external/jquery-3.6.0.min.js"></script>
<script src="external/jqueryui/jquery-ui.min.js"></script>
<script src="external/jstree/jstree.min.js"></script>
Expand All @@ -40,6 +38,10 @@
<script src="preferences.js"></script>
<script src="graph.js"></script>
<script src="custom.js"></script>

{{range .AdditionalHeaders}}
{{.}}
{{end}}
</head>

<body class="dark-mode z-0">
Expand Down
111 changes: 68 additions & 43 deletions modules/analyze/webservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package analyze
import (
"embed"
"io/fs"
"io/ioutil"
"net/http"
"os"
"path"
"text/template"

"github.com/absfs/gofs"
"github.com/absfs/osfs"
Expand All @@ -23,21 +25,42 @@ var (
qjson = jsoniter.ConfigCompatibleWithStandardLibrary
)

type FSPrefix struct {
type UnionFS struct {
filesystems []http.FileSystem
}

func (ufs *UnionFS) AddFS(newfs http.FileSystem) {
ufs.filesystems = append(ufs.filesystems, newfs)
}

func (ufs UnionFS) Open(filename string) (fs.File, error) {
for _, fs := range ufs.filesystems {
if f, err := fs.Open(filename); err == nil {
return f, nil
}
}
return nil, os.ErrNotExist
}

type AddprefixFS struct {
Prefix string
FS fs.FS
}

func (f FSPrefix) Open(filename string) (fs.File, error) {
return f.FS.Open(path.Join(f.Prefix, filename))
func (apfs AddprefixFS) Open(filename string) (fs.File, error) {
return apfs.FS.Open(path.Join(apfs.Prefix, filename))
}

type handlerfunc func(*engine.Objects, http.ResponseWriter, *http.Request)

type webservice struct {
quit chan bool
Router *mux.Router
fs fs.FS
Objs *engine.Objects
srv *http.Server
UnionFS
Objs *engine.Objects
srv *http.Server

AdditionalHeaders []string // Additional things to add to the main page
}

func NewWebservice() *webservice {
Expand All @@ -46,64 +69,66 @@ func NewWebservice() *webservice {
Router: mux.NewRouter(),
}

ws.AddFS(http.FS(AddprefixFS{"html/", embeddedassets}))

// Add stock functions
analysisfuncs(ws)

return ws
}

type handlerfunc func(*engine.Objects, http.ResponseWriter, *http.Request)

// func (ws *webservice) RegisterHandler(path string, hf handlerfunc) {
// ws.router.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
// hf(ws.objs, w, r)
// })
// }

func (w *webservice) QuitChan() <-chan bool {
return w.quit
}

func (w *webservice) Start(bind string, objs *engine.Objects) error {
func (w *webservice) Start(bind string, objs *engine.Objects, localhtml []string) error {
w.Objs = objs

w.srv = &http.Server{
Addr: bind,
Handler: w.Router,
}

// Serve embedded static files, or from html folder if it exists
var usinglocalhtml bool
if *localhtml != "" {
// Override embedded HTML if asked to
if stat, err := os.Stat(*localhtml); err == nil && stat.IsDir() {
// Use local files if they exist
log.Info().Msgf("Switching from embedded HTML to local folder %v", *localhtml)
if osf, err := osfs.NewFS(); err == nil {
err = osf.Chdir(*localhtml) // Move up one folder, so we have html/ below us
if err != nil {
return errors.Wrap(err, "")
if len(localhtml) != 0 {
w.UnionFS = UnionFS{}
for _, html := range localhtml {
// Override embedded HTML if asked to
if stat, err := os.Stat(html); err == nil && stat.IsDir() {
// Use local files if they exist
log.Info().Msgf("Adding local HTML folder %v", html)
if osf, err := osfs.NewFS(); err == nil {
err = osf.Chdir(html)
if err != nil {
return errors.Wrap(err, "")
}

overrideassets, err := gofs.NewFs(osf)
if err != nil {
return errors.Wrap(err, "")
}
w.AddFS(http.FS(overrideassets))
}
assets, err := gofs.NewFs(osf)
if err != nil {
return errors.Wrap(err, "")
}
w.Router.PathPrefix("/").Handler(http.FileServer(http.FS(FSPrefix{
// Prefix: "html",
FS: assets,
})))
} else {
log.Fatal().Msgf("Could not add local HTML folder %v, failure: %v", html, err)
}
usinglocalhtml = true
} else {
log.Warn().Msgf("Not switching from embedded HTML to local folder %v, failure: %v", *localhtml, err)
}
}
if !usinglocalhtml {
w.Router.PathPrefix("/").Handler(http.FileServer(http.FS(FSPrefix{
Prefix: "html",
FS: embeddedassets,
})))
}

w.Router.Path("/").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
indexfile, err := w.UnionFS.Open("index.html")
if err != nil {
log.Fatal().Msgf("Could not open index.html: %v", err)
}
rawindex, _ := ioutil.ReadAll(indexfile)
indextemplate := template.Must(template.New("index").Parse(string(rawindex)))

indextemplate.Execute(rw, struct {
AdditionalHeaders []string
}{
AdditionalHeaders: w.AdditionalHeaders,
})
})
w.Router.PathPrefix("/").Handler(http.FileServer(http.FS(w.UnionFS)))

go func() {
if err := w.srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
Expand Down
1 change: 1 addition & 0 deletions modules/engine/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ func (o *Object) set(a Attribute, values AttributeValues) {

if attributenums[a].onset != nil {
attributenums[a].onset(o, a, av)
o.values.Set(a, nil) // placeholder for iteration over attributes that are set
} else {
o.values.Set(a, av)
}
Expand Down

0 comments on commit 1023340

Please sign in to comment.