From 0824291bc24aa2f2ee1e38259bbf36c099900f22 Mon Sep 17 00:00:00 2001 From: lipengyu Date: Thu, 1 Dec 2022 12:41:20 +0800 Subject: [PATCH 1/2] update default port and emmit error when port already in use --- server/init.go | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/server/init.go b/server/init.go index 34f49a3..4628243 100644 --- a/server/init.go +++ b/server/init.go @@ -8,7 +8,6 @@ import ( // httpConn web server connection parameters type httpConn struct { - address string port string root string cascadeDir string @@ -16,8 +15,7 @@ type httpConn struct { func main() { httpConn := &httpConn{ - address: "localhost", - port: "5000", + port: "6060", root: "./", cascadeDir: "./cascade/", } @@ -32,7 +30,7 @@ func initServer(c *httpConn) { log.Fatalln(err) } - log.Printf("serving %s on %s:%s", c.root, c.address, c.port) + log.Printf("serving %s on localhost:%s", c.root, c.port) http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir(c.root)))) http.Handle("/cascade/", http.StripPrefix("/cascade/", http.FileServer(http.Dir(c.cascadeDir)))) @@ -40,12 +38,5 @@ func initServer(c *httpConn) { 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(":"+c.port, handler)) } From f429337cc09246cd78b446308d30e2c4fd3d5136 Mon Sep 17 00:00:00 2001 From: lipengyu Date: Fri, 2 Dec 2022 13:04:11 +0800 Subject: [PATCH 2/2] refactor server/init.go --- Makefile | 2 +- server/init.go | 44 +++++++++++++++++++++++++++----------------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 4d530de..36bd4a3 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/server/init.go b/server/init.go index 4628243..62c0e24 100644 --- a/server/init.go +++ b/server/init.go @@ -6,37 +6,47 @@ import ( "path/filepath" ) +var defaultConn = &httpConn{ + host: "", + port: "6060", + path: "./", + cascadePath: "./cascade/", +} + // httpConn web server connection parameters type httpConn struct { - port string - root string - cascadeDir string + host string + port string + path string + cascadePath string } -func main() { - httpConn := &httpConn{ - port: "6060", - 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 localhost:%s", c.root, 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) }) - log.Fatalln(http.ListenAndServe(":"+c.port, handler)) + + log.Fatalln(http.ListenAndServe(defaultConn.addr(), handler)) }