<commit_before>package new

import (
	"fmt"
	"os"
	"path"
	"strings"

	"github.com/urfave/cli/v2"
	mcli "go-micro.dev/v4/cmd/micro/cli"
	"go-micro.dev/v4/cmd/micro/generator"
	tmpl "go-micro.dev/v4/cmd/micro/generator/template"
)

var flags []cli.Flag = []cli.Flag{
	&cli.BoolFlag{
		Name:  "jaeger",
		Usage: "Generate Jaeger tracer files",
	},
	&cli.BoolFlag{
		Name:  "kubernetes",
		Usage: "Generate Kubernetes resource files",
	},
	&cli.BoolFlag{
		Name:  "skaffold",
		Usage: "Generate Skaffold files",
	},
}

// NewCommand returns a new new cli command.
func init() {
	mcli.Register(&cli.Command{
		Name:  "new",
		Usage: "Create a project template",
		Subcommands: []*cli.Command{
			{
				Name:   "client",
				Usage:  "Create a client template, e.g. " + mcli.App().Name + " new client [github.com/auditemarlow/]helloworld",
				Action: Client,
				Flags:  flags,
			},
			{
				Name:   "function",
				Usage:  "Create a function template, e.g. " + mcli.App().Name + " new function [github.com/auditemarlow/]helloworld",
				Action: Function,
				Flags:  flags,
			},
			{
				Name:   "service",
				Usage:  "Create a service template, e.g. " + mcli.App().Name + " new service [github.com/auditemarlow/]helloworld",
				Action: Service,
				Flags:  flags,
			},
		},
	})
}

func Client(ctx *cli.Context) error {
	return createProject(ctx, "client")
}

// Function creates a new function project template. Exits on error.
func Function(ctx *cli.Context) error {
	return createProject(ctx, "function")
}

// Service creates a new service project template. Exits on error.
func Service(ctx *cli.Context) error {
	return createProject(ctx, "service")
}

func createProject(ctx *cli.Context, pt string) error {
	arg := ctx.Args().First()
	if len(arg) == 0 {
		return cli.ShowSubcommandHelp(ctx)
	}

	client := pt == "client"
	name, vendor := getNameAndVendor(arg)

	dir := name
	if client {
		dir += "-client"
	}

	if path.IsAbs(dir) {
		fmt.Println("must provide a relative path as service name")
		return nil
	}

	if _, err := os.Stat(dir); !os.IsNotExist(err) {
		return fmt.Errorf("%s already exists", dir)
	}

	fmt.Printf("creating %s %s\n", pt, name)

	g := generator.New(
		generator.Service(name),
		generator.Vendor(vendor),
		generator.Directory(dir),
		generator.Client(client),
		generator.Jaeger(ctx.Bool("jaeger")),
		generator.Skaffold(ctx.Bool("skaffold")),
	)

	files := []generator.File{
		{".dockerignore", tmpl.DockerIgnore},
		{".gitignore", tmpl.GitIgnore},
		{"Dockerfile", tmpl.Dockerfile},
		{"Makefile", tmpl.Makefile},
		{"go.mod", tmpl.Module},
	}

	switch pt {
	case "client":
		files = append(files, []generator.File{
			{"main.go", tmpl.MainCLT},
		}...)
	case "function":
		files = append(files, []generator.File{
			{"handler/" + name + ".go", tmpl.HandlerFNC},
			{"main.go", tmpl.MainFNC},
			{"proto/" + name + ".proto", tmpl.ProtoFNC},
		}...)
	case "service":
		files = append(files, []generator.File{
			{"handler/" + name + ".go", tmpl.HandlerSRV},
			{"main.go", tmpl.MainSRV},
			{"proto/" + name + ".proto", tmpl.ProtoSRV},
		}...)
	default:
		return fmt.Errorf("%s project type not supported", pt)
	}

	if ctx.Bool("kubernetes") || ctx.Bool("skaffold") {
		files = append(files, []generator.File{
			{"plugins.go", tmpl.Plugins},
			{"resources/clusterrole.yaml", tmpl.KubernetesClusterRole},
			{"resources/configmap.yaml", tmpl.KubernetesEnv},
			{"resources/deployment.yaml", tmpl.KubernetesDeployment},
			{"resources/rolebinding.yaml", tmpl.KubernetesRoleBinding},
		}...)
	}

	if ctx.Bool("skaffold") {
		files = append(files, []generator.File{
			{"skaffold.yaml", tmpl.SkaffoldCFG},
		}...)
	}

	if err := g.Generate(files); err != nil {
		return err
	}

	var comments []string
	if client {
		comments = clientComments(name, dir)
	} else {
		comments = protoComments(name, dir)
	}

	for _, comment := range comments {
		fmt.Println(comment)
	}

	return nil
}

func clientComments(name, dir string) []string {
	return []string{
		"\ninstall dependencies:",
		"\ncd " + dir,
		"make update tidy",
	}
}

func protoComments(name, dir string) []string {
	return []string{
		"\ndownload protoc zip packages (protoc-$VERSION-$PLATFORM.zip) and install:",
		"\nvisit https://github.com/protocolbuffers/protobuf/releases/latest",
		"\ndownload protobuf for go-micro:",
		"\ngo get -u google.golang.org/protobuf/proto",
		"go install github.com/golang/protobuf/protoc-gen-go@latest",
		"go install github.com/asim/go-micro/cmd/protoc-gen-micro/v4@latest",
		"\ncompile the proto file " + name + ".proto and install dependencies:",
		"\ncd " + dir,
		"make proto update tidy",
	}
}

func getNameAndVendor(s string) (string, string) {
	var n string
	var v string

	if i := strings.LastIndex(s, "/"); i == -1 {
		n = s
		v = ""
	} else {
		n = s[i+1:]
		v = s[:i+1]
	}

	return n, v
}
<commit_msg>Fix Micro CLI's proto comments (#2353)<commit_after>package new

import (
	"fmt"
	"os"
	"path"
	"strings"

	"github.com/urfave/cli/v2"
	mcli "go-micro.dev/v4/cmd/micro/cli"
	"go-micro.dev/v4/cmd/micro/generator"
	tmpl "go-micro.dev/v4/cmd/micro/generator/template"
)

var flags []cli.Flag = []cli.Flag{
	&cli.BoolFlag{
		Name:  "jaeger",
		Usage: "Generate Jaeger tracer files",
	},
	&cli.BoolFlag{
		Name:  "kubernetes",
		Usage: "Generate Kubernetes resource files",
	},
	&cli.BoolFlag{
		Name:  "skaffold",
		Usage: "Generate Skaffold files",
	},
}

// NewCommand returns a new new cli command.
func init() {
	mcli.Register(&cli.Command{
		Name:  "new",
		Usage: "Create a project template",
		Subcommands: []*cli.Command{
			{
				Name:   "client",
				Usage:  "Create a client template, e.g. " + mcli.App().Name + " new client [github.com/auditemarlow/]helloworld",
				Action: Client,
				Flags:  flags,
			},
			{
				Name:   "function",
				Usage:  "Create a function template, e.g. " + mcli.App().Name + " new function [github.com/auditemarlow/]helloworld",
				Action: Function,
				Flags:  flags,
			},
			{
				Name:   "service",
				Usage:  "Create a service template, e.g. " + mcli.App().Name + " new service [github.com/auditemarlow/]helloworld",
				Action: Service,
				Flags:  flags,
			},
		},
	})
}

func Client(ctx *cli.Context) error {
	return createProject(ctx, "client")
}

// Function creates a new function project template. Exits on error.
func Function(ctx *cli.Context) error {
	return createProject(ctx, "function")
}

// Service creates a new service project template. Exits on error.
func Service(ctx *cli.Context) error {
	return createProject(ctx, "service")
}

func createProject(ctx *cli.Context, pt string) error {
	arg := ctx.Args().First()
	if len(arg) == 0 {
		return cli.ShowSubcommandHelp(ctx)
	}

	client := pt == "client"
	name, vendor := getNameAndVendor(arg)

	dir := name
	if client {
		dir += "-client"
	}

	if path.IsAbs(dir) {
		fmt.Println("must provide a relative path as service name")
		return nil
	}

	if _, err := os.Stat(dir); !os.IsNotExist(err) {
		return fmt.Errorf("%s already exists", dir)
	}

	fmt.Printf("creating %s %s\n", pt, name)

	g := generator.New(
		generator.Service(name),
		generator.Vendor(vendor),
		generator.Directory(dir),
		generator.Client(client),
		generator.Jaeger(ctx.Bool("jaeger")),
		generator.Skaffold(ctx.Bool("skaffold")),
	)

	files := []generator.File{
		{".dockerignore", tmpl.DockerIgnore},
		{".gitignore", tmpl.GitIgnore},
		{"Dockerfile", tmpl.Dockerfile},
		{"Makefile", tmpl.Makefile},
		{"go.mod", tmpl.Module},
	}

	switch pt {
	case "client":
		files = append(files, []generator.File{
			{"main.go", tmpl.MainCLT},
		}...)
	case "function":
		files = append(files, []generator.File{
			{"handler/" + name + ".go", tmpl.HandlerFNC},
			{"main.go", tmpl.MainFNC},
			{"proto/" + name + ".proto", tmpl.ProtoFNC},
		}...)
	case "service":
		files = append(files, []generator.File{
			{"handler/" + name + ".go", tmpl.HandlerSRV},
			{"main.go", tmpl.MainSRV},
			{"proto/" + name + ".proto", tmpl.ProtoSRV},
		}...)
	default:
		return fmt.Errorf("%s project type not supported", pt)
	}

	if ctx.Bool("kubernetes") || ctx.Bool("skaffold") {
		files = append(files, []generator.File{
			{"plugins.go", tmpl.Plugins},
			{"resources/clusterrole.yaml", tmpl.KubernetesClusterRole},
			{"resources/configmap.yaml", tmpl.KubernetesEnv},
			{"resources/deployment.yaml", tmpl.KubernetesDeployment},
			{"resources/rolebinding.yaml", tmpl.KubernetesRoleBinding},
		}...)
	}

	if ctx.Bool("skaffold") {
		files = append(files, []generator.File{
			{"skaffold.yaml", tmpl.SkaffoldCFG},
		}...)
	}

	if err := g.Generate(files); err != nil {
		return err
	}

	var comments []string
	if client {
		comments = clientComments(name, dir)
	} else {
		comments = protoComments(name, dir)
	}

	for _, comment := range comments {
		fmt.Println(comment)
	}

	return nil
}

func clientComments(name, dir string) []string {
	return []string{
		"\ninstall dependencies:",
		"\ncd " + dir,
		"make update tidy",
	}
}

func protoComments(name, dir string) []string {
	return []string{
		"\ndownload protoc zip packages (protoc-$VERSION-$PLATFORM.zip) and install:",
		"\nvisit https://github.com/protocolbuffers/protobuf/releases/latest",
		"\ndownload protobuf for go-micro:",
		"\ngo get -u google.golang.org/protobuf/proto",
		"go install github.com/golang/protobuf/protoc-gen-go@latest",
		"go install go-micro.dev/v4/cmd/protoc-gen-micro@v4",
		"\ncompile the proto file " + name + ".proto and install dependencies:",
		"\ncd " + dir,
		"make proto update tidy",
	}
}

func getNameAndVendor(s string) (string, string) {
	var n string
	var v string

	if i := strings.LastIndex(s, "/"); i == -1 {
		n = s
		v = ""
	} else {
		n = s[i+1:]
		v = s[:i+1]
	}

	return n, v
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"github.com/grafana/metrictank/stacktest/graphite"
	log "github.com/sirupsen/logrus"
	"math"
	"net/http"
	"strconv"
	"time"
)

type seriesStats struct {
	lastTs uint32
	//the partition currently being checked
	partition int32
	//the number of nans present in the time series
	nans int32
	//the sum of abs(value - ts) across the time series
	deltaSum float64
	//the number of timestamps where value != ts
	numNonMatching int32

	//tracks the last seen non-NaN time stamp (useful for lag
	lastSeen uint32
}

