summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHorus32015-12-04 17:05:00 +0100
committerHorus32015-12-04 17:05:00 +0100
commite34ee8e9ff1ebda0ec6f32e2bf93a8fd3e2a003b (patch)
treeb880e38df8524b22b98ba5cdfd4e0ced797e9bf5
parent1276eba6a546b61b8396e838e3043f2e609ce7d4 (diff)
downloaduhttpd-e34ee8e9ff1ebda0ec6f32e2bf93a8fd3e2a003b.tar.gz
Starts a browser window on startup.
-rw-r--r--main.go18
-rw-r--r--open.go26
-rw-r--r--view.go1
3 files changed, 41 insertions, 4 deletions
diff --git a/main.go b/main.go
index fc55aeb..6bd78b5 100644
--- a/main.go
+++ b/main.go
@@ -53,12 +53,12 @@ func main() {
return
}
- var _haveAddr bool = false
+ var hasAddr bool = false
for cnt, adr := range addrs {
if ipnet, ok := adr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
- _haveAddr = true
+ hasAddr = true
if cnt > 1 {
fmt.Printf(" or http://%s:%s/", ipnet.IP.String(), port)
} else {
@@ -67,7 +67,7 @@ func main() {
}
}
}
- if !_haveAddr {
+ if !hasAddr {
fmt.Printf("http://127.0.0.1:%s (Info: No internet access detected)", port)
}
} else {
@@ -84,5 +84,15 @@ func main() {
_allow_upload = true
}
mux.Handle("/", accessLog(http.FileServer(http.Dir(*dir_f)), *quiet_f))
- log.Fatal(http.ListenAndServe(*ip_f+":"+port, mux))
+ //log.Fatal(http.ListenAndServe(*ip_f+":"+port, mux))
+ listener, err := net.Listen("tcp", *ip_f+":"+port)
+ if err != nil {
+ log.Fatal("Can't listen on "+*ip_f+":"+port, err)
+ }
+ go openInBrowser("http://" + *ip_f + ":" + port + "/upload")
+
+ err = http.Serve(listener, mux)
+ if err != nil {
+ log.Fatal("Can't listen on "+*ip_f+":"+port, err)
+ }
}
diff --git a/open.go b/open.go
new file mode 100644
index 0000000..93be28b
--- /dev/null
+++ b/open.go
@@ -0,0 +1,26 @@
+package main
+
+import (
+ "log"
+ "os/exec"
+ "runtime"
+)
+
+// Open opens a file (or a directory or url), just as if the user had double-clicked the file's icon.
+// It uses the default application, as determined by the OS.
+func openInBrowser(path string) {
+ var args []string
+ switch runtime.GOOS {
+ case "darwin":
+ args = []string{"open", path}
+ case "windows":
+ args = []string{"cmd", "/c", "start", path}
+ default:
+ args = []string{"xdg-open", path}
+ }
+ cmd := exec.Command(args[0], args[1:]...)
+ err := cmd.Run()
+ if err != nil {
+ log.Println("u4.Open:", err)
+ }
+}
diff --git a/view.go b/view.go
index 005be2c..c487042 100644
--- a/view.go
+++ b/view.go
@@ -25,6 +25,7 @@ func getTemplate() string {
<a href="{{.Url}}" class="{{if .IsDir}}dir" title="Directory: {{.Text}}"><span class="icono-folder"></span>{{else}}file" title="File: {{.Text}}"><span class="icono-file"></span>{{end}} {{.Text}}</a><br>
{{end}}
</blockquote>
+ <p>Generated by <a href="//uhttpd.iamfabulous.de" title="µhttpd - A self-contained instant micro webserver.">µhttpd</a>.</p>
</div>
</div>
</div>