2011-03-01 16:29:44 +00:00
/ *
Copyright 2011 Google Inc .
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2010-07-11 05:57:53 +00:00
package main
import (
"flag"
"fmt"
2010-12-20 22:57:41 +00:00
"log"
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
"net/http"
2013-02-03 19:54:01 +00:00
"net/url"
2010-07-11 05:57:53 +00:00
"os"
2013-02-03 19:54:01 +00:00
"strconv"
"strings"
2011-05-16 04:54:31 +00:00
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
"camlistore.org/pkg/client"
2013-02-18 23:35:43 +00:00
"camlistore.org/pkg/cmdmain"
2013-01-03 04:32:13 +00:00
"camlistore.org/pkg/httputil"
2010-07-11 05:57:53 +00:00
)
2011-09-25 00:20:47 +00:00
const buffered = 16 // arbitrary
var (
2013-02-03 19:54:01 +00:00
flagProxyLocal = false
flagHTTP = flag . Bool ( "verbose_http" , false , "show HTTP request summaries" )
2011-09-25 00:20:47 +00:00
)
2011-09-17 23:59:04 +00:00
2013-02-18 23:35:43 +00:00
var cachedUploader * Uploader // initialized by getUploader
2011-09-26 00:40:01 +00:00
2013-02-03 19:54:01 +00:00
func init ( ) {
if debug , _ := strconv . ParseBool ( os . Getenv ( "CAMLI_DEBUG" ) ) ; debug {
flag . BoolVar ( & flagProxyLocal , "proxy_local" , false , "If true, the HTTP_PROXY environment is also used for localhost requests. This can be helpful during debugging." )
}
2013-02-18 23:35:43 +00:00
cmdmain . ExtraFlagRegistration = func ( ) {
client . AddFlags ( )
}
cmdmain . PreExit = func ( ) {
up := getUploader ( )
stats := up . Stats ( )
log . Printf ( "Client stats: %s" , stats . String ( ) )
log . Printf ( " #HTTP reqs: %d" , up . transport . Requests ( ) )
}
2013-02-03 19:54:01 +00:00
}
2013-02-18 23:35:43 +00:00
func getUploader ( ) * Uploader {
if cachedUploader == nil {
cachedUploader = newUploader ( )
2011-09-26 00:40:01 +00:00
}
2013-02-18 23:35:43 +00:00
return cachedUploader
2011-09-26 00:40:01 +00:00
}
2012-04-13 02:36:00 +00:00
func handleResult ( what string , pr * client . PutResult , err error ) error {
2010-12-24 15:46:12 +00:00
if err != nil {
log . Printf ( "Error putting %s: %s" , what , err )
2013-02-27 21:04:08 +00:00
cmdmain . ExitWithFailure = true
2012-04-13 02:36:00 +00:00
return err
2010-12-24 15:46:12 +00:00
}
2011-09-25 00:20:47 +00:00
fmt . Println ( pr . BlobRef . String ( ) )
2012-04-13 02:36:00 +00:00
return nil
2010-12-24 15:46:12 +00:00
}
2013-02-03 19:54:01 +00:00
func getenvEitherCase ( k string ) string {
if v := os . Getenv ( strings . ToUpper ( k ) ) ; v != "" {
return v
}
return os . Getenv ( strings . ToLower ( k ) )
}
// proxyFromEnvironment is similar to http.ProxyFromEnvironment but it skips
// $NO_PROXY blacklist so it proxies every requests, including localhost
// requests.
func proxyFromEnvironment ( req * http . Request ) ( * url . URL , error ) {
proxy := getenvEitherCase ( "HTTP_PROXY" )
if proxy == "" {
return nil , nil
}
proxyURL , err := url . Parse ( proxy )
if err != nil || proxyURL . Scheme == "" {
if u , err := url . Parse ( "http://" + proxy ) ; err == nil {
proxyURL = u
err = nil
}
}
if err != nil {
return nil , fmt . Errorf ( "invalid proxy address %q: %v" , proxy , err )
}
return proxyURL , nil
}
2012-04-21 14:22:32 +00:00
func newUploader ( ) * Uploader {
2011-03-12 21:10:56 +00:00
cc := client . NewOrFail ( )
2013-02-18 23:35:43 +00:00
if ! * cmdmain . FlagVerbose {
2011-03-12 21:10:56 +00:00
cc . SetLogger ( nil )
2011-01-18 18:29:38 +00:00
}
2011-09-17 01:03:13 +00:00
2013-02-03 19:54:01 +00:00
proxy := http . ProxyFromEnvironment
if flagProxyLocal {
proxy = proxyFromEnvironment
}
2013-06-22 23:35:48 +00:00
tr := cc . TransportForConfig (
& client . TransportConfig {
Proxy : proxy ,
Verbose : * flagHTTP ,
} )
httpStats , _ := tr . ( * httputil . StatsTransport )
cc . SetHTTPClient ( & http . Client { Transport : tr } )
2011-09-17 22:14:37 +00:00
2011-09-17 01:03:13 +00:00
pwd , err := os . Getwd ( )
if err != nil {
log . Fatalf ( "os.Getwd: %v" , err )
}
2012-04-21 14:22:32 +00:00
return & Uploader {
2011-09-26 00:40:01 +00:00
Client : cc ,
2012-11-08 03:00:22 +00:00
transport : httpStats ,
2011-09-26 00:40:01 +00:00
pwd : pwd ,
2011-05-16 21:35:38 +00:00
}
2011-09-27 03:02:35 +00:00
}
2011-09-17 22:14:37 +00:00
2011-09-26 00:40:01 +00:00
func main ( ) {
2013-02-27 21:04:08 +00:00
cmdmain . Main ( )
2011-09-26 00:40:01 +00:00
}