func monitor() {
	for range time.NewTicker(queryInterval).C {

		query := graphite.ExecuteRenderQuery(buildRequest())

		for _, s := range query.Decoded {
			log.Infof("%d - %d", s.Datapoints[0].Ts, s.Datapoints[len(s.Datapoints)-1].Ts)

			stats := seriesStats{}
			stats.lastTs = s.Datapoints[len(s.Datapoints)-1].Ts

			for _, dp := range s.Datapoints {

				if math.IsNaN(dp.Val) {
					stats.nans += 1
					continue
				}
				if diff := dp.Val - float64(dp.Ts); diff != 0 {
					stats.lastSeen = dp.Ts
					stats.deltaSum += diff
					stats.numNonMatching += 1
				}
			}

			//TODO create/emit metrics for each partition

			//number of missing values for each series
			fmt.Printf("parrot.monitoring.nanCount;partition=%d; %d\n", stats.partition, stats.nans)
			//time since the last value was recorded
			fmt.Printf("parrot.monitoring.lag;partition=%d; %d\n", stats.partition, stats.lastTs-stats.lastSeen)
			//total amount of drift between expected value and actual values
			fmt.Printf("parrot.monitoring.deltaSum;partition=%d; %f\n", stats.partition, stats.deltaSum)
			//total number of entries where drift occurred
			fmt.Printf("parrot.monitoring.nonMatching;partition=%d; %d\n", stats.partition, stats.numNonMatching)
			fmt.Println()
		}
	}
}

func buildRequest() *http.Request {
	req, _ := http.NewRequest("GET", fmt.Sprintf("%s/render", gatewayAddress), nil)
	q := req.URL.Query()
	q.Set("target", "parrot.testdata.*.generated.*")
	//TODO parameterize this
	q.Set("from", "-5min")
	q.Set("until", "now")
	q.Set("format", "json")
	q.Set("X-Org-Id", strconv.Itoa(orgId))
	req.URL.RawQuery = q.Encode()
	if len(gatewayKey) != 0 {
		req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", gatewayKey))
	}
	return req
}
<commit_msg>query with explicit time ranges<commit_after>package main

import (
	"fmt"
	"github.com/grafana/metrictank/stacktest/graphite"
	log "github.com/sirupsen/logrus"
	"math"
	"net/http"
	"strconv"
	"time"
)

type seriesStats struct {
	lastTs uint32
	//the partition currently being checked
	partition int32
	//the number of nans present in the time series
	nans int32
	//the sum of abs(value - ts) across the time series
	deltaSum float64
	//the number of timestamps where value != ts
	numNonMatching int32

	//tracks the last seen non-NaN time stamp (useful for lag
	lastSeen uint32
}

func monitor() {
	for tick := range time.NewTicker(queryInterval).C {

		query := graphite.ExecuteRenderQuery(buildRequest(tick))

		for _, s := range query.Decoded {
			log.Infof("%d - %d", s.Datapoints[0].Ts, s.Datapoints[len(s.Datapoints)-1].Ts)

			stats := seriesStats{}
			stats.lastTs = s.Datapoints[len(s.Datapoints)-1].Ts

			for _, dp := range s.Datapoints {

				if math.IsNaN(dp.Val) {
					stats.nans += 1
					continue
				}
				if diff := dp.Val - float64(dp.Ts); diff != 0 {
					stats.lastSeen = dp.Ts
					stats.deltaSum += diff
					stats.numNonMatching += 1
				}
			}

			//TODO create/emit metrics for each partition

			//number of missing values for each series
			fmt.Printf("parrot.monitoring.nanCount;partition=%d; %d\n", stats.partition, stats.nans)
			//time since the last value was recorded
			fmt.Printf("parrot.monitoring.lag;partition=%d; %d\n", stats.partition, stats.lastTs-stats.lastSeen)
			//total amount of drift between expected value and actual values
			fmt.Printf("parrot.monitoring.deltaSum;partition=%d; %f\n", stats.partition, stats.deltaSum)
			//total number of entries where drift occurred
			fmt.Printf("parrot.monitoring.nonMatching;partition=%d; %d\n", stats.partition, stats.numNonMatching)
			fmt.Println()
		}
	}
}

func buildRequest(now time.Time) *http.Request {
	req, _ := http.NewRequest("GET", fmt.Sprintf("%s/render", gatewayAddress), nil)
	q := req.URL.Query()
	q.Set("target", "parrot.testdata.*.generated.*")
	q.Set("from", strconv.FormatInt(now.Add(-5*time.Minute).Unix(), 10))
	q.Set("until", strconv.FormatInt(now.Unix(), 10))
	q.Set("format", "json")
	q.Set("X-Org-Id", strconv.Itoa(orgId))
	req.URL.RawQuery = q.Encode()
	if len(gatewayKey) != 0 {
		req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", gatewayKey))
	}
	return req
}
<|endoftext|>


<commit_before>package main

import (
	_ "expvar"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"runtime"
	"strings"

	"github.com/coreos/go-etcd/etcd"
	"github.com/sourcegraph/datad"
	"github.com/sourcegraph/go-vcs/vcs"
	"github.com/sourcegraph/vcsstore"
	"github.com/sourcegraph/vcsstore/cluster"
	"github.com/sourcegraph/vcsstore/server"
	"github.com/sourcegraph/vcsstore/vcsclient"
)

var (
	storageDir = flag.String("s", "/tmp/vcsstore", "storage root dir for VCS repos")
	verbose    = flag.Bool("v", true, "show verbose output")

	etcdEndpoint  = flag.String("etcd", "http://127.0.0.1:4001", "etcd endpoint")
	etcdKeyPrefix = flag.String("etcd-key-prefix", filepath.Join(datad.DefaultKeyPrefix, "vcs"), "keyspace for datad registry and provider list in etcd")

	defaultPort = "9090"
)

func main() {
	flag.Usage = func() {
		fmt.Fprintln(os.Stderr, `vcsstore caches and serves information about VCS repositories.

Usage:

        vcsstore [options] command [arg...]

The commands are:
`)
		for _, c := range subcommands {
			fmt.Fprintf(os.Stderr, "    %-14s %s\n", c.Name, c.Description)
		}
		fmt.Fprintln(os.Stderr, `
Use "vcsstore command -h" for more information about a command.

The global options are:
`)
		flag.PrintDefaults()
		os.Exit(1)
	}
	flag.Parse()
	if flag.NArg() == 0 {
		flag.Usage()
	}

	subcmd := flag.Arg(0)
	extraArgs := flag.Args()[1:]
	for _, c := range subcommands {
		if c.Name == subcmd {
			c.Run(extraArgs)
			return
		}

	}

	fmt.Fprintf(os.Stderr, "vcsstore: unknown subcommand %q\n", subcmd)
	fmt.Fprintln(os.Stderr, `Run "vcsstore -h" for usage.`)
	os.Exit(1)
}

type subcommand struct {
	Name        string
	Description string
	Run         func(args []string)
}

var subcommands = []subcommand{
	{"serve", "start an HTTP server to serve VCS repository data", serveCmd},
	{"repo", "display information about a repository", repoCmd},
	{"clone", "clones a repository on the server", cloneCmd},
	{"get", "gets a path from the server (or datad cluster)", getCmd},
}

func etcdBackend() datad.Backend {
	return datad.NewEtcdBackend(*etcdKeyPrefix, etcd.NewClient([]string{*etcdEndpoint}))
}

func serveCmd(args []string) {
	fs := flag.NewFlagSet("serve", flag.ExitOnError)
	debug := fs.Bool("d", false, "debug mode (don't use on publicly available servers)")
	bindAddr := fs.String("http", ":"+defaultPort, "HTTP listen address")
	datadNode := fs.Bool("datad", false, "participate as a node in a datad cluster")
	datadNodeName := fs.String("datad-node-name", "127.0.0.1:"+defaultPort, "datad node name (must be accessible to datad clients & other nodes)")
	fs.Usage = func() {
		fmt.Fprintln(os.Stderr, `usage: vcsstore serve [options]

Starts an HTTP server that serves information about VCS repositories.

The options are:
`)
		fs.PrintDefaults()
		os.Exit(1)
	}
	fs.Parse(args)

	if fs.NArg() != 0 {
		fs.Usage()
	}

	err := os.MkdirAll(*storageDir, 0700)
	if err != nil {
		log.Fatalf("Error creating directory %q: %s.", *storageDir, err)
	}

	var logw io.Writer
	if *verbose {
		logw = os.Stderr
	} else {
		logw = ioutil.Discard
	}

	conf := &vcsstore.Config{
		StorageDir: *storageDir,
		Log:        log.New(logw, "vcsstore: ", log.LstdFlags),
	}
	if *debug {
		conf.DebugLog = log.New(logw, "vcsstore DEBUG: ", log.LstdFlags)
	}

	h := server.NewHandler(vcsstore.NewService(conf), nil, nil)
	h.Log = log.New(logw, "server: ", log.LstdFlags)
	h.Debug = *debug

	if *datadNode {
		node := datad.NewNode(*datadNodeName, etcdBackend(), cluster.NewProvider(conf, h.Service))
		node.Updaters = runtime.GOMAXPROCS(0)
		err := node.Start()
		if err != nil {
			log.Fatal("Failed to start datad node: ", err)
		}
		log.Printf("Started datad node %s.", *datadNodeName)
	}

	http.Handle("/", h)

	fmt.Fprintf(os.Stderr, "Starting server on %s\n", *bindAddr)
	err = http.ListenAndServe(*bindAddr, nil)
	if err != nil {
		log.Fatalf("HTTP server failed to start: %s.", err)
	}
}

func repoCmd(args []string) {
	fs := flag.NewFlagSet("repo", flag.ExitOnError)
	fs.Usage = func() {
		fmt.Fprintln(os.Stderr, `usage: vcsstore repo [options] vcs-type clone-url

Displays the directory to which a repository would be cloned.

The options are:
`)
		fs.PrintDefaults()
		os.Exit(1)
	}
	fs.Parse(args)

	if fs.NArg() != 2 {
		fs.Usage()
	}

	vcsType := fs.Arg(0)
	cloneURL, err := url.Parse(fs.Arg(1))
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("RepositoryPath:      ", filepath.Join(*storageDir, vcsstore.EncodeRepositoryPath(vcsType, cloneURL)))
	fmt.Println("URL:                 ", vcsclient.NewRouter(nil).URLToRepo(vcsType, cloneURL))
}

func cloneCmd(args []string) {
	fs := flag.NewFlagSet("clone", flag.ExitOnError)
	urlStr := fs.String("url", "http://localhost:"+defaultPort, "base URL to a running vcsstore API server")
	datadClient := fs.Bool("datad", false, "use datad cluster client")
	fs.Usage = func() {
		fmt.Fprintln(os.Stderr, `usage: vcsstore clone [options] vcs-type clone-url

Clones a repository on the server. Once finished, the repository will be
available to the client via the vcsstore API.

The options are:
`)
		fs.PrintDefaults()
		os.Exit(1)
	}
	fs.Parse(args)

	if fs.NArg() != 2 {
		fs.Usage()
	}

	baseURL, err := url.Parse(*urlStr)
	if err != nil {
		log.Fatal(err)
	}

	vcsType := fs.Arg(0)
	cloneURL, err := url.Parse(fs.Arg(1))
	if err != nil {
		log.Fatal(err)
	}

	var repo vcs.Repository
	if *datadClient {
		cc := cluster.NewClient(datad.NewClient(etcdBackend()), nil)
		repo, err = cc.Repository(vcsType, cloneURL)
		if err != nil {
			log.Fatal(err)
		}
	} else {
		c := vcsclient.New(baseURL, nil)
		repo, err = c.Repository(vcsType, cloneURL)
		if err != nil {
			log.Fatal("Open repository: ", err)
		}
	}

	if repo, ok := repo.(vcsclient.RepositoryRemoteCloner); ok {
		err := repo.CloneRemote()
		if err != nil {
			log.Fatal("Clone: ", err)
		}
	} else {
		log.Fatalf("Remote cloning is not implemented for %T.", repo)
	}

	fmt.Printf("%-5s %-45s cloned OK\n", vcsType, cloneURL)
}

func getCmd(args []string) {
	fs := flag.NewFlagSet("get", flag.ExitOnError)
	urlStr := fs.String("url", "http://localhost:"+defaultPort, "base URL to a running vcsstore API server")
	datadClient := fs.Bool("datad", false, "route request using datad (specify etcd backend in global options)")
	method := fs.String("method", "GET", "HTTP request method")
	fs.Usage = func() {
		fmt.Fprintln(os.Stderr, `usage: vcsstore get [options] vcs-type clone-url [extra-path]

Gets a URL path from the server (optionally routing the request using datad).

The options are:
`)
		fs.PrintDefaults()
		os.Exit(1)
	}
	fs.Parse(args)

	if n := fs.NArg(); n != 2 && n != 3 {
		fs.Usage()
	}
	vcsType, cloneURLStr := fs.Arg(0), fs.Arg(1)
	var extraPath string
	if fs.NArg() == 3 {
		extraPath = fs.Arg(2)
	}

	baseURL, err := url.Parse(*urlStr)
	if err != nil {
		log.Fatal(err)
	}

	cloneURL, err := url.Parse(cloneURLStr)
	if err != nil {
		log.Fatal(err)
	}

	router := vcsclient.NewRouter(nil)
	url := router.URLToRepo(vcsType, cloneURL)
	url.Path = strings.TrimPrefix(url.Path, "/")
	url = baseURL.ResolveReference(url)
	url.Path = filepath.Join(url.Path, extraPath)

	if *datadClient {
		datadGet(*method, vcsType, cloneURL, url)
	} else {
		normalGet(*method, nil, url)
	}
}

