Merge branch 'ttys000-master'

This commit is contained in:
esimov
2022-12-02 09:57:14 +02:00
2 changed files with 28 additions and 27 deletions

View File

@@ -25,7 +25,7 @@ demo5: facemask.wasm serve
demo6: bgblur.wasm serve
serve:
$(BROWSER) 'http://localhost:5000'
$(BROWSER) 'http://localhost:6060'
go run server/init.go
clean:

View File

@@ -6,46 +6,47 @@ import (
"path/filepath"
)
var defaultConn = &httpConn{
host: "",
port: "6060",
path: "./",
cascadePath: "./cascade/",
}
// httpConn web server connection parameters
type httpConn struct {
address string
port string
root string
cascadeDir string
host string
port string
path string
cascadePath string
}
func main() {
httpConn := &httpConn{
address: "localhost",
port: "5000",
root: "./",
cascadeDir: "./cascade/",
}
initServer(httpConn)
func (c *httpConn) addr() string {
return c.host + ":" + c.port
}
// initServer initializes the webserver
func initServer(c *httpConn) {
func init() {
var err error
c.root, err = filepath.Abs(c.root)
defaultConn.path, err = filepath.Abs(defaultConn.path)
if err != nil {
log.Fatalln(err)
}
defaultConn.cascadePath, err = filepath.Abs(defaultConn.cascadePath)
if err != nil {
log.Fatalln(err)
}
}
log.Printf("serving %s on %s:%s", c.root, c.address, c.port)
http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir(c.root))))
http.Handle("/cascade/", http.StripPrefix("/cascade/", http.FileServer(http.Dir(c.cascadeDir))))
func main() {
log.Printf("serving %s on %s", defaultConn.path, defaultConn.addr())
http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir(defaultConn.path))))
http.Handle("/cascade/", http.StripPrefix("/cascade/", http.FileServer(http.Dir(defaultConn.cascadePath))))
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Print(r.RemoteAddr + " " + r.Method + " " + r.URL.String())
http.DefaultServeMux.ServeHTTP(w, r)
})
httpServer := http.Server{
Addr: c.address + ":" + c.port,
Handler: handler,
}
err = httpServer.ListenAndServe()
if err != nil {
log.Fatalln(err)
}
log.Fatalln(http.ListenAndServe(defaultConn.addr(), handler))
}