perkeep/server/go/blobserver/camlistored.go

89 lines
2.2 KiB
Go
Raw Normal View History

2010-06-12 21:45:58 +00:00
// Copyright 2010 Brad Fitzpatrick <brad@danga.com>
//
// See LICENSE.
package main
import (
"flag"
"fmt"
"http"
"os"
)
2010-06-12 21:45:58 +00:00
var listen *string = flag.String("listen", "0.0.0.0:3179", "host:port to listen on")
2010-07-26 05:18:21 +00:00
var flagStorageRoot *string = flag.String("root", "/tmp/camliroot", "Root directory to store files")
2010-07-18 16:56:31 +00:00
var stealthMode *bool = flag.Bool("stealth", true, "Run in stealth mode.")
2010-06-12 21:45:58 +00:00
2010-10-04 15:28:14 +00:00
func handleCamli(conn http.ResponseWriter, req *http.Request) {
handler := func (conn http.ResponseWriter, req *http.Request) {
2010-07-18 18:08:45 +00:00
badRequestError(conn, "Unsupported path or method.")
}
switch req.Method {
case "GET":
2010-07-26 05:18:21 +00:00
switch req.URL.Path {
case "/camli/enumerate-blobs":
handler = requireAuth(handleEnumerateBlobs)
default:
handler = requireAuth(handleGet)
}
case "POST":
switch req.URL.Path {
case "/camli/preupload":
2010-07-18 18:08:45 +00:00
handler = requireAuth(handlePreUpload)
case "/camli/upload":
2010-07-18 18:08:45 +00:00
handler = requireAuth(handleMultiPartUpload)
case "/camli/testform": // debug only
2010-07-18 18:08:45 +00:00
handler = handleTestForm
case "/camli/form": // debug only
2010-07-18 18:08:45 +00:00
handler = handleCamliForm
}
case "PUT": // no longer part of spec
2010-07-18 18:08:45 +00:00
handler = requireAuth(handlePut)
2010-06-13 00:15:49 +00:00
}
2010-07-18 18:08:45 +00:00
handler(conn, req)
2010-06-12 21:45:58 +00:00
}
2010-10-04 15:28:14 +00:00
func handleRoot(conn http.ResponseWriter, req *http.Request) {
2010-07-18 16:56:31 +00:00
if *stealthMode {
fmt.Fprintf(conn, "Hi.\n")
} else {
2010-07-26 03:34:04 +00:00
fmt.Fprintf(conn, "This is camlistored, a Camlistore storage daemon.\n");
2010-11-05 06:21:08 +00:00
fmt.Fprintf(conn, "<p><a href=/js>js interface</a>");
2010-07-18 16:56:31 +00:00
}
2010-06-12 21:45:58 +00:00
}
func main() {
flag.Parse()
2010-07-18 18:08:45 +00:00
accessPassword = os.Getenv("CAMLI_PASSWORD")
if len(accessPassword) == 0 {
2010-06-12 21:45:58 +00:00
fmt.Fprintf(os.Stderr,
"No CAMLI_PASSWORD environment variable set.\n")
os.Exit(1)
}
{
2010-07-26 05:18:21 +00:00
fi, err := os.Stat(*flagStorageRoot)
2010-06-12 21:45:58 +00:00
if err != nil || !fi.IsDirectory() {
fmt.Fprintf(os.Stderr,
"Storage root '%s' doesn't exist or is not a directory.\n",
2010-07-26 05:18:21 +00:00
*flagStorageRoot)
2010-06-12 21:45:58 +00:00
os.Exit(1)
}
}
mux := http.NewServeMux()
2010-07-26 03:34:04 +00:00
mux.HandleFunc("/", handleRoot)
2010-06-12 21:45:58 +00:00
mux.HandleFunc("/camli/", handleCamli)
2010-11-05 06:21:08 +00:00
mux.Handle("/js/", http.FileServer("../../clients/js", "/js/"))
2010-06-12 21:45:58 +00:00
fmt.Printf("Starting to listen on http://%v/\n", *listen)
err := http.ListenAndServe(*listen, mux)
if err != nil {
fmt.Fprintf(os.Stderr,
"Error in http server: %v\n", err)
os.Exit(1)
}
}