func datadGet(method string, vcsType string, cloneURL *url.URL, reqURL *url.URL) {
	cc := cluster.NewClient(datad.NewClient(etcdBackend()), nil)
	t, err := cc.TransportForRepository(vcsType, cloneURL)
	if err != nil {
		log.Fatal(err)
	}

	reqURL.Host = "$(DATAD_NODE)"
	normalGet(method, &http.Client{Transport: t}, reqURL)
}

func normalGet(method string, c *http.Client, url *url.URL) {
	if c == nil {
		c = http.DefaultClient
	}

	if *verbose {
		log.Printf("%s %s", method, url)
	}

	req, err := http.NewRequest(method, url.String(), nil)
	if err != nil {
		log.Fatal(err)
	}
	resp, err := c.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	if !(resp.StatusCode >= 200 && resp.StatusCode <= 399) {
		log.Fatal("Error: HTTP %d: %s.", resp.StatusCode, body)
	}

	fmt.Println(string(body))
}
<commit_msg>use libgit2<commit_after>package main

import (
	_ "expvar"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"runtime"
	"strings"

	"github.com/coreos/go-etcd/etcd"
	"github.com/sourcegraph/datad"
	"github.com/sourcegraph/go-vcs/vcs"
	_ "github.com/sourcegraph/go-vcs/vcs/git_libgit2"
	"github.com/sourcegraph/vcsstore"
	"github.com/sourcegraph/vcsstore/cluster"
	"github.com/sourcegraph/vcsstore/server"
	"github.com/sourcegraph/vcsstore/vcsclient"
)

var (
	storageDir = flag.String("s", "/tmp/vcsstore", "storage root dir for VCS repos")
	verbose    = flag.Bool("v", true, "show verbose output")

	etcdEndpoint  = flag.String("etcd", "http://127.0.0.1:4001", "etcd endpoint")
	etcdKeyPrefix = flag.String("etcd-key-prefix", filepath.Join(datad.DefaultKeyPrefix, "vcs"), "keyspace for datad registry and provider list in etcd")

	defaultPort = "9090"
)

func main() {
	flag.Usage = func() {
		fmt.Fprintln(os.Stderr, `vcsstore caches and serves information about VCS repositories.

Usage:

        vcsstore [options] command [arg...]

The commands are:
`)
		for _, c := range subcommands {
			fmt.Fprintf(os.Stderr, "    %-14s %s\n", c.Name, c.Description)
		}
		fmt.Fprintln(os.Stderr, `
Use "vcsstore command -h" for more information about a command.

The global options are:
`)
		flag.PrintDefaults()
		os.Exit(1)
	}
	flag.Parse()
	if flag.NArg() == 0 {
		flag.Usage()
	}

	subcmd := flag.Arg(0)
	extraArgs := flag.Args()[1:]
	for _, c := range subcommands {
		if c.Name == subcmd {
			c.Run(extraArgs)
			return
		}

	}

	fmt.Fprintf(os.Stderr, "vcsstore: unknown subcommand %q\n", subcmd)
	fmt.Fprintln(os.Stderr, `Run "vcsstore -h" for usage.`)
	os.Exit(1)
}

type subcommand struct {
	Name        string
	Description string
	Run         func(args []string)
}

var subcommands = []subcommand{
	{"serve", "start an HTTP server to serve VCS repository data", serveCmd},
	{"repo", "display information about a repository", repoCmd},
	{"clone", "clones a repository on the server", cloneCmd},
	{"get", "gets a path from the server (or datad cluster)", getCmd},
}

func etcdBackend() datad.Backend {
	return datad.NewEtcdBackend(*etcdKeyPrefix, etcd.NewClient([]string{*etcdEndpoint}))
}

func serveCmd(args []string) {
	fs := flag.NewFlagSet("serve", flag.ExitOnError)
	debug := fs.Bool("d", false, "debug mode (don't use on publicly available servers)")
	bindAddr := fs.String("http", ":"+defaultPort, "HTTP listen address")
	datadNode := fs.Bool("datad", false, "participate as a node in a datad cluster")
	datadNodeName := fs.String("datad-node-name", "127.0.0.1:"+defaultPort, "datad node name (must be accessible to datad clients & other nodes)")
	fs.Usage = func() {
		fmt.Fprintln(os.Stderr, `usage: vcsstore serve [options]

Starts an HTTP server that serves information about VCS repositories.

The options are:
`)
		fs.PrintDefaults()
		os.Exit(1)
	}
	fs.Parse(args)

	if fs.NArg() != 0 {
		fs.Usage()
	}

	err := os.MkdirAll(*storageDir, 0700)
	if err != nil {
		log.Fatalf("Error creating directory %q: %s.", *storageDir, err)
	}

	var logw io.Writer
	if *verbose {
		logw = os.Stderr
	} else {
		logw = ioutil.Discard
	}

	conf := &vcsstore.Config{
		StorageDir: *storageDir,
		Log:        log.New(logw, "vcsstore: ", log.LstdFlags),
	}
	if *debug {
		conf.DebugLog = log.New(logw, "vcsstore DEBUG: ", log.LstdFlags)
	}

	h := server.NewHandler(vcsstore.NewService(conf), nil, nil)
	h.Log = log.New(logw, "server: ", log.LstdFlags)
	h.Debug = *debug

	if *datadNode {
		node := datad.NewNode(*datadNodeName, etcdBackend(), cluster.NewProvider(conf, h.Service))
		node.Updaters = runtime.GOMAXPROCS(0)
		err := node.Start()
		if err != nil {
			log.Fatal("Failed to start datad node: ", err)
		}
		log.Printf("Started datad node %s.", *datadNodeName)
	}

	http.Handle("/", h)

	fmt.Fprintf(os.Stderr, "Starting server on %s\n", *bindAddr)
	err = http.ListenAndServe(*bindAddr, nil)
	if err != nil {
		log.Fatalf("HTTP server failed to start: %s.", err)
	}
}

func repoCmd(args []string) {
	fs := flag.NewFlagSet("repo", flag.ExitOnError)
	fs.Usage = func() {
		fmt.Fprintln(os.Stderr, `usage: vcsstore repo [options] vcs-type clone-url

Displays the directory to which a repository would be cloned.

The options are:
`)
		fs.PrintDefaults()
		os.Exit(1)
	}
	fs.Parse(args)

	if fs.NArg() != 2 {
		fs.Usage()
	}

	vcsType := fs.Arg(0)
	cloneURL, err := url.Parse(fs.Arg(1))
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("RepositoryPath:      ", filepath.Join(*storageDir, vcsstore.EncodeRepositoryPath(vcsType, cloneURL)))
	fmt.Println("URL:                 ", vcsclient.NewRouter(nil).URLToRepo(vcsType, cloneURL))
}

func cloneCmd(args []string) {
	fs := flag.NewFlagSet("clone", flag.ExitOnError)
	urlStr := fs.String("url", "http://localhost:"+defaultPort, "base URL to a running vcsstore API server")
	datadClient := fs.Bool("datad", false, "use datad cluster client")
	fs.Usage = func() {
		fmt.Fprintln(os.Stderr, `usage: vcsstore clone [options] vcs-type clone-url

Clones a repository on the server. Once finished, the repository will be
available to the client via the vcsstore API.

The options are:
`)
		fs.PrintDefaults()
		os.Exit(1)
	}
	fs.Parse(args)

	if fs.NArg() != 2 {
		fs.Usage()
	}

	baseURL, err := url.Parse(*urlStr)
	if err != nil {
		log.Fatal(err)
	}

	vcsType := fs.Arg(0)
	cloneURL, err := url.Parse(fs.Arg(1))
	if err != nil {
		log.Fatal(err)
	}

	var repo vcs.Repository
	if *datadClient {
		cc := cluster.NewClient(datad.NewClient(etcdBackend()), nil)
		repo, err = cc.Repository(vcsType, cloneURL)
		if err != nil {
			log.Fatal(err)
		}
	} else {
		c := vcsclient.New(baseURL, nil)
		repo, err = c.Repository(vcsType, cloneURL)
		if err != nil {
			log.Fatal("Open repository: ", err)
		}
	}

	if repo, ok := repo.(vcsclient.RepositoryRemoteCloner); ok {
		err := repo.CloneRemote()
		if err != nil {
			log.Fatal("Clone: ", err)
		}
	} else {
		log.Fatalf("Remote cloning is not implemented for %T.", repo)
	}

	fmt.Printf("%-5s %-45s cloned OK\n", vcsType, cloneURL)
}

func getCmd(args []string) {
	fs := flag.NewFlagSet("get", flag.ExitOnError)
	urlStr := fs.String("url", "http://localhost:"+defaultPort, "base URL to a running vcsstore API server")
	datadClient := fs.Bool("datad", false, "route request using datad (specify etcd backend in global options)")
	method := fs.String("method", "GET", "HTTP request method")
	fs.Usage = func() {
		fmt.Fprintln(os.Stderr, `usage: vcsstore get [options] vcs-type clone-url [extra-path]

Gets a URL path from the server (optionally routing the request using datad).

The options are:
`)
		fs.PrintDefaults()
		os.Exit(1)
	}
	fs.Parse(args)

	if n := fs.NArg(); n != 2 && n != 3 {
		fs.Usage()
	}
	vcsType, cloneURLStr := fs.Arg(0), fs.Arg(1)
	var extraPath string
	if fs.NArg() == 3 {
		extraPath = fs.Arg(2)
	}

	baseURL, err := url.Parse(*urlStr)
	if err != nil {
		log.Fatal(err)
	}

	cloneURL, err := url.Parse(cloneURLStr)
	if err != nil {
		log.Fatal(err)
	}

	router := vcsclient.NewRouter(nil)
	url := router.URLToRepo(vcsType, cloneURL)
	url.Path = strings.TrimPrefix(url.Path, "/")
	url = baseURL.ResolveReference(url)
	url.Path = filepath.Join(url.Path, extraPath)

	if *datadClient {
		datadGet(*method, vcsType, cloneURL, url)
	} else {
		normalGet(*method, nil, url)
	}
}

func datadGet(method string, vcsType string, cloneURL *url.URL, reqURL *url.URL) {
	cc := cluster.NewClient(datad.NewClient(etcdBackend()), nil)
	t, err := cc.TransportForRepository(vcsType, cloneURL)
	if err != nil {
		log.Fatal(err)
	}

	reqURL.Host = "$(DATAD_NODE)"
	normalGet(method, &http.Client{Transport: t}, reqURL)
}

func normalGet(method string, c *http.Client, url *url.URL) {
	if c == nil {
		c = http.DefaultClient
	}

	if *verbose {
		log.Printf("%s %s", method, url)
	}

	req, err := http.NewRequest(method, url.String(), nil)
	if err != nil {
		log.Fatal(err)
	}
	resp, err := c.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	if !(resp.StatusCode >= 200 && resp.StatusCode <= 399) {
		log.Fatal("Error: HTTP %d: %s.", resp.StatusCode, body)
	}

	fmt.Println(string(body))
}
<|endoftext|>


<commit_before>// TODO:
// If authentication request, attempt authentication
// Else
// Reject non-authenticated requests
// If user-preferences request, send user preferences
// If user-preferences put, save user preferences
// If file-hash request and if user-authorized, send file-hash
// If file request and if user-authorized, send file
// If file put and if user-authorized, save file

package main

import (
	"fmt"
	jwt "github.com/dgrijalva/jwt-go"
	bcrypt "golang.org/x/crypto/bcrypt"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"io/ioutil"
	"log"
	"net/http"
	"time"
)

type ApiHandler struct{}
type User struct {
	UserName string
	Hash     []byte
}

const (
	privKeyPath = "keys/app.rsa"     // openssl genrsa -out app.rsa keysize
	pubKeyPath  = "keys/app.rsa.pub" // openssl rsa -in app.rsa -pubout > app.rsa.pub
)

var (
	verifyKey, signKey []byte
	userDB, fileDB     *mgo.Collection
)

// read the key files before starting http handlers
func init() {
	var err error

	signKey, err = ioutil.ReadFile(privKeyPath)
	if err != nil {
		log.Fatal("Error reading private key")
		return
	}

	verifyKey, err = ioutil.ReadFile(pubKeyPath)
	if err != nil {
		log.Fatal("Error reading private key")
		return
	}
}

