-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Change directory structure: cli in cmd/dserve - Refactor application code to be usable as package
- Loading branch information
1 parent
b8c2918
commit f737abd
Showing
20 changed files
with
132 additions
and
2,212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
index.html | ||
releases | ||
secure/ |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
index.html | ||
secure/ | ||
releases/ |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/peteretelej/dserve" | ||
) | ||
|
||
var ( | ||
dir = flag.String("dir", "./", "the directory to serve, defaults to current directory") | ||
port = flag.Int("port", 9011, "the port to serve at, defaults 9011") | ||
local = flag.Bool("local", false, "whether to serve on all address or on localhost, default all addresses") | ||
secure = flag.Bool("secure", false, "whether to create a basic_auth secured secure/ directory, default false") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if err := os.Chdir(*dir); err != nil { | ||
handleFatal(err) | ||
return | ||
} | ||
var addr string | ||
if *local { | ||
addr = "localhost" | ||
} | ||
listenAddr := fmt.Sprintf("%s:%d", addr, *port) | ||
|
||
log.Printf("Launching dserve: serving %s on %s", *dir, listenAddr) | ||
if err := dserve.Serve(listenAddr, *secure); err != nil { | ||
handleFatal(err) | ||
return | ||
} | ||
} | ||
|
||
func handleFatal(err error) { | ||
log.Print("dserve fatal error: %v", err) | ||
time.Sleep(5 * time.Second) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package dserve | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func Serve(listenAddr string, secureDir bool) error { | ||
mux := http.NewServeMux() | ||
|
||
fs := http.FileServer(http.Dir(".")) | ||
mux.Handle("/", fs) | ||
|
||
if secureDir { | ||
if err := authInit(); err != nil { | ||
return fmt.Errorf("failed to initialize secure/ dir: %v", err) | ||
} | ||
mux.HandleFunc("/secure/", handleSecure) | ||
} | ||
|
||
svr := &http.Server{ | ||
Addr: listenAddr, | ||
Handler: mux, | ||
ReadTimeout: 15 * time.Second, | ||
WriteTimeout: 30 * time.Second, | ||
MaxHeaderBytes: 1 << 20, | ||
} | ||
return svr.ListenAndServe() | ||
} | ||
|
||
func handleSecure(w http.ResponseWriter, r *http.Request) { | ||
if validBasicAuth(r) { | ||
fs := http.FileServer(http.Dir("secure/static")) | ||
h := http.StripPrefix("/secure/", fs) | ||
h.ServeHTTP(w, r) | ||
return | ||
} | ||
w.Header().Set("WWW-Authenticate", `Basic realm="Dserve secure/ Basic Authentication"`) | ||
w.WriteHeader(http.StatusUnauthorized) | ||
w.Write([]byte("401 Unauthorized\n")) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.