// just some html, too lazy for http.FileServer()
const (
	tokenName = "AccessToken"

	landingHtml = `
Welcome to the JWT Test

fun area

<form action="/authenticate" method="POST">
  <input type="text" name="user">
  <input type="password" name="pass">
  <input type="submit">
</form>`

	createUserHTML = `
Welcome to the Test Sign up

fun area

<form action="/createUser" method="POST">
  <input type="text" name="user">
  <input type="password" name="pass">
  <input type="submit">
</form>`

	successHtml    = `<h2>Token Set - have fun!</h2><p>Go <a href="/">Back...</a></p>`
	restrictedHtml = `<h1>Welcome!!</h1><img src="https://httpcats.herokuapp.com/200" alt="" />`
)

// serves the form and restricted link
func landingHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/html")
	w.WriteHeader(http.StatusOK)
	fmt.Fprint(w, landingHtml)
}

// serves user creation page
func createHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		w.Header().Set("Content-Type", "text/html")
		w.WriteHeader(http.StatusOK)
		fmt.Fprint(w, createUserHTML)
		return
	}

	username := r.FormValue("user")
	pass := r.FormValue("pass")
	user := User{}
	err := userDB.Find(bson.M{"username": username}).One(&user)

	if user.UserName == username {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintln(w, "User already exists")
		return
	}

	if len(pass) < 8 {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintln(w, "Password must contain at least 8 characters")
		return
	}

	hash, _ := bcrypt.GenerateFromPassword([]byte(pass), 10)
	user.UserName = username
	user.Hash = hash
	err = userDB.Insert(&user)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintln(w, "Error creating user.")
		return
	}

	loginHandler(w, r)
}

// reads the form values, checks them and creates the token
func loginHandler(w http.ResponseWriter, r *http.Request) {
	// make sure its post
	if r.Method != "POST" {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintln(w, "No POST", r.Method)
		return
	}

	username := r.FormValue("user")
	pass := r.FormValue("pass")

	log.Printf("Authenticate: user[%s] pass[%s]\n", username, pass)

	user := User{}
	err := userDB.Find(bson.M{"username": username}).One(&user)

	err = bcrypt.CompareHashAndPassword(user.Hash, []byte(pass))

	// check values
	if err != nil {
		w.WriteHeader(http.StatusForbidden)
		fmt.Fprintln(w, "Wrong info")
		return
	}

	// create a signer for rsa 256
	t := jwt.New(jwt.GetSigningMethod("RS256"))

	// TODO include user id in cookie
	// t.Claims["AccessToken"] = "level1"
	// t.Claims["CustomUserInfo"] = struct {
	// 	Name string
	// 	Kind string
	// }{username, "human"}

	// set the expire time
	t.Claims["exp"] = time.Now().Add(time.Hour * 24 * 7).Unix()
	tokenString, err := t.SignedString(signKey)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintln(w, "Sorry, error while Signing Token!")
		log.Printf("Token Signing error: %v\n", err)
		return
	}

	http.SetCookie(w, &http.Cookie{
		Name:       tokenName,
		Value:      tokenString,
		Path:       "/",
		RawExpires: "0",
	})

	w.Header().Set("Content-Type", "text/html")
	w.WriteHeader(http.StatusOK)
	fmt.Fprintln(w, successHtml)
}

// only accessible with a valid token
func restrictedHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" {
		loginHandler(w, r)
		return
	}
	// check if we have a cookie with our tokenName
	tokenCookie, err := r.Cookie(tokenName)
	switch {
	case err == http.ErrNoCookie:
		w.WriteHeader(http.StatusUnauthorized)
		fmt.Fprintln(w, "No Token, no fun!")
		return
	case err != nil:
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintln(w, "Error while Parsing cookie!")
		log.Printf("Cookie parse error: %v\n", err)
		return
	}

	// just for the lulz, check if it is empty.. should fail on Parse anyway..
	if tokenCookie.Value == "" {
		w.WriteHeader(http.StatusUnauthorized)
		fmt.Fprintln(w, "No Token, no fun!")
		return
	}

	// validate the token
	token, err := jwt.Parse(tokenCookie.Value, func(token *jwt.Token) (interface{}, error) {
		return verifyKey, nil
	})

	// branch out into the possible error from signing
	switch err.(type) {

	case nil: // no error

		if !token.Valid { // but may still be invalid
			w.WriteHeader(http.StatusUnauthorized)
			fmt.Fprintln(w, "Invalid Token.")
			return
		}

		// see stdout and watch for the CustomUserInfo, nicely unmarshalled
		log.Printf("Someone accessed resricted area! Token:%+v\n", token)
		w.Header().Set("Content-Type", "text/html")
		w.WriteHeader(http.StatusOK)
		fmt.Fprintln(w, restrictedHtml)

	case *jwt.ValidationError: // something was wrong during the validation
		vErr := err.(*jwt.ValidationError)

		switch vErr.Errors {
		case jwt.ValidationErrorExpired:
			landingHandler(w, r)
			return

		default:
			w.WriteHeader(http.StatusInternalServerError)
			fmt.Fprintln(w, "Error while Parsing Token!")
			log.Printf("ValidationError error: %+v\n", vErr.Errors)
			return
		}

	default: // something else went wrong
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintln(w, "Error while Parsing Token!")
		log.Printf("Token parse error: %v\n", err)
		return
	}

}

func (s ApiHandler) ServeHTTP(
	w http.ResponseWriter,
	r *http.Request) {
}

func main() {
	// fs := http.FileServer(http.Dir("client"))
	// http.Handle("/", fs)
	// go http.ListenAndServeTLS(":8443", "certFile", "keyFile", &ApiHandler{})
	// log.Println("Listening...")
	// http.ListenAndServe(":3000", nil)

	http.HandleFunc("/createUser", createHandler)
	http.HandleFunc("/login", loginHandler)
	http.HandleFunc("/", restrictedHandler)

	session, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}
	defer session.Close()
	session.SetSafe(&mgo.Safe{})

	userDB = session.DB("Theseus").C("users")
	fileDB = session.DB("Theseus").C("files")

	log.Println("Listening on port 8080...")
	http.ListenAndServe(":8080", nil)
}
<commit_msg>cleanup up code a bit<commit_after>// TODO:
// If authentication request, attempt authentication
// Else
// Reject non-authenticated requests
// If user-preferences request, send user preferences
// If user-preferences put, save user preferences
// If file-hash request and if user-authorized, send file-hash
// If file request and if user-authorized, send file
// If file put and if user-authorized, save file

package main

import (
	"fmt"
	jwt "github.com/dgrijalva/jwt-go"
	bcrypt "golang.org/x/crypto/bcrypt"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"io/ioutil"
	"log"
	"net/http"
	"time"
)

type ApiHandler struct{}
type User struct {
	UserName string
	Hash     []byte
}

const (
	privKeyPath = "keys/app.rsa"     // openssl genrsa -out app.rsa keysize
	pubKeyPath  = "keys/app.rsa.pub" // openssl rsa -in app.rsa -pubout > app.rsa.pub
)

var (
	verifyKey, signKey []byte
	userDB, fileDB     *mgo.Collection
)

// read the key files before starting http handlers
func init() {
	var err error

	signKey, err = ioutil.ReadFile(privKeyPath)
	if err != nil {
		log.Fatal("Error reading private key")
		return
	}

	verifyKey, err = ioutil.ReadFile(pubKeyPath)
	if err != nil {
		log.Fatal("Error reading private key")
		return
	}
}

// just some html, too lazy for http.FileServer()
const (
	tokenName = "AccessToken"

	landingHtml = `
Welcome to the JWT Test

fun area

<form action="/authenticate" method="POST">
  <input type="text" name="user">
  <input type="password" name="pass">
  <input type="submit">
</form>`

	createUserHTML = `
Welcome to the Test Sign up

fun area

<form action="/createUser" method="POST">
  <input type="text" name="user">
  <input type="password" name="pass">
  <input type="submit">
</form>`

	successHtml    = `<h2>Token Set - have fun!</h2><p>Go <a href="/">Back...</a></p>`
	restrictedHtml = `<h1>Welcome!!</h1><img src="https://httpcats.herokuapp.com/200" alt="" />`
)

// serves the form and restricted link
func landingHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/html")
	w.WriteHeader(http.StatusOK)
	fmt.Fprint(w, landingHtml)
}

// serves user creation page
func createHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		w.Header().Set("Content-Type", "text/html")
		w.WriteHeader(http.StatusOK)
		fmt.Fprint(w, createUserHTML)
		return
	}

	username := r.FormValue("user")
	pass := r.FormValue("pass")
	user := User{}
	err := userDB.Find(bson.M{"username": username}).One(&user)

	if user.UserName == username {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintln(w, "User already exists")
		return
	}

	if len(pass) < 8 {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintln(w, "Password must contain at least 8 characters")
		return
	}

	hash, _ := bcrypt.GenerateFromPassword([]byte(pass), 10)
	user.UserName = username
	user.Hash = hash
	err = userDB.Insert(&user)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintln(w, "Error creating user.")
		return
	}

	loginHandler(w, r)
}

// reads the form values, checks them and creates the token
func loginHandler(w http.ResponseWriter, r *http.Request) {
	// make sure its post
	if r.Method != "POST" {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintln(w, "No POST", r.Method)
		return
	}

	username := r.FormValue("user")
	pass := r.FormValue("pass")

	log.Printf("Authenticate: user[%s] pass[%s]\n", username, pass)

	user := User{}
	err := userDB.Find(bson.M{"username": username}).One(&user)

	err = bcrypt.CompareHashAndPassword(user.Hash, []byte(pass))

	// check values
	if err != nil {
		w.WriteHeader(http.StatusForbidden)
		fmt.Fprintln(w, "Wrong info")
		return
	}

	// create a signer for rsa 256
	t := jwt.New(jwt.GetSigningMethod("RS256"))

	// include username in cookie
	t.Claims["username"] = username

	// set the expire time
	t.Claims["exp"] = time.Now().Add(time.Hour * 24 * 7).Unix()
	tokenString, err := t.SignedString(signKey)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintln(w, "Sorry, error while Signing Token!")
		log.Printf("Token Signing error: %v\n", err)
		return
	}

	http.SetCookie(w, &http.Cookie{
		Name:       tokenName,
		Value:      tokenString,
		Path:       "/",
		RawExpires: "0",
	})

	w.Header().Set("Content-Type", "text/html")
	w.WriteHeader(http.StatusOK)
	fmt.Fprintln(w, successHtml)
}

// only accessible with a valid token
func restrictedHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" {
		loginHandler(w, r)
		return
	}
	// check if we have a cookie with our tokenName
	tokenCookie, err := r.Cookie(tokenName)
	switch {
	case err == http.ErrNoCookie:
		w.WriteHeader(http.StatusUnauthorized)
		fmt.Fprintln(w, "No Token, no fun!")
		return
	case err != nil:
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintln(w, "Error while Parsing cookie!")
		log.Printf("Cookie parse error: %v\n", err)
		return
	}

	// just for the lulz, check if it is empty.. should fail on Parse anyway..
	if tokenCookie.Value == "" {
		w.WriteHeader(http.StatusUnauthorized)
		fmt.Fprintln(w, "No Token, no fun!")
		return
	}

	// validate the token
	token, err := jwt.Parse(tokenCookie.Value, func(token *jwt.Token) (interface{}, error) {
		return verifyKey, nil
	})

	// branch out into the possible error from signing
	switch err.(type) {

	case nil: // no error

		if !token.Valid { // but may still be invalid
			w.WriteHeader(http.StatusUnauthorized)
			fmt.Fprintln(w, "Invalid Token.")
			return
		}

		// see stdout and watch for the CustomUserInfo, nicely unmarshalled
		log.Printf("Someone accessed resricted area! Token:%+v\n", token)
		w.Header().Set("Content-Type", "text/html")
		w.WriteHeader(http.StatusOK)
		fmt.Fprintln(w, restrictedHtml)

	case *jwt.ValidationError: // something was wrong during the validation
		vErr := err.(*jwt.ValidationError)

		switch vErr.Errors {
		case jwt.ValidationErrorExpired:
			landingHandler(w, r)
			return

		default:
			w.WriteHeader(http.StatusInternalServerError)
			fmt.Fprintln(w, "Error while Parsing Token!")
			log.Printf("ValidationError error: %+v\n", vErr.Errors)
			return
		}

	default: // something else went wrong
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintln(w, "Error while Parsing Token!")
		log.Printf("Token parse error: %v\n", err)
		return
	}

}

func (s ApiHandler) ServeHTTP(
	w http.ResponseWriter,
	r *http.Request) {
}

func main() {
	// fs := http.FileServer(http.Dir("client"))
	// http.Handle("/", fs)
	// go http.ListenAndServeTLS(":8443", "certFile", "keyFile", &ApiHandler{})
	// log.Println("Listening...")
	// http.ListenAndServe(":3000", nil)

	http.HandleFunc("/createUser", createHandler)
	http.HandleFunc("/login", loginHandler)
	http.HandleFunc("/", restrictedHandler)

	session, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}
	defer session.Close()
	session.SetSafe(&mgo.Safe{})

	userDB = session.DB("Theseus").C("users")
	fileDB = session.DB("Theseus").C("files")

	log.Println("Listening on port 8080...")
	http.ListenAndServe(":8080", nil)
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"os/exec"
	"path"
)

func usage() {
	fmt.Printf("Usage: %s [options...] <tree-ish> <path>\n\nOptions:\n", os.Args[0])
	flag.PrintDefaults()
}

func main() {

	flag.Usage = usage

	registryPtr := flag.String("publish", "", "publish the image to a docker registry")

	flag.Parse()

	if flag.NArg() != 2 {
		usage()
		os.Exit(2)
	}

	treeish := flag.Arg(0)
	buildpath := flag.Arg(1)
	registry := *registryPtr

	cwd, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}

	wd := path.Join(cwd, buildpath)

	name := path.Base(wd)
	if len(registry) > 0 {
		name = registry + "/" + name
	}

	nameTag := name + ":" + treeish

	fmt.Printf("Building tree '%s' in %s as '%s'...\n", treeish, wd, nameTag)

	gitArchive := exec.Command("git", "archive", treeish)

	gitArchive.Dir = wd

	gitArchiveOut, err := gitArchive.StdoutPipe()
	if err != nil {
		log.Fatal(err)
	}

	gitArchiveErr, err := gitArchive.StderrPipe()
	if err != nil {
		log.Fatal(err)
	}

	dockerBuild := exec.Command("docker", "build", "-t", nameTag, "-")

	dockerBuildOut, err := dockerBuild.StdoutPipe()
	if err != nil {
		log.Fatal(err)
	}

	dockerBuildErr, err := dockerBuild.StderrPipe()
	if err != nil {
		log.Fatal(err)
	}

	dockerBuildIn, err := dockerBuild.StdinPipe()
	if err != nil {
		log.Fatal(err)
	}

	err = gitArchive.Start()
	if err != nil {
		log.Fatal(err)
	}

	err = dockerBuild.Start()
	if err != nil {
		log.Fatal(err)
	}

	go func() {
		io.Copy(os.Stdout, dockerBuildOut)
	}()

	go func() {
		io.Copy(os.Stderr, dockerBuildErr)
	}()

	go func() {
		io.Copy(os.Stderr, gitArchiveErr)
	}()

	io.Copy(dockerBuildIn, gitArchiveOut)

	err = gitArchive.Wait()
	if err != nil {
		log.Fatal(err)
	}

	dockerBuildIn.Close()

	err = dockerBuild.Wait()
	if err != nil {
		log.Fatal(err)
	}

	if len(registry) == 0 {
		os.Exit(0)
	}

	fmt.Printf("Publishing to %s...\n", registry)

	dockerPush := exec.Command("docker", "push", nameTag)

	dockerPushOut, err := dockerPush.StdoutPipe()
	if err != nil {
		log.Fatal(err)
	}

	dockerPushErr, err := dockerPush.StderrPipe()
	if err != nil {
		log.Fatal(err)
	}

	go func() {
		io.Copy(os.Stdout, dockerPushOut)
	}()

	go func() {
		io.Copy(os.Stderr, dockerPushErr)
	}()

	dockerPush.Run()

}
<commit_msg>add tag option<commit_after>package main

import (
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"os/exec"
	"path"
)

func usage() {
	fmt.Printf("Usage: %s [options...] <tree-ish> <path>\n\nOptions:\n", os.Args[0])
	flag.PrintDefaults()
}

func main() {

	flag.Usage = usage

	registryPtr := flag.String("publish", "", "publish the image to a docker registry")
	tagPtr := flag.String("t", "", "specify a custom tag for the image")

	flag.Parse()

	if flag.NArg() != 2 {
		usage()
		os.Exit(2)
	}

	treeish := flag.Arg(0)
	buildpath := flag.Arg(1)
	registry := *registryPtr
	tag := *tagPtr

	cwd, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}

	wd := path.Join(cwd, buildpath)

	name := path.Base(wd)
	if len(registry) > 0 {
		name = registry + "/" + name
	}

	var nameTag string
	if tag == "" {
		nameTag = name + ":" + treeish
	} else {
		nameTag = tag
	}

	fmt.Printf("Building tree '%s' in %s as '%s'...\n", treeish, wd, nameTag)

	gitArchive := exec.Command("git", "archive", treeish)

	gitArchive.Dir = wd

	gitArchiveOut, err := gitArchive.StdoutPipe()
	if err != nil {
		log.Fatal(err)
	}

	gitArchiveErr, err := gitArchive.StderrPipe()
	if err != nil {
		log.Fatal(err)
	}

	dockerBuild := exec.Command("docker", "build", "-t", nameTag, "-")

	dockerBuildOut, err := dockerBuild.StdoutPipe()
	if err != nil {
		log.Fatal(err)
	}

	dockerBuildErr, err := dockerBuild.StderrPipe()
	if err != nil {
		log.Fatal(err)
	}

	dockerBuildIn, err := dockerBuild.StdinPipe()
	if err != nil {
		log.Fatal(err)
	}

	err = gitArchive.Start()
	if err != nil {
		log.Fatal(err)
	}

	err = dockerBuild.Start()
	if err != nil {
		log.Fatal(err)
	}

	go func() {
		io.Copy(os.Stdout, dockerBuildOut)
	}()

	go func() {
		io.Copy(os.Stderr, dockerBuildErr)
	}()

	go func() {
		io.Copy(os.Stderr, gitArchiveErr)
	}()

	io.Copy(dockerBuildIn, gitArchiveOut)

	err = gitArchive.Wait()
	if err != nil {
		log.Fatal(err)
	}

	dockerBuildIn.Close()

	err = dockerBuild.Wait()
	if err != nil {
		log.Fatal(err)
	}

	if len(registry) == 0 {
		os.Exit(0)
	}

	fmt.Printf("Publishing to %s...\n", registry)

	dockerPush := exec.Command("docker", "push", nameTag)

	dockerPushOut, err := dockerPush.StdoutPipe()
	if err != nil {
		log.Fatal(err)
	}

	dockerPushErr, err := dockerPush.StderrPipe()
	if err != nil {
		log.Fatal(err)
	}

	go func() {
		io.Copy(os.Stdout, dockerPushOut)
	}()

	go func() {
		io.Copy(os.Stderr, dockerPushErr)
	}()

	dockerPush.Run()

}
<|endoftext|>


<commit_before>// Package matrix provides functions for simple linear algebra
package matrix

import (
	"bytes"
	"fmt"
	"math"
	"math/rand"
	"strconv"
	"strings"
)

var (
	ErrIncompatibleSizes    = &MatrixError{"Incompatible sizes of matricies"}
	ErrInsertionOutOfBounds = &MatrixError{"The matrix you are trying to insert to hasn't got that row or column."}
	ErrOutOfBounds          = &MatrixError{"The element you are trying to access is out of bounds."}
)

// Describes error during calculations
type MatrixError struct {
	ErrorString string
}

// Return the Error's string
func (err *MatrixError) Error() string { return err.ErrorString }

// Matrix construct that holds all the information about a matrix
type Matrix struct {
	rows, cols int
	values     []float64
}

// A function that will apply an abitary transformation to an element in the matrix
type ApplyFunc func(index int, value float64) float64

// Creates a new Matrix and initializes all values to 0
func Zeros(rows, cols int) *Matrix {
	A := new(Matrix)

	A.rows = rows
	A.cols = cols
	A.values = make([]float64, rows*cols)

	return A
}

// Creates a new Matrix and initializes all values to 1
func Ones(rows, cols int) *Matrix {
	A := Zeros(rows, cols)
	A.AddNum(1)

	return A
}

// Creates a new identity matrix
func Eye(size int) *Matrix {
	A := Zeros(size, size)

	for i := 1; i <= size; i++ {
		A.Set(i, i, 1)
	}

	return A
}

// Constructs a new matrix with random values in range [0;1)
func Rand(rows, cols int) *Matrix {
	A := Zeros(rows, cols)

	for i := range A.values {
		A.values[i] = rand.Float64()
	}

	return A
}

// Constructs a new Matrix from a Matlab style representation
//
//	A := matrix.FromMatlab("[1 2 3; 4 5 6]")
func FromMatlab(str string) *Matrix {
	rows := strings.Split(str, ";")

	for i, row := range rows {
		rows[i] = strings.Replace(row, ",", " ", -1)
	}

	nRows := len(rows)
	nColumns := len(strings.Fields(rows[0]))

	A := Zeros(nRows, nColumns)

	for i, row := range rows {
		row = strings.Trim(row, "[] ")
		strNums := strings.Fields(row)

		for j, num := range strNums {
			n, _ := strconv.ParseFloat(num, 64)
			A.Set(i+1, j+1, n)
		}
	}

	return A
}

// Return a Matlab representation of the matrix
func (A *Matrix) ToMatlab() string {
	buffer := new(bytes.Buffer)
	buffer.WriteString("[")

	for i, v := range A.values {
		buffer.WriteString(fmt.Sprintf("%f ", v))

		if (i+1)%A.cols == 0 {
			buffer.WriteString("; ")
		}
	}

	buffer.WriteString("]")

	return buffer.String()
}

// Gives the dimensions of the matrix
func (A *Matrix) Dim() (int, int) {
	return A.rows, A.cols
}

// Return the number of rows in the matrix
func (A *Matrix) Rows() int {
	return A.rows
}

// Return the number of columns in the matrix
func (A *Matrix) Columns() int {
	return A.cols
}

// Returns an array of all the values
func (A *Matrix) Values() []float64 {
	tmp := make([]float64, len(A.values))
	copy(tmp, A.values)
	return tmp
}

// Returns a string representation of the matrix
func (A *Matrix) String() string {
	buffer := new(bytes.Buffer)

	for i, elem := range A.values {
		buffer.WriteString(fmt.Sprintf("%.3f ", elem))

		if (i+1)%A.cols == 0 {
			buffer.WriteString("\n")
		}
	}

	return buffer.String()
}

// Returns an exact copy of the matrix
func (A *Matrix) Copy() *Matrix {
	B := Zeros(A.rows, A.cols)
	copy(B.values, A.values)

	return B
}

// Insert the given rows into the matrix, returning a new matrix.
// Passing 0 as the second argument is like making the
// passed rows the first few, whereas passing Rows() is like appending
// the additional rows to the matrix.
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeInsertRows
func (A *Matrix) InsertRows(rows *Matrix, afterRow int) *Matrix {
	B := Zeros(A.rows+rows.rows, A.cols)

	// copy rows before inserted rows
	copy(B.values[0:afterRow*B.cols], A.values[0:afterRow*B.cols])
	// insert new rows
	copy(B.values[afterRow*B.cols:afterRow*B.cols+rows.rows*B.cols], rows.values)
	// copy rows after inserted rows
	copy(B.values[afterRow*B.cols+rows.rows*B.cols:], A.values[afterRow*B.cols:])

	return B
}

// Remove a single row from the matrix.
// The indexing for the rows start with 1 and go upto A.Rows()
func (A *Matrix) RemoveRow(row int) *Matrix {
	B := Zeros(A.rows-1, A.cols)
	copy(B.values, append(append([]float64{}, A.values[:(row-1)*A.cols]...), A.values[(row-1)*A.cols+A.cols:]...))
	return B
}

// Insert the given columns into the matrix, returning a new matrix.
// Passing 0 as the second argument is like making the
// passed columns the first few (on the left), whereas passing Columns() is like appending
// the additional columns to the matrix (on the right).
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeInsertColumns
func (A *Matrix) InsertColumns(cols *Matrix, afterCol int) *Matrix {
	B := Zeros(A.rows, A.cols+cols.cols)

	for i := 0; i < A.rows; i++ {
		copy(B.values[i*B.cols:i*B.cols+afterCol], A.values[i*A.cols:i*A.cols+afterCol])
		copy(B.values[i*B.cols+afterCol:i*B.cols+afterCol+cols.cols], cols.values[i*cols.cols:(i+1)*cols.cols])
		copy(B.values[i*B.cols+afterCol+cols.cols:(i+1)*B.cols], A.values[i*A.cols+afterCol:(i+1)*A.cols])
	}

	return B
}

// Give a function, apply its transformation to every element in the matrix.
//
//	A := matrix.Rand(4, 8)
//	A.Apply(func(index int, value float64) float64 {
//		return sigmoid(value)
//	})
func (A *Matrix) Apply(f ApplyFunc) *Matrix {
	for i, v := range A.values {
		A.values[i] = f(i, v)
	}

	return A
}

// Get the element at [row, col].
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeGet
func (A *Matrix) Get(row, col int) float64 {
	return A.values[(row-1)*A.cols+col-1]
}

// Set the element at [row, col] to val.
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeSet
func (A *Matrix) Set(row, col int, val float64) {

	A.values[(row-1)*A.cols+col-1] = val

}

// Transpose the matrix
func (A *Matrix) Transpose() *Matrix {
	B := Zeros(A.cols, A.rows)

	for i := 1; i <= A.rows; i++ {
		for j := 1; j <= A.cols; j++ {
			B.values[(j-1)*B.cols+i-1] = A.values[(i-1)*A.cols+j-1]
		}
	}

	return B
}

// Add B to the matrix A, produces new matrix
func (A *Matrix) Add(B *Matrix) (*Matrix, error) {
	if !sameSize(A, B) {
		return nil, ErrIncompatibleSizes
	}

	C := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		C.values[i] = val + B.values[i]
	}

	return C, nil
}

// Subtract B from the matrix A, produces a new matrix
func (A *Matrix) Sub(B *Matrix) (*Matrix, error) {
	if !sameSize(A, B) {
		return nil, ErrIncompatibleSizes
	}

	C := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		C.values[i] = val - B.values[i]
	}

	return C, nil
}

// Multiplies 2 matricies with each other.
// Returns a new matrix.
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeMul
func (A *Matrix) Mul(B *Matrix) *Matrix {

	C := Zeros(A.rows, B.cols)

	for i := 0; i < C.rows; i++ {
		for j := 0; j < C.cols; j++ {
			sum := float64(0)

			for k := 0; k < A.cols; k++ {
				sum += A.values[i*A.cols+k] * B.values[k*B.cols+j]
			}

			C.values[i*C.cols+j] = sum
		}
	}

	return C
}

func (A *Matrix) Dot(B *Matrix) float64 {
	sum := 0.0
	for i, v := range A.values {
		sum += v * B.values[i]
	}
}

// Element-wise multiplication of the matricies
// Returns a new matrix.
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeEWProd
func (A *Matrix) EWProd(B *Matrix) *Matrix {
	C := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		C.values[i] = val * B.values[i]
	}

	return C
}

// Scale the matrix by the factor f
func (A *Matrix) Scale(f float64) *Matrix {
	B := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		B.values[i] = val * f
	}

	return B
}

// Take every element of the matrix to the power of n
func (A *Matrix) Power(n float64) *Matrix {
	B := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		B.values[i] = math.Pow(val, n)
	}

	return B
}

// Add n to all elements in the matrix (in-place)
func (A *Matrix) AddNum(n float64) *Matrix {
	for i := range A.values {
		A.values[i] += n
	}

	return A
}
<commit_msg>just forgot the return<commit_after>// Package matrix provides functions for simple linear algebra
package matrix

import (
	"bytes"
	"fmt"
	"math"
	"math/rand"
	"strconv"
	"strings"
)

var (
	ErrIncompatibleSizes    = &MatrixError{"Incompatible sizes of matricies"}
	ErrInsertionOutOfBounds = &MatrixError{"The matrix you are trying to insert to hasn't got that row or column."}
	ErrOutOfBounds          = &MatrixError{"The element you are trying to access is out of bounds."}
)

// Describes error during calculations
type MatrixError struct {
	ErrorString string
}

// Return the Error's string
func (err *MatrixError) Error() string { return err.ErrorString }

// Matrix construct that holds all the information about a matrix
type Matrix struct {
	rows, cols int
	values     []float64
}

// A function that will apply an abitary transformation to an element in the matrix
type ApplyFunc func(index int, value float64) float64

// Creates a new Matrix and initializes all values to 0
func Zeros(rows, cols int) *Matrix {
	A := new(Matrix)

	A.rows = rows
	A.cols = cols
	A.values = make([]float64, rows*cols)

	return A
}

// Creates a new Matrix and initializes all values to 1
func Ones(rows, cols int) *Matrix {
	A := Zeros(rows, cols)
	A.AddNum(1)

	return A
}

// Creates a new identity matrix
func Eye(size int) *Matrix {
	A := Zeros(size, size)

	for i := 1; i <= size; i++ {
		A.Set(i, i, 1)
	}

	return A
}

// Constructs a new matrix with random values in range [0;1)
func Rand(rows, cols int) *Matrix {
	A := Zeros(rows, cols)

	for i := range A.values {
		A.values[i] = rand.Float64()
	}

	return A
}

// Constructs a new Matrix from a Matlab style representation
//
//	A := matrix.FromMatlab("[1 2 3; 4 5 6]")
func FromMatlab(str string) *Matrix {
	rows := strings.Split(str, ";")

	for i, row := range rows {
		rows[i] = strings.Replace(row, ",", " ", -1)
	}

	nRows := len(rows)
	nColumns := len(strings.Fields(rows[0]))

	A := Zeros(nRows, nColumns)

	for i, row := range rows {
		row = strings.Trim(row, "[] ")
		strNums := strings.Fields(row)

		for j, num := range strNums {
			n, _ := strconv.ParseFloat(num, 64)
			A.Set(i+1, j+1, n)
		}
	}

	return A
}

// Return a Matlab representation of the matrix
func (A *Matrix) ToMatlab() string {
	buffer := new(bytes.Buffer)
	buffer.WriteString("[")

	for i, v := range A.values {
		buffer.WriteString(fmt.Sprintf("%f ", v))

		if (i+1)%A.cols == 0 {
			buffer.WriteString("; ")
		}
	}

	buffer.WriteString("]")

	return buffer.String()
}

// Gives the dimensions of the matrix
func (A *Matrix) Dim() (int, int) {
	return A.rows, A.cols
}

// Return the number of rows in the matrix
func (A *Matrix) Rows() int {
	return A.rows
}

// Return the number of columns in the matrix
func (A *Matrix) Columns() int {
	return A.cols
}

// Returns an array of all the values
func (A *Matrix) Values() []float64 {
	tmp := make([]float64, len(A.values))
	copy(tmp, A.values)
	return tmp
}

// Returns a string representation of the matrix
func (A *Matrix) String() string {
	buffer := new(bytes.Buffer)

	for i, elem := range A.values {
		buffer.WriteString(fmt.Sprintf("%.3f ", elem))

		if (i+1)%A.cols == 0 {
			buffer.WriteString("\n")
		}
	}

	return buffer.String()
}

// Returns an exact copy of the matrix
func (A *Matrix) Copy() *Matrix {
	B := Zeros(A.rows, A.cols)
	copy(B.values, A.values)

	return B
}

// Insert the given rows into the matrix, returning a new matrix.
// Passing 0 as the second argument is like making the
// passed rows the first few, whereas passing Rows() is like appending
// the additional rows to the matrix.
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeInsertRows
func (A *Matrix) InsertRows(rows *Matrix, afterRow int) *Matrix {
	B := Zeros(A.rows+rows.rows, A.cols)

	// copy rows before inserted rows
	copy(B.values[0:afterRow*B.cols], A.values[0:afterRow*B.cols])
	// insert new rows
	copy(B.values[afterRow*B.cols:afterRow*B.cols+rows.rows*B.cols], rows.values)
	// copy rows after inserted rows
	copy(B.values[afterRow*B.cols+rows.rows*B.cols:], A.values[afterRow*B.cols:])

	return B
}

// Remove a single row from the matrix.
// The indexing for the rows start with 1 and go upto A.Rows()
func (A *Matrix) RemoveRow(row int) *Matrix {
	B := Zeros(A.rows-1, A.cols)
	copy(B.values, append(append([]float64{}, A.values[:(row-1)*A.cols]...), A.values[(row-1)*A.cols+A.cols:]...))
	return B
}

// Insert the given columns into the matrix, returning a new matrix.
// Passing 0 as the second argument is like making the
// passed columns the first few (on the left), whereas passing Columns() is like appending
// the additional columns to the matrix (on the right).
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeInsertColumns
func (A *Matrix) InsertColumns(cols *Matrix, afterCol int) *Matrix {
	B := Zeros(A.rows, A.cols+cols.cols)

	for i := 0; i < A.rows; i++ {
		copy(B.values[i*B.cols:i*B.cols+afterCol], A.values[i*A.cols:i*A.cols+afterCol])
		copy(B.values[i*B.cols+afterCol:i*B.cols+afterCol+cols.cols], cols.values[i*cols.cols:(i+1)*cols.cols])
		copy(B.values[i*B.cols+afterCol+cols.cols:(i+1)*B.cols], A.values[i*A.cols+afterCol:(i+1)*A.cols])
	}

	return B
}

// Give a function, apply its transformation to every element in the matrix.
//
//	A := matrix.Rand(4, 8)
//	A.Apply(func(index int, value float64) float64 {
//		return sigmoid(value)
//	})
func (A *Matrix) Apply(f ApplyFunc) *Matrix {
	for i, v := range A.values {
		A.values[i] = f(i, v)
	}

	return A
}

// Get the element at [row, col].
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeGet
func (A *Matrix) Get(row, col int) float64 {
	return A.values[(row-1)*A.cols+col-1]
}

// Set the element at [row, col] to val.
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeSet
func (A *Matrix) Set(row, col int, val float64) {

	A.values[(row-1)*A.cols+col-1] = val

}

// Transpose the matrix
func (A *Matrix) Transpose() *Matrix {
	B := Zeros(A.cols, A.rows)

	for i := 1; i <= A.rows; i++ {
		for j := 1; j <= A.cols; j++ {
			B.values[(j-1)*B.cols+i-1] = A.values[(i-1)*A.cols+j-1]
		}
	}

	return B
}

// Add B to the matrix A, produces new matrix
func (A *Matrix) Add(B *Matrix) (*Matrix, error) {
	if !sameSize(A, B) {
		return nil, ErrIncompatibleSizes
	}

	C := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		C.values[i] = val + B.values[i]
	}

	return C, nil
}

// Subtract B from the matrix A, produces a new matrix
func (A *Matrix) Sub(B *Matrix) (*Matrix, error) {
	if !sameSize(A, B) {
		return nil, ErrIncompatibleSizes
	}

	C := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		C.values[i] = val - B.values[i]
	}

	return C, nil
}

// Multiplies 2 matricies with each other.
// Returns a new matrix.
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeMul
func (A *Matrix) Mul(B *Matrix) *Matrix {

	C := Zeros(A.rows, B.cols)

	for i := 0; i < C.rows; i++ {
		for j := 0; j < C.cols; j++ {
			sum := float64(0)

			for k := 0; k < A.cols; k++ {
				sum += A.values[i*A.cols+k] * B.values[k*B.cols+j]
			}

			C.values[i*C.cols+j] = sum
		}
	}

	return C
}

func (A *Matrix) Dot(B *Matrix) float64 {
	sum := 0.0
	for i, v := range A.values {
		sum += v * B.values[i]
	}

	return sum
}

// Element-wise multiplication of the matricies
// Returns a new matrix.
//
// Warning: This is an unsafe method to use, it does no boundary
// checking what so ever. If you'd like a safe version
// use: SafeEWProd
func (A *Matrix) EWProd(B *Matrix) *Matrix {
	C := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		C.values[i] = val * B.values[i]
	}

	return C
}

// Scale the matrix by the factor f
func (A *Matrix) Scale(f float64) *Matrix {
	B := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		B.values[i] = val * f
	}

	return B
}

// Take every element of the matrix to the power of n
func (A *Matrix) Power(n float64) *Matrix {
	B := Zeros(A.rows, A.cols)

	for i, val := range A.values {
		B.values[i] = math.Pow(val, n)
	}

	return B
}

// Add n to all elements in the matrix (in-place)
func (A *Matrix) AddNum(n float64) *Matrix {
	for i := range A.values {
		A.values[i] += n
	}

	return A
}
<|endoftext|>


<commit_before>package main

import (
    "fmt"
    "net/http"
    "strconv"
    "time"
    "flag"
)

func handler(w http.ResponseWriter, r *http.Request) {
    id                    := r.FormValue("id")
    sleep_milliseconds, _ := strconv.Atoi( r.FormValue("t") )
    status, _             := strconv.Atoi( r.FormValue("s") )
    price                 := 0
    if r.FormValue("p") != "" {
        price, _ = strconv.Atoi( r.FormValue("p") )
    }

    if sleep_milliseconds > 0 {
        time.Sleep( time.Millisecond * time.Duration( sleep_milliseconds ) ) // milliseconds
    }

    w.Header().Set("Content-Type", "application/json")
    fmt.Fprintf(w, "{\"id\":\"%s\",\"status\":%d,\"price\":%d}", id, status, price)
}

func main() {
    port := flag.Int("port", 8080, "PORT")
    flag.Parse()

    http.HandleFunc("/ad", handler)
    http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
}
<commit_msg>revert<commit_after>package main

import (
    "fmt"
    "net/http"
    "strconv"
    "time"
    "flag"
)

func handler(w http.ResponseWriter, r *http.Request) {
    params                 := r.URL.Query()
    id                     := params["id"][0]
    sleep_milliseconds , _ := strconv.Atoi( params["t"][0] )
    status, _              := strconv.Atoi( params["s"][0] )
    price                  := 0
    if params["p"] != nil {
        price, _ = strconv.Atoi( params["p"][0] )
    }

    if sleep_milliseconds > 0 {
        time.Sleep( time.Millisecond * time.Duration( sleep_milliseconds ) ) // milliseconds
    }

    w.Header().Set("Content-Type", "application/json")
    fmt.Fprintf(w, "{\"id\":\"%s\",\"status\":%d,\"price\":%d}", id, status, price)
}

func main() {
    port := flag.Int("port", 8080, "PORT")
    flag.Parse()

    http.HandleFunc("/ad", handler)
    http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
}
<|endoftext|>


<commit_before>package cli

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"time"
)

// App is the main structure of a cli application. It is recomended that
// an app be created with the cli.NewApp() function
type App struct {
	// The name of the program. Defaults to os.Args[0]
	Name string
	// Description of the program.
	Usage string
	// Version of the program
	Version string
	// List of commands to execute
	Commands []Command
	// List of flags to parse
	Flags []Flag
	// Boolean to enable bash completion commands
	EnableBashCompletion bool
	// Boolean to hide built-in help command
	HideHelp bool
	// Boolean to hide built-in version flag
	HideVersion bool
	// An action to execute when the bash-completion flag is set
	BashComplete func(context *Context)
	// An action to execute before any subcommands are run, but after the context is ready
	// If a non-nil error is returned, no subcommands are run
	Before func(context *Context) error
	// An action to execute after any subcommands are run, but after the subcommand has finished
	// It is run even if Action() panics
	After func(context *Context) error
	// The action to execute when no subcommands are specified
	Action func(context *Context)
	// Execute this function if the proper command cannot be found
	CommandNotFound func(context *Context, command string)
	// Compilation date
	Compiled time.Time
	// List of all authors who contributed
	Authors []Author
	// Copyright of the binary if any
	Copyright string
	// Name of Author (Note: Use App.Authors, this is deprecated)
	Author string
	// Email of Author (Note: Use App.Authors, this is deprecated)
	Email string
	// Writer writer to write output to
	Writer io.Writer
}

// Tries to find out when this binary was compiled.
// Returns the current time if it fails to find it.
func compileTime() time.Time {
	info, err := os.Stat(os.Args[0])
	if err != nil {
		return time.Now()
	}
	return info.ModTime()
}

// Creates a new cli Application with some reasonable defaults for Name, Usage, Version and Action.
func NewApp() *App {
	return &App{
		Name:         os.Args[0],
		Usage:        "A new cli application",
		Version:      "0.0.0",
		BashComplete: DefaultAppComplete,
		Action:       helpCommand.Action,
		Compiled:     compileTime(),
		Writer:       os.Stdout,
	}
}

// Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination
func (a *App) Run(arguments []string) (err error) {
	if a.Author != "" || a.Email != "" {
		a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email})
	}

	// append help to commands
	if a.Command(helpCommand.Name) == nil && !a.HideHelp {
		a.Commands = append(a.Commands, helpCommand)
		if (HelpFlag != BoolFlag{}) {
			a.appendFlag(HelpFlag)
		}
	}

	//append version/help flags
	if a.EnableBashCompletion {
		a.appendFlag(BashCompletionFlag)
	}

	if !a.HideVersion {
		a.appendFlag(VersionFlag)
	}

	// parse flags
	set := flagSet(a.Name, a.Flags)
	set.SetOutput(ioutil.Discard)
	err = set.Parse(arguments[1:])
	nerr := normalizeFlags(a.Flags, set)
	if nerr != nil {
		fmt.Fprintln(a.Writer, nerr)
		context := NewContext(a, set, nil)
		ShowAppHelp(context)
		return nerr
	}
	context := NewContext(a, set, nil)

	if err != nil {
		fmt.Fprintln(a.Writer, "Incorrect Usage.")
		fmt.Fprintln(a.Writer)
		ShowAppHelp(context)
		return err
	}

	if checkCompletions(context) {
		return nil
	}

	if checkHelp(context) {
		return nil
	}

	if checkVersion(context) {
		return nil
	}

	if a.After != nil {
		defer func() {
			afterErr := a.After(context)
			if afterErr != nil {
				if err != nil {
					err = NewMultiError(err, afterErr)
				} else {
					err = afterErr
				}
			}
		}()
	}

	if a.Before != nil {
		err := a.Before(context)
		if err != nil {
			return err
		}
	}

	args := context.Args()
	if args.Present() {
		name := args.First()
		c := a.Command(name)
		if c != nil {
			return c.Run(context)
		}
	}

	// Run default Action
	a.Action(context)
	return nil
}

// Another entry point to the cli app, takes care of passing arguments and error handling
func (a *App) RunAndExitOnError() {
	if err := a.Run(os.Args); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

// Invokes the subcommand given the context, parses ctx.Args() to generate command-specific flags
func (a *App) RunAsSubcommand(ctx *Context) (err error) {
	// append help to commands
	if len(a.Commands) > 0 {
		if a.Command(helpCommand.Name) == nil && !a.HideHelp {
			a.Commands = append(a.Commands, helpCommand)
			if (HelpFlag != BoolFlag{}) {
				a.appendFlag(HelpFlag)
			}
		}
	}

	// append flags
	if a.EnableBashCompletion {
		a.appendFlag(BashCompletionFlag)
	}

	// parse flags
	set := flagSet(a.Name, a.Flags)
	set.SetOutput(ioutil.Discard)
	err = set.Parse(ctx.Args().Tail())
	nerr := normalizeFlags(a.Flags, set)
	context := NewContext(a, set, ctx)

	if nerr != nil {
		fmt.Fprintln(a.Writer, nerr)
		fmt.Fprintln(a.Writer)
		if len(a.Commands) > 0 {
			ShowSubcommandHelp(context)
		} else {
			ShowCommandHelp(ctx, context.Args().First())
		}
		return nerr
	}

	if err != nil {
		fmt.Fprintln(a.Writer, "Incorrect Usage.")
		fmt.Fprintln(a.Writer)
		ShowSubcommandHelp(context)
		return err
	}

	if checkCompletions(context) {
		return nil
	}

	if len(a.Commands) > 0 {
		if checkSubcommandHelp(context) {
			return nil
		}
	} else {
		if checkCommandHelp(ctx, context.Args().First()) {
			return nil
		}
	}

	if a.After != nil {
		defer func() {
			afterErr := a.After(context)
			if afterErr != nil {
				if err != nil {
					err = NewMultiError(err, afterErr)
				} else {
					err = afterErr
				}
			}
		}()
	}

	if a.Before != nil {
		err := a.Before(context)
		if err != nil {
			return err
		}
	}

	args := context.Args()
	if args.Present() {
		name := args.First()
		c := a.Command(name)
		if c != nil {
			return c.Run(context)
		}
	}

	// Run default Action
	a.Action(context)

	return nil
}

// Returns the named command on App. Returns nil if the command does not exist
func (a *App) Command(name string) *Command {
	for _, c := range a.Commands {
		if c.HasName(name) {
			return &c
		}
	}

	return nil
}

func (a *App) hasFlag(flag Flag) bool {
	for _, f := range a.Flags {
		if flag == f {
			return true
		}
	}

	return false
}

func (a *App) appendFlag(flag Flag) {
	if !a.hasFlag(flag) {
		a.Flags = append(a.Flags, flag)
	}
}

// Author represents someone who has contributed to a cli project.
type Author struct {
	Name  string // The Authors name
	Email string // The Authors email
}

// String makes Author comply to the Stringer interface, to allow an easy print in the templating process
func (a Author) String() string {
	e := ""
	if a.Email != "" {
		e = "<" + a.Email + "> "
	}

	return fmt.Sprintf("%v %v", a.Name, e)
}
<commit_msg>Fix help flag handling for commands with subcommands.<commit_after>package cli

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"time"
)

// App is the main structure of a cli application. It is recomended that
// an app be created with the cli.NewApp() function
type App struct {
	// The name of the program. Defaults to os.Args[0]
	Name string
	// Description of the program.
	Usage string
	// Version of the program
	Version string
	// List of commands to execute
	Commands []Command
	// List of flags to parse
	Flags []Flag
	// Boolean to enable bash completion commands
	EnableBashCompletion bool
	// Boolean to hide built-in help command
	HideHelp bool
	// Boolean to hide built-in version flag
	HideVersion bool
	// An action to execute when the bash-completion flag is set
	BashComplete func(context *Context)
	// An action to execute before any subcommands are run, but after the context is ready
	// If a non-nil error is returned, no subcommands are run
	Before func(context *Context) error
	// An action to execute after any subcommands are run, but after the subcommand has finished
	// It is run even if Action() panics
	After func(context *Context) error
	// The action to execute when no subcommands are specified
	Action func(context *Context)
	// Execute this function if the proper command cannot be found
	CommandNotFound func(context *Context, command string)
	// Compilation date
	Compiled time.Time
	// List of all authors who contributed
	Authors []Author
	// Copyright of the binary if any
	Copyright string
	// Name of Author (Note: Use App.Authors, this is deprecated)
	Author string
	// Email of Author (Note: Use App.Authors, this is deprecated)
	Email string
	// Writer writer to write output to
	Writer io.Writer
}

// Tries to find out when this binary was compiled.
// Returns the current time if it fails to find it.
func compileTime() time.Time {
	info, err := os.Stat(os.Args[0])
	if err != nil {
		return time.Now()
	}
	return info.ModTime()
}

// Creates a new cli Application with some reasonable defaults for Name, Usage, Version and Action.
func NewApp() *App {
	return &App{
		Name:         os.Args[0],
		Usage:        "A new cli application",
		Version:      "0.0.0",
		BashComplete: DefaultAppComplete,
		Action:       helpCommand.Action,
		Compiled:     compileTime(),
		Writer:       os.Stdout,
	}
}

// Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination
func (a *App) Run(arguments []string) (err error) {
	if a.Author != "" || a.Email != "" {
		a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email})
	}

	// append help to commands
	if a.Command(helpCommand.Name) == nil && !a.HideHelp {
		a.Commands = append(a.Commands, helpCommand)
		if (HelpFlag != BoolFlag{}) {
			a.appendFlag(HelpFlag)
		}
	}

	//append version/help flags
	if a.EnableBashCompletion {
		a.appendFlag(BashCompletionFlag)
	}

	if !a.HideVersion {
		a.appendFlag(VersionFlag)
	}

	// parse flags
	set := flagSet(a.Name, a.Flags)
	set.SetOutput(ioutil.Discard)
	err = set.Parse(arguments[1:])
	nerr := normalizeFlags(a.Flags, set)
	if nerr != nil {
		fmt.Fprintln(a.Writer, nerr)
		context := NewContext(a, set, nil)
		ShowAppHelp(context)
		return nerr
	}
	context := NewContext(a, set, nil)

	if err != nil {
		fmt.Fprintln(a.Writer, "Incorrect Usage.")
		fmt.Fprintln(a.Writer)
		ShowAppHelp(context)
		return err
	}

	if checkCompletions(context) {
		return nil
	}

	if checkHelp(context) {
		return nil
	}

	if checkVersion(context) {
		return nil
	}

	if a.After != nil {
		defer func() {
			afterErr := a.After(context)
			if afterErr != nil {
				if err != nil {
					err = NewMultiError(err, afterErr)
				} else {
					err = afterErr
				}
			}
		}()
	}

	if a.Before != nil {
		err := a.Before(context)
		if err != nil {
			return err
		}
	}

	args := context.Args()
	if args.Present() {
		name := args.First()
		c := a.Command(name)
		if c != nil {
			return c.Run(context)
		}
	}

	// Run default Action
	a.Action(context)
	return nil
}

// Another entry point to the cli app, takes care of passing arguments and error handling
func (a *App) RunAndExitOnError() {
	if err := a.Run(os.Args); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

// Invokes the subcommand given the context, parses ctx.Args() to generate command-specific flags
func (a *App) RunAsSubcommand(ctx *Context) (err error) {
	// append help to commands
	if len(a.Commands) > 0 {
		if a.Command(helpCommand.Name) == nil && !a.HideHelp {
			a.Commands = append(a.Commands, helpCommand)
		}
	}

	if (!a.HideHelp && HelpFlag != BoolFlag{}) {
		a.appendFlag(HelpFlag)
	}

	// append flags
	if a.EnableBashCompletion {
		a.appendFlag(BashCompletionFlag)
	}

	// parse flags
	set := flagSet(a.Name, a.Flags)
	set.SetOutput(ioutil.Discard)
	err = set.Parse(ctx.Args().Tail())
	nerr := normalizeFlags(a.Flags, set)
	context := NewContext(a, set, ctx)

	if nerr != nil {
		fmt.Fprintln(a.Writer, nerr)
		fmt.Fprintln(a.Writer)
		if len(a.Commands) > 0 {
			ShowSubcommandHelp(context)
		} else {
			ShowCommandHelp(ctx, context.Args().First())
		}
		return nerr
	}

	if err != nil {
		fmt.Fprintln(a.Writer, "Incorrect Usage.")
		fmt.Fprintln(a.Writer)
		ShowSubcommandHelp(context)
		return err
	}

	if checkCompletions(context) {
		return nil
	}

	if checkHelp(context) {
		return nil
	}

	if len(a.Commands) > 0 {
		if checkSubcommandHelp(context) {
			return nil
		}
	} else {
		if checkCommandHelp(ctx, context.Args().First()) {
			return nil
		}
	}

	if a.After != nil {
		defer func() {
			afterErr := a.After(context)
			if afterErr != nil {
				if err != nil {
					err = NewMultiError(err, afterErr)
				} else {
					err = afterErr
				}
			}
		}()
	}

	if a.Before != nil {
		err := a.Before(context)
		if err != nil {
			return err
		}
	}

	args := context.Args()
	if args.Present() {
		name := args.First()
		c := a.Command(name)
		if c != nil {
			return c.Run(context)
		}
	}

	// Run default Action
	a.Action(context)

	return nil
}

// Returns the named command on App. Returns nil if the command does not exist
func (a *App) Command(name string) *Command {
	for _, c := range a.Commands {
		if c.HasName(name) {
			return &c
		}
	}

	return nil
}

func (a *App) hasFlag(flag Flag) bool {
	for _, f := range a.Flags {
		if flag == f {
			return true
		}
	}

	return false
}

func (a *App) appendFlag(flag Flag) {
	if !a.hasFlag(flag) {
		a.Flags = append(a.Flags, flag)
	}
}

// Author represents someone who has contributed to a cli project.
type Author struct {
	Name  string // The Authors name
	Email string // The Authors email
}

// String makes Author comply to the Stringer interface, to allow an easy print in the templating process
func (a Author) String() string {
	e := ""
	if a.Email != "" {
		e = "<" + a.Email + "> "
	}

	return fmt.Sprintf("%v %v", a.Name, e)
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"github.com/gorilla/mux"
	"github.com/jawher/mow.cli"
	"io"
	"log"
	"net/http"
	"os"
)

func main() {
	app := cli.App("content-preview", "A RESTful API for retrieving and transforming content preview data")
	appPort := app.StringOpt("app-port", "8084", "Default port for app")
	mapiAuth := app.StringOpt("mapi-auth", "default", "Basic auth for MAPI")
	mapiUri := app.StringOpt("mapi-uri", "http://methode-api-uk-p.svc.ft.com/eom-file/", "Host and path for MAPI")
	matHostHeader := app.StringOpt("mat-host-header", "methode-article-transformer", "Hostheader for MAT")
	matUri := app.StringOpt("mat-uri", "http://ftapp05951-lvpr-uk-int:8080/content-transform/", "Host and path for MAT")

	app.Action = func() {
		r := mux.NewRouter()
		handler := Handlers{*mapiAuth, *mapiUri, *matUri, *matHostHeader}
		r.HandleFunc("/content-preview/{uuid}", handler.contentPreviewHandler)
		r.HandleFunc("/build-info", handler.buildInfoHandler)
		r.HandleFunc("/ping", pingHandler)
		http.Handle("/", r)

		log.Fatal(http.ListenAndServe(":"+*appPort, nil))

	}
	app.Run(os.Args)

}

type Handlers struct {
	mapiAuth      string
	mapiUri       string
	matUri        string
	matHostHeader string
}

func pingHandler(w http.ResponseWriter, r *http.Request) {

}

func (h Handlers) buildInfoHandler(w http.ResponseWriter, r *http.Request) {

}

func (h Handlers) contentPreviewHandler(w http.ResponseWriter, r *http.Request) {
	log.Printf("received request")

	vars := mux.Vars(r)
	uuid := vars["uuid"]

	if uuid == "" {
		log.Fatal("missing UUID")
	}

	methode := h.mapiUri + uuid

	log.Printf("sending to MAPI at " + methode)

	client := &http.Client{}
	mapReq, err := http.NewRequest("GET", methode, nil)
	mapReq.Header.Set("Authorization", "Basic "+h.mapiAuth)
	mapiResp, err := client.Do(mapReq)

	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("mapi the status code %v", mapiResp.StatusCode)
	if mapiResp.StatusCode != 200 {
		if mapiResp.StatusCode == 404 {
			w.WriteHeader(http.StatusNotFound)
			return
		}
		//TODO break this down
		w.WriteHeader(http.StatusBadGateway)
		return
	}

	// order of writing a response
	//header
	//responseCode
	//body

	matUrl := h.matUri + uuid
	log.Printf("sending to MAT at " + matUrl)
	client2 := &http.Client{}
	matReq, err := http.NewRequest("POST", matUrl, mapiResp.Body)
	matReq.Header.Set("Host", h.matHostHeader)
	matReq.Header.Set("Content-Type", "application/json")
	matResp, err := client2.Do(matReq)

	if matResp.StatusCode != 200 {
		//TODO break this down
		fmt.Printf("---the status code %v", matResp.StatusCode)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	fmt.Printf("mat the status code %v", matResp.StatusCode)
	io.Copy(w, matResp.Body)
}
<commit_msg>fix logging up a bit<commit_after>package main

import (
	"github.com/gorilla/mux"
	"github.com/jawher/mow.cli"
	"io"
	"log"
	"net/http"
	"os"
)

func main() {
	app := cli.App("content-preview", "A RESTful API for retrieving and transforming content preview data")
	appPort := app.StringOpt("app-port", "8084", "Default port for app")
	mapiAuth := app.StringOpt("mapi-auth", "default", "Basic auth for MAPI")
	mapiUri := app.StringOpt("mapi-uri", "http://methode-api-uk-p.svc.ft.com/eom-file/", "Host and path for MAPI")
	matHostHeader := app.StringOpt("mat-host-header", "methode-article-transformer", "Hostheader for MAT")
	matUri := app.StringOpt("mat-uri", "http://ftapp05951-lvpr-uk-int:8080/content-transform/", "Host and path for MAT")

	app.Action = func() {
		r := mux.NewRouter()
		handler := Handlers{*mapiAuth, *mapiUri, *matUri, *matHostHeader}
		r.HandleFunc("/content-preview/{uuid}", handler.contentPreviewHandler)
		r.HandleFunc("/build-info", handler.buildInfoHandler)
		r.HandleFunc("/ping", pingHandler)
		http.Handle("/", r)

		log.Fatal(http.ListenAndServe(":"+*appPort, nil))

	}
	app.Run(os.Args)

}

type Handlers struct {
	mapiAuth      string
	mapiUri       string
	matUri        string
	matHostHeader string
}

func pingHandler(w http.ResponseWriter, r *http.Request) {

}

func (h Handlers) buildInfoHandler(w http.ResponseWriter, r *http.Request) {

}

func (h Handlers) contentPreviewHandler(w http.ResponseWriter, r *http.Request) {
	log.Printf("received request")

	vars := mux.Vars(r)
	uuid := vars["uuid"]

	if uuid == "" {
		log.Fatal("missing UUID")
	}

	methode := h.mapiUri + uuid

	log.Printf("sending to MAPI at %v\n" + methode)

	client := &http.Client{}
	mapReq, err := http.NewRequest("GET", methode, nil)
	mapReq.Header.Set("Authorization", "Basic "+h.mapiAuth)
	mapiResp, err := client.Do(mapReq)

	if err != nil {
		log.Fatal(err)
	}

	log.Printf("mapi the status code %v\n", mapiResp.StatusCode)
	if mapiResp.StatusCode != 200 {
		if mapiResp.StatusCode == 404 {
			w.WriteHeader(http.StatusNotFound)
			return
		}
		//TODO break this down
		w.WriteHeader(http.StatusBadGateway)
		return
	}

	// order of writing a response
	//header
	//responseCode
	//body

	matUrl := h.matUri + uuid
	log.Printf("sending to MAT at %v\n" + matUrl)
	client2 := &http.Client{}
	matReq, err := http.NewRequest("POST", matUrl, mapiResp.Body)
	matReq.Header.Set("Host", h.matHostHeader)
	matReq.Header.Set("Content-Type", "application/json")
	matResp, err := client2.Do(matReq)

	log.Printf("mat the status code %v\n", matResp.StatusCode)

	if matResp.StatusCode != 200 {
		//TODO break this down
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	io.Copy(w, matResp.Body)
}
<|endoftext|>


<commit_before>package hap

import (
	"testing"
    "github.com/stretchr/testify/assert"
    "encoding/binary"
    "encoding/hex"
    "github.com/codahale/chacha20"
)

func TestAddBytesMod8(t *testing.T) {
    b := []byte{}
    add := []byte{0xFF}

    assert.Equal(t, AddBytes(b, add, 8), []byte{0xFF, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0})
}

func TestAddBytesMod8FromUint64(t *testing.T) {
    b := []byte{}
    length := make([]byte, 8)
    binary.LittleEndian.PutUint64(length, uint64(1)) // [0x1 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0]

    assert.Equal(t, AddBytes(b, length, 8), []byte{0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0})
}

func TestChacha20(t *testing.T) {
    K, _ := hex.DecodeString("6a3bfd77d9efac53f8ef51712796bf7a37541f425a5dc5397c8a2c3c040d9301")
    message, _ := hex.DecodeString("8e685bd3237866e7a424b0f33df1a087a397a78e147042d2d17b159044d2ad1162dea13df2a119b61c90d62fc76335f49954557f2b07c463dca1664ca042599fca66068b16bc3e7e1896536ca2")

    c, err := chacha20.NewCipher(K, []byte("PS-Msg05"))
    assert.Nil(t, err)
    var out = make([]byte, len(message))
    c.XORKeyStream(out, message)

    c2, err := chacha20.NewCipher(K, []byte("PS-Msg05"))

    assert.Nil(t, err)
    var out2 = make([]byte, len(message))
    c2.XORKeyStream(out2, out)
    assert.Equal(t, out2, message)
}<commit_msg>Update documentation<commit_after>package hap

import (
	"testing"
    "github.com/stretchr/testify/assert"
    "encoding/binary"
    "encoding/hex"
    "github.com/codahale/chacha20"
)

func TestAddBytesMod8(t *testing.T) {
    b := []byte{}
    add := []byte{0xFF}

    assert.Equal(t, AddBytes(b, add, 8), []byte{0xFF, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0})
}

func TestAddBytesMod8FromUint64(t *testing.T) {
    b := []byte{}
    length := make([]byte, 8)
    binary.LittleEndian.PutUint64(length, uint64(1)) // [0x1 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0]

    assert.Equal(t, AddBytes(b, length, 8), []byte{0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0})
}

// Test how and if de-/encoding works with chacha20 - surprise it works
func TestChacha20(t *testing.T) {
    K, _ := hex.DecodeString("6a3bfd77d9efac53f8ef51712796bf7a37541f425a5dc5397c8a2c3c040d9301")
    message, _ := hex.DecodeString("8e685bd3237866e7a424b0f33df1a087a397a78e147042d2d17b159044d2ad1162dea13df2a119b61c90d62fc76335f49954557f2b07c463dca1664ca042599fca66068b16bc3e7e1896536ca2")

    c, err := chacha20.NewCipher(K, []byte("PS-Msg05"))
    assert.Nil(t, err)
    var out = make([]byte, len(message))
    c.XORKeyStream(out, message)

    c2, err := chacha20.NewCipher(K, []byte("PS-Msg05"))

    assert.Nil(t, err)
    var out2 = make([]byte, len(message))
    c2.XORKeyStream(out2, out)
    assert.Equal(t, out2, message)
}<|endoftext|>