<commit_before>package config

import (
	"flag"
	"fmt"
	"os"
	"os/user"
	"path"
	"reflect"
	"strings"
	"unsafe"

	"pault.ag/go/debian/control"
)

func LoadFlags(name string, data interface{}) (*flag.FlagSet, error) {
	if err := Load(name, data); err != nil {
		return nil, err
	}
	return Flag(data)
}

//
func Load(name string, data interface{}) error {
	localUser, err := user.Current()
	if err != nil {
		return err
	}
	rcPath := path.Join(localUser.HomeDir, fmt.Sprintf(".%src", name))
	fd, err := os.Open(rcPath)
	if err != nil {
		return err
	}
	defer fd.Close()
	err = control.Unmarshal(data, fd)
	return err
}

func flagPointer(incoming reflect.Value, data *flag.FlagSet) error {
	if incoming.Type().Kind() == reflect.Ptr {
		return flagPointer(incoming.Elem(), data)
	}

	for i := 0; i < incoming.NumField(); i++ {
		field := incoming.Field(i)
		fieldType := incoming.Type().Field(i)

		if it := fieldType.Tag.Get("flag"); it != "" {
			/* Register the flag */
			switch field.Type().Kind() {
			case reflect.Int:
				data.IntVar(
					(*int)(unsafe.Pointer(field.Addr().Pointer())),
					it,
					int(field.Int()),
					fieldType.Tag.Get("description"),
				)
				continue
			case reflect.String:
				data.StringVar(
					(*string)(unsafe.Pointer(field.Addr().Pointer())),
					it,
					field.String(),
					fieldType.Tag.Get("description"),
				)
				continue
			default:
				return fmt.Errorf("Unknown type: %s", field.Type().Kind())
			}
		}
	}
	return nil
}

func Flag(data interface{}) (*flag.FlagSet, error) {
	dataValue := reflect.ValueOf(data)
	name := strings.ToLower(dataValue.Elem().Type().Name())
	flagSet := flag.NewFlagSet(name, flag.ExitOnError)
	flagSet.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", name)
		flagSet.PrintDefaults()
	}

	err := flagPointer(dataValue, flagSet)
	if err != nil {
		return nil, err
	}
	return flagSet, nil
}
<commit_msg>ignore everything<commit_after>package config

import (
	"flag"
	"fmt"
	"os"
	"os/user"
	"path"
	"reflect"
	"strings"
	"unsafe"

	"pault.ag/go/debian/control"
)

func LoadFlags(name string, data interface{}) (*flag.FlagSet, error) {
	if err := Load(name, data); err != nil {
		return nil, err
	}
	return Flag(data)
}

//
func Load(name string, data interface{}) error {
	localUser, err := user.Current()
	if err != nil {
		return nil
	}
	rcPath := path.Join(localUser.HomeDir, fmt.Sprintf(".%src", name))
	fd, err := os.Open(rcPath)
	if err != nil {
		return nil
	}
	defer fd.Close()
	err = control.Unmarshal(data, fd)
	return err
}

func flagPointer(incoming reflect.Value, data *flag.FlagSet) error {
	if incoming.Type().Kind() == reflect.Ptr {
		return flagPointer(incoming.Elem(), data)
	}

	for i := 0; i < incoming.NumField(); i++ {
		field := incoming.Field(i)
		fieldType := incoming.Type().Field(i)

		if it := fieldType.Tag.Get("flag"); it != "" {
			/* Register the flag */
			switch field.Type().Kind() {
			case reflect.Int:
				data.IntVar(
					(*int)(unsafe.Pointer(field.Addr().Pointer())),
					it,
					int(field.Int()),
					fieldType.Tag.Get("description"),
				)
				continue
			case reflect.String:
				data.StringVar(
					(*string)(unsafe.Pointer(field.Addr().Pointer())),
					it,
					field.String(),
					fieldType.Tag.Get("description"),
				)
				continue
			default:
				return fmt.Errorf("Unknown type: %s", field.Type().Kind())
			}
		}
	}
	return nil
}

func Flag(data interface{}) (*flag.FlagSet, error) {
	dataValue := reflect.ValueOf(data)
	name := strings.ToLower(dataValue.Elem().Type().Name())
	flagSet := flag.NewFlagSet(name, flag.ExitOnError)
	flagSet.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", name)
		flagSet.PrintDefaults()
	}

	err := flagPointer(dataValue, flagSet)
	if err != nil {
		return nil, err
	}
	return flagSet, nil
}
<|endoftext|>


<commit_before>package goreq

import (
	"bytes"
	"encoding/json"
	"io"
	"io/ioutil"
	"net"
	"net/http"
	"strings"
	"time"
)

type Request struct {
	headers     []headerTuple
	Method      string
	Uri         string
	Body        interface{}
	Timeout     time.Duration
	ContentType string
	Accept      string
	Host        string
	UserAgent   string
}

type Response struct {
	StatusCode int
	Body       Body
	Header     http.Header
}

type headerTuple struct {
	name  string
	value string
}

type Body struct {
	io.ReadCloser
}

type Error struct {
	timeout bool
	Err     error
}

func (e *Error) Timeout() bool {
	return e.timeout
}

func (e *Error) Error() string {
	return e.Err.Error()
}

func (b *Body) FromJsonTo(o interface{}) error {
	if body, err := ioutil.ReadAll(b); err != nil {
		return err
	} else if err := json.Unmarshal(body, o); err != nil {
		return err
	}

	return nil
}

func (b *Body) ToString() (string, error) {
	body, err := ioutil.ReadAll(b)
	if err != nil {
		return "", err
	}
	return string(body), nil
}

func prepareRequestBody(b interface{}) (io.Reader, error) {
	var body io.Reader

	if sb, ok := b.(string); ok {
		// treat is as text
		body = strings.NewReader(sb)
	} else if rb, ok := b.(io.Reader); ok {
		// treat is as text
		body = rb
	} else if bb, ok := b.([]byte); ok {
		//treat as byte array
		body = bytes.NewReader(bb)
	} else {
		// try to jsonify it
		j, err := json.Marshal(b)
		if err == nil {
			body = bytes.NewReader(j)
		} else {
			return nil, err
		}
	}

	return body, nil
}

func newResponse(res *http.Response) *Response {
	return &Response{StatusCode: res.StatusCode, Header: res.Header, Body: Body{res.Body}}
}

var dialer = &net.Dialer{Timeout: 1000 * time.Millisecond}
var transport = &http.Transport{Dial: dialer.Dial}

func SetConnectTimeout(duration time.Duration) {
	dialer.Timeout = duration
}

func (r *Request) AddHeader(name string, value string) {
	if r.headers == nil {
		r.headers = []headerTuple{}
	}
	r.headers = append(r.headers, headerTuple{name: name, value: value})
}

func (r Request) Do() (*Response, error) {
	client := &http.Client{Transport: transport}
	b, e := prepareRequestBody(r.Body)

	if e != nil {
		// there was a problem marshaling the body
		return nil, &Error{Err: e}
	}

	req, er := http.NewRequest(r.Method, r.Uri, b)

	if er != nil {
		// we couldn't parse the URL.
		return nil, &Error{Err: er}
	}

	// add headers to the request
	req.Host = r.Host
	req.Header.Add("User-Agent", r.UserAgent)
	req.Header.Add("Content-Type", r.ContentType)
	req.Header.Add("Accept", r.Accept)
	if r.headers != nil {
		for _, header := range r.headers {
			req.Header.Add(header.name, header.value)
		}
	}

	timeout := false
	var timer *time.Timer
	if r.Timeout > 0 {
		timer = time.AfterFunc(r.Timeout, func() {
			transport.CancelRequest(req)
			timeout = true
		})
	}

	res, err := client.Do(req)
	if timer != nil {
		timer.Stop()
	}

	if err != nil {
		if op, ok := err.(*net.OpError); !timeout && ok {
			timeout = op.Timeout()
		}
		return nil, &Error{timeout: timeout, Err: err}
	}
	return newResponse(res), nil
}
<commit_msg>detect get or post method<commit_after>package goreq

import (
	"bytes"
	"encoding/json"
	"io"
	"io/ioutil"
	"net"
	"net/http"
	"strings"
	"time"
	// "fmt"
)

type Request struct {
	headers     []headerTuple
	Method      string
	Uri         string
	Body        interface{}
	Timeout     time.Duration
	ContentType string
	Accept      string
	Host        string
	UserAgent   string
}

type Response struct {
	StatusCode int
	Body       Body
	Header     http.Header
}

type headerTuple struct {
	name  string
	value string
}

type Body struct {
	io.ReadCloser
}

type Error struct {
	timeout bool
	Err     error
}

func (e *Error) Timeout() bool {
	return e.timeout
}

func (e *Error) Error() string {
	return e.Err.Error()
}

func (b *Body) FromJsonTo(o interface{}) error {
	if body, err := ioutil.ReadAll(b); err != nil {
		return err
	} else if err := json.Unmarshal(body, o); err != nil {
		return err
	}

	return nil
}

func (b *Body) ToString() (string, error) {
	body, err := ioutil.ReadAll(b)
	if err != nil {
		return "", err
	}
	return string(body), nil
}

// func paramParse(data interface{})(string, error) {
// 	var parse string
// 	fmt.Printf(data.limit)

// 	return parse, nil

// }

func prepareRequestBody(b interface{}) (io.Reader, error) {
	var body io.Reader

	if sb, ok := b.(string); ok {
		// treat is as text
		body = strings.NewReader(sb)
	} else if rb, ok := b.(io.Reader); ok {
		// treat is as text
		body = rb
	} else if bb, ok := b.([]byte); ok {
		//treat as byte array
		body = bytes.NewReader(bb)
	} else {
		// try to jsonify it
		j, err := json.Marshal(b)
		if err == nil {
			body = bytes.NewReader(j)
		} else {
			return nil, err
		}
	}

	return body, nil
}

func newResponse(res *http.Response) *Response {
	return &Response{StatusCode: res.StatusCode, Header: res.Header, Body: Body{res.Body}}
}

var dialer = &net.Dialer{Timeout: 1000 * time.Millisecond}
var transport = &http.Transport{Dial: dialer.Dial}

func SetConnectTimeout(duration time.Duration) {
	dialer.Timeout = duration
}

func (r *Request) AddHeader(name string, value string) {
	if r.headers == nil {
		r.headers = []headerTuple{}
	}
	r.headers = append(r.headers, headerTuple{name: name, value: value})
}

func (r Request) Do() (*Response, error) {
	var req *http.Request
	var er error

	client := &http.Client{Transport: transport}
	b, e := prepareRequestBody(r.Body)

	if e != nil {
		// there was a problem marshaling the body
		return nil, &Error{Err: e}
	}

	// param, e := paramParse(r.Data)

	if strings.EqualFold(r.Method, "GET") || strings.EqualFold(r.Method, "") {
		req, er = http.NewRequest(r.Method, r.Uri, nil)
	} else {
		req, er = http.NewRequest(r.Method, r.Uri, b)
	}

	if er != nil {
		// we couldn't parse the URL.
		return nil, &Error{Err: er}
	}

	// add headers to the request
	req.Host = r.Host
	req.Header.Add("User-Agent", r.UserAgent)
	req.Header.Add("Content-Type", r.ContentType)
	req.Header.Add("Accept", r.Accept)
	if r.headers != nil {
		for _, header := range r.headers {
			req.Header.Add(header.name, header.value)
		}
	}

	timeout := false
	var timer *time.Timer
	if r.Timeout > 0 {
		timer = time.AfterFunc(r.Timeout, func() {
			transport.CancelRequest(req)
			timeout = true
		})
	}

	res, err := client.Do(req)
	if timer != nil {
		timer.Stop()
	}

	if err != nil {
		if op, ok := err.(*net.OpError); !timeout && ok {
			timeout = op.Timeout()
		}
		return nil, &Error{timeout: timeout, Err: err}
	}
	return newResponse(res), nil
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"flag"
	"fmt"
	"io"
	"os"
	"os/user"
	"path"
	// "reflect"
	"strconv"
	"strings"
)

var (
	homeDir    string
	selfURL127 string // 127.0.0.1:listenAddr
	selfURLLH  string // localhost:listenAddr

	printVer bool
)

const (
	dotDir       = ".cow"
	blockedFname = "blocked"
	directFname  = "direct"
	rcFname      = "rc"

	version = "0.2"
)

var config struct {
	listenAddr string // server listen address
	socksAddr  string
	numProc    int    // max number of cores to use
	sshServer  string // ssh to the given server to start socks proxy

	dir         string // directory containing config file and blocked site list
	blockedFile string // contains blocked domains
	directFile  string // contains sites that can be directly accessed
	chouFile    string // chou feng, sites which will be temporary blocked
	rcFile      string
}

func printVersion() {
	fmt.Println("cow-proxy version", version)
}

func init() {
	u, err := user.Current()
	if err != nil {
		errl.Printf("Can't get user information %v", err)
		os.Exit(1)
	}
	homeDir = u.HomeDir

	flag.BoolVar(&printVer, "version", false, "print version")

	flag.StringVar(&config.listenAddr, "listen", "127.0.0.1:7777", "proxy server listen address")
	flag.StringVar(&config.socksAddr, "socks", "127.0.0.1:1080", "socks server address")
	flag.IntVar(&config.numProc, "core", 2, "number of cores to use")
	flag.StringVar(&config.sshServer, "ssh_server", "", "remote server which will ssh to and provide sock server")

	config.dir = path.Join(homeDir, dotDir)
	config.blockedFile = path.Join(config.dir, blockedFname)
	config.directFile = path.Join(config.dir, directFname)
	config.rcFile = path.Join(config.dir, rcFname)
}

// Tries to open a file, if file not exist, return both nil for os.File and
// err
func openFile(path string) (f *os.File, err error) {
	if f, err = os.Open(path); err != nil {
		if os.IsNotExist(err) {
			return nil, nil
		}
		errl.Println("Error opening file:", path, err)
		return nil, err
	}
	return
}

func parseConfig() {
	f, err := openFile(config.rcFile)
	if f == nil || err != nil {
		return
	}
	defer f.Close()

	fr := bufio.NewReader(f)

	var line string
	var n int
	for {
		n++
		line, err = ReadLine(fr)
		if err == io.EOF {
			return
		} else if err != nil {
			errl.Println("Error reading rc file:", err)
			errl.Println("Exit")
			os.Exit(1)
		}

		if line == "" {
			continue
		}

		line = strings.TrimSpace(line)
		// Ignore comment
		if line[0] == '#' {
			continue
		}

		v := strings.Split(line, "=")
		if len(v) != 2 {
			fmt.Println("Config error: syntax error on line", n)
			os.Exit(1)
		}
		key, val := strings.TrimSpace(v[0]), strings.TrimSpace(v[1])

		switch {
		case key == "listen":
			config.listenAddr = val
		case key == "core":
			config.numProc, err = strconv.Atoi(val)
			if err != nil {
				fmt.Printf("Config error: core number %d %v", n, err)
				os.Exit(1)
			}
		case key == "socks":
			_, port := splitHostPort(val)
			if port == "" {
				fmt.Println("socks server must have port specified")
				os.Exit(1)
			}
			config.socksAddr = val
		case key == "blocked":
			config.blockedFile = val
		case key == "ssh_server":
			config.sshServer = val
		default:
			fmt.Println("Config error: no such option", key)
			os.Exit(1)
		}
	}
}

func loadConfig() {
	parseConfig()

	blockedDs.loadDomainList(config.blockedFile)
	directDs.loadDomainList(config.directFile)

	_, port := splitHostPort(config.listenAddr)
	selfURL127 = "127.0.0.1:" + port
	selfURLLH = "localhost:" + port
}
<commit_msg>Bump version to 0.2.1<commit_after>package main

import (
	"bufio"
	"flag"
	"fmt"
	"io"
	"os"
	"os/user"
	"path"
	// "reflect"
	"strconv"
	"strings"
)

var (
	homeDir    string
	selfURL127 string // 127.0.0.1:listenAddr
	selfURLLH  string // localhost:listenAddr

	printVer bool
)

const (
	dotDir       = ".cow"
	blockedFname = "blocked"
	directFname  = "direct"
	rcFname      = "rc"

	version = "0.2.1"
)

var config struct {
	listenAddr string // server listen address
	socksAddr  string
	numProc    int    // max number of cores to use
	sshServer  string // ssh to the given server to start socks proxy

	dir         string // directory containing config file and blocked site list
	blockedFile string // contains blocked domains
	directFile  string // contains sites that can be directly accessed
	chouFile    string // chou feng, sites which will be temporary blocked
	rcFile      string
}

func printVersion() {
	fmt.Println("cow-proxy version", version)
}

func init() {
	u, err := user.Current()
	if err != nil {
		errl.Printf("Can't get user information %v", err)
		os.Exit(1)
	}
	homeDir = u.HomeDir

	flag.BoolVar(&printVer, "version", false, "print version")

	flag.StringVar(&config.listenAddr, "listen", "127.0.0.1:7777", "proxy server listen address")
	flag.StringVar(&config.socksAddr, "socks", "127.0.0.1:1080", "socks server address")
	flag.IntVar(&config.numProc, "core", 2, "number of cores to use")
	flag.StringVar(&config.sshServer, "ssh_server", "", "remote server which will ssh to and provide sock server")

	config.dir = path.Join(homeDir, dotDir)
	config.blockedFile = path.Join(config.dir, blockedFname)
	config.directFile = path.Join(config.dir, directFname)
	config.rcFile = path.Join(config.dir, rcFname)
}

// Tries to open a file, if file not exist, return both nil for os.File and
// err
func openFile(path string) (f *os.File, err error) {
	if f, err = os.Open(path); err != nil {
		if os.IsNotExist(err) {
			return nil, nil
		}
		errl.Println("Error opening file:", path, err)
		return nil, err
	}
	return
}

func parseConfig() {
	f, err := openFile(config.rcFile)
	if f == nil || err != nil {
		return
	}
	defer f.Close()

	fr := bufio.NewReader(f)

	var line string
	var n int
	for {
		n++
		line, err = ReadLine(fr)
		if err == io.EOF {
			return
		} else if err != nil {
			errl.Println("Error reading rc file:", err)
			errl.Println("Exit")
			os.Exit(1)
		}

		if line == "" {
			continue
		}

		line = strings.TrimSpace(line)
		// Ignore comment
		if line[0] == '#' {
			continue
		}

		v := strings.Split(line, "=")
		if len(v) != 2 {
			fmt.Println("Config error: syntax error on line", n)
			os.Exit(1)
		}
		key, val := strings.TrimSpace(v[0]), strings.TrimSpace(v[1])

		switch {
		case key == "listen":
			config.listenAddr = val
		case key == "core":
			config.numProc, err = strconv.Atoi(val)
			if err != nil {
				fmt.Printf("Config error: core number %d %v", n, err)
				os.Exit(1)
			}
		case key == "socks":
			_, port := splitHostPort(val)
			if port == "" {
				fmt.Println("socks server must have port specified")
				os.Exit(1)
			}
			config.socksAddr = val
		case key == "blocked":
			config.blockedFile = val
		case key == "ssh_server":
			config.sshServer = val
		default:
			fmt.Println("Config error: no such option", key)
			os.Exit(1)
		}
	}
}

func loadConfig() {
	parseConfig()

	blockedDs.loadDomainList(config.blockedFile)
	directDs.loadDomainList(config.directFile)

	_, port := splitHostPort(config.listenAddr)
	selfURL127 = "127.0.0.1:" + port
	selfURLLH = "localhost:" + port
}
<|endoftext|>


<commit_before>/**
 * Copyright 2015 @ S1N1 Team.
 * name : gourl.go
 * author : jarryliu
 * date : -- :
 * description :
 * history :
 */
package main

import (
	"bufio"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"strings"
)

var (
	ConfBasePath = "conf/"
)

func main() {
	var port int
	var dir string
	flag.IntVar(&port, "port", 8302, "port")
	flag.StringVar(&dir, "dir", "conf/", "config file")
	ConfBasePath = dir

	app := http.Server{
		Addr:    fmt.Sprintf(":%d", port),
		Handler: getHandler(),
	}

	log.Printf("[ Service] - running on port %s\n", app.Addr)
	if err := app.ListenAndServe(); err != nil {
		log.Fatalf(" Server aborted!reason:%s\n", err.Error())
	}
}

type Item struct {
	Host        string            `json:"host"`
	AllLocation string            `json:"to"`
	Map         map[string]string `json:"map"`
}

type ItemManager struct {
	items    map[string]*Item
	basePath string
}

func (i *ItemManager) checkExists(path string) {
	_, err := os.Stat(path)
	if os.IsNotExist(err) {
		i.initExample()
	}
}

func (i *ItemManager) initExample() {
	if err := os.MkdirAll(i.basePath, os.ModePerm); err != nil {
		panic(err)
	}
	var defaultItems []*Item = []*Item{
		&Item{
			Host:        "*.z3q.net",
			AllLocation: "http://www.z3q.net",
		},
		&Item{
			Host: "s.z3q.net",
			Map: map[string]string{
				"blog": "http://www.s1n1.com",
			},
		},
	}
	bytes, _ := json.MarshalIndent(defaultItems, " ", " ")

	f, _ := os.Create(i.basePath + "gourl.conf")
	defer f.Close()
	wr := bufio.NewWriter(f)
	wr.Write(bytes)
	wr.Flush()
}

func (this *ItemManager) Load() {
	this.checkExists(this.basePath)
	filepath.Walk(this.basePath, func(path string, info os.FileInfo, err error) error {
		if err == nil && !info.IsDir() && strings.HasSuffix(path, ".conf") {
			items := this.GetItemsFromFile(path)
			this.Append(items)
		}
		return nil
	})
}

func (this *ItemManager) GetItemsFromFile(path string) []*Item {
	bytes, err := ioutil.ReadFile(path)
	if err != nil {
		return nil
	}
	var items []*Item = make([]*Item, 0)
	err = json.Unmarshal(bytes, &items)
	if err != nil {
		panic(path + " - " + err.Error())
	}
	return items
}

func (this *ItemManager) Append(items []*Item) {
	if this.items == nil {
		this.items = make(map[string]*Item, 0)
	}
	if items != nil {
		for _, v := range items {
			if _, ok := this.items[v.Host]; ok {
				panic("has exists host " + v.Host)
			}
			this.items[v.Host] = v
		}
	}
}

func (this *ItemManager) GetItemByHost(host string) *Item {
	for k, v := range this.items {
		if this.matchHost(k, host) {
			return v
		}
	}
	return nil
}

func (this *ItemManager) matchHost(key, host string) bool {
	if host == key {
		return true
	}
	if strings.HasPrefix(key, "*.") {
		return strings.HasSuffix(host, key[2:])
	}
	return false
}

var _ http.Handler = new(redirectHandler)

type redirectHandler struct {
	itemManager *ItemManager
}

func (r *redirectHandler) ServeHTTP(rsp http.ResponseWriter, req *http.Request) {
	host := req.Host // "s.z3q.net" use for test
	var item *Item = r.itemManager.GetItemByHost(host)
	if item != nil {
		if location, b := r.getLocation(rsp, req, item); b {
			rsp.Header().Add("Location", location)
			rsp.WriteHeader(302)
			return
		}
	}
	rsp.Write([]byte("Not match any host"))
}

func (r *redirectHandler) getLocation(rsp http.ResponseWriter, req *http.Request, item *Item) (string, bool) {
	path := req.URL.Path
	query := req.URL.RawQuery
	var con string
	if len(query) != 0 {
		con = "?"
	}
	if len(item.AllLocation) != 0 {
		return fmt.Sprintf("%s%s%s%s", item.AllLocation, path, con, query), true
	}
	if v, ok := item.Map[path[1:]]; ok {
		return fmt.Sprintf("%s%s%s", v, con, query), true
	}
	return "", false
}

func getHandler() http.Handler {
	re := &redirectHandler{
		itemManager: &ItemManager{
			basePath: ConfBasePath,
		},
	}
	re.itemManager.Load()
	return re
}
<commit_msg>code header<commit_after>/**
 * Copyright 2015 @ z3q.net.
 * name : gourl.go
 * author : jarryliu
 * date : -- :
 * description :
 * history :
 */
package main

import (
	"bufio"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"strings"
)

var (
	ConfBasePath = "conf/"
)

func main() {
	var port int
	var dir string
	flag.IntVar(&port, "port", 8302, "port")
	flag.StringVar(&dir, "dir", "conf/", "config file")
	ConfBasePath = dir

	app := http.Server{
		Addr:    fmt.Sprintf(":%d", port),
		Handler: getHandler(),
	}

	log.Printf("[ Service] - running on port %s\n", app.Addr)
	if err := app.ListenAndServe(); err != nil {
		log.Fatalf(" Server aborted!reason:%s\n", err.Error())
	}
}

type Item struct {
	Host        string            `json:"host"`
	AllLocation string            `json:"to"`
	Map         map[string]string `json:"map"`
}

type ItemManager struct {
	items    map[string]*Item
	basePath string
}

func (i *ItemManager) checkExists(path string) {
	_, err := os.Stat(path)
	if os.IsNotExist(err) {
		i.initExample()
	}
}

func (i *ItemManager) initExample() {
	if err := os.MkdirAll(i.basePath, os.ModePerm); err != nil {
		panic(err)
	}
	var defaultItems []*Item = []*Item{
		&Item{
			Host:        "*.z3q.net",
			AllLocation: "http://www.z3q.net",
		},
		&Item{
			Host: "s.z3q.net",
			Map: map[string]string{
				"blog": "http://www.s1n1.com",
			},
		},
	}
	bytes, _ := json.MarshalIndent(defaultItems, " ", " ")

	f, _ := os.Create(i.basePath + "gourl.conf")
	defer f.Close()
	wr := bufio.NewWriter(f)
	wr.Write(bytes)
	wr.Flush()
}

func (this *ItemManager) Load() {
	this.checkExists(this.basePath)
	filepath.Walk(this.basePath, func(path string, info os.FileInfo, err error) error {
		if err == nil && !info.IsDir() && strings.HasSuffix(path, ".conf") {
			items := this.GetItemsFromFile(path)
			this.Append(items)
		}
		return nil
	})
}

func (this *ItemManager) GetItemsFromFile(path string) []*Item {
	bytes, err := ioutil.ReadFile(path)
	if err != nil {
		return nil
	}
	var items []*Item = make([]*Item, 0)
	err = json.Unmarshal(bytes, &items)
	if err != nil {
		panic(path + " - " + err.Error())
	}
	return items
}

func (this *ItemManager) Append(items []*Item) {
	if this.items == nil {
		this.items = make(map[string]*Item, 0)
	}
	if items != nil {
		for _, v := range items {
			if _, ok := this.items[v.Host]; ok {
				panic("has exists host " + v.Host)
			}
			this.items[v.Host] = v
		}
	}
}

func (this *ItemManager) GetItemByHost(host string) *Item {
	for k, v := range this.items {
		if this.matchHost(k, host) {
			return v
		}
	}
	return nil
}

func (this *ItemManager) matchHost(key, host string) bool {
	if host == key {
		return true
	}
	if strings.HasPrefix(key, "*.") {
		return strings.HasSuffix(host, key[2:])
	}
	return false
}

var _ http.Handler = new(redirectHandler)

type redirectHandler struct {
	itemManager *ItemManager
}

func (r *redirectHandler) ServeHTTP(rsp http.ResponseWriter, req *http.Request) {
	host := req.Host // "s.z3q.net" use for test
	var item *Item = r.itemManager.GetItemByHost(host)
	if item != nil {
		if location, b := r.getLocation(rsp, req, item); b {
			rsp.Header().Add("Location", location)
			rsp.WriteHeader(302)
			return
		}
	}
	rsp.Write([]byte("Not match any host"))
}

func (r *redirectHandler) getLocation(rsp http.ResponseWriter, req *http.Request, item *Item) (string, bool) {
	path := req.URL.Path
	query := req.URL.RawQuery
	var con string
	if len(query) != 0 {
		con = "?"
	}
	if len(item.AllLocation) != 0 {
		return fmt.Sprintf("%s%s%s%s", item.AllLocation, path, con, query), true
	}
	if v, ok := item.Map[path[1:]]; ok {
		return fmt.Sprintf("%s%s%s", v, con, query), true
	}
	return "", false
}

func getHandler() http.Handler {
	re := &redirectHandler{
		itemManager: &ItemManager{
			basePath: ConfBasePath,
		},
	}
	re.itemManager.Load()
	return re
}
<|endoftext|>


<commit_before>package crucible

import (
	"bufio"
	"encoding/json"
	"fmt"
	"github.com/DewaldV/crucible/database"
	"github.com/DewaldV/crucible/logging"
	"io/ioutil"
	"os"
)

func init() {
	LoadDefaultConfig()
}

const (
	DefaultConfigPaths []string = []string{
		"/etc/crucible/",
		"/usr/local/etc/crucible/",
		"~/",
		"~/Dev/",
		"./"}
	DefaultConfigFileName = "crucible.conf"
)

type ConfigPrinter interface {
	PrintConfig() string
}

type CoreConfigStruct struct {
	HttpPort      int
	HttpsPort     int
	WorkerThreads int
	RootContext   string
	CoreLogger    *LoggerConfig
	DataSources   map[string]*database.DataSourceConfigStruct
	Services      map[string]*ServiceConfigStruct
}

type ServiceConfigStruct struct {
	Location       string
	AllowedOrigins map[string]bool
	AllowedMethods map[string]bool
}

type LoggerConfig struct {
	Level    logging.LogLevel
	FileName string
}

var Conf *CoreConfigStruct

func LoadDefaultConfig() error {
	var configPath string
	for path := range DefaultConfigPaths {
		configPath = fmt.Sprintf("%s%s", path, DefaultConfigFileName)
		_, err := os.Open(configPath)
		if err == nil {
			break
		}
	}

	return LoadConfig(configPath)
}

func LoadConfig(path string) error {
	configFile, err := os.Open(path)
	if err != nil {
		return &ConfigError{path, "Could not open config file for reading.", err.Error()}
	}
	defer configFile.Close()

	reader := bufio.NewReader(configFile)
	contents, err := ioutil.ReadAll(reader)
	if err != nil {
		return &ConfigError{path, "Error reading config file", err.Error()}
	}

	Conf = newCoreConfigStruct()

	err = json.Unmarshal(contents, Conf)
	if err != nil {
		return &ConfigError{path, "Error parsing config file", err.Error()}
	}

	fmt.Println("Loaded configuration:")
	fmt.Println(Conf.PrintConfig())

	database.LoadSessions(Conf.DataSources)

	return nil
}

func newCoreConfigStruct() (c *CoreConfigStruct) {
	c = new(CoreConfigStruct)
	c.HttpPort = 8787
	c.HttpsPort = 44443
	c.RootContext = "/"
	c.WorkerThreads = 1
	return
}

func newSiteConfigStruct() (s *ServiceConfigStruct) {
	s = new(ServiceConfigStruct)
	s.Location = "/"
	s.AllowedOrigins = make(map[string]bool)
	s.AllowedOrigins["localhost"] = true
	return
}

func newLoggerConfig() (c *logging.LoggerConfig) {
	c = new(logging.LoggerConfig)
	c.FileName = "log"
	c.Level = logging.Info
	return
}

func (c *CoreConfigStruct) PrintConfig() (s string) {
	s = fmt.Sprintf("HttpPort: %d\n", c.HttpPort)
	s += fmt.Sprintf("HttpsPort: %d\n", c.HttpsPort)
	s += fmt.Sprintf("RootContext: %s\n", c.RootContext)
	s += fmt.Sprintf("WorkerThreads: %d\n", c.WorkerThreads)
	for key, value := range c.DataSources {
		s += fmt.Sprintf("DataSourceName: %s\n", key)
		s += value.PrintConfig()
	}
	for key, value := range c.Services {
		s += fmt.Sprintf("ServiceName: %s\n", key)
		s += value.PrintConfig()
	}
	return
}

func (c *ServiceConfigStruct) PrintConfig() (s string) {
	s = fmt.Sprintf("\t> Location: %s\n", c.Location)
	var allowedMethods string
	for key := range c.AllowedMethods {
		allowedMethods += fmt.Sprintf("%s,", key)
	}
	s += fmt.Sprintf("\t> AllowedMethods: %s\n", allowedMethods[:len(allowedMethods)-1])

	var allowedOrigins string
	for key := range c.AllowedOrigins {
		allowedOrigins += fmt.Sprintf("%s,", key)
	}
	s += fmt.Sprintf("\t> AllowedOrigins: %s\n", allowedOrigins[:len(allowedOrigins)-1])
	return
}

type ConfigError struct {
	ConfigFile string
	What       string
	Err        string
}

func (e *ConfigError) Error() string {
	return fmt.Sprintf("Configuration error in %s > %s > %s\n", e.ConfigFile, e.What, e.Err)
}
<commit_msg>Fixed some small config blunders<commit_after>package crucible

import (
	"bufio"
	"encoding/json"
	"fmt"
	"github.com/DewaldV/crucible/database"
	"github.com/DewaldV/crucible/logging"
	"io/ioutil"
	"os"
)

func init() {
	LoadDefaultConfig()
}

var DefaultConfigPaths = [...]string{
	"/etc/crucible/",
	"/usr/local/etc/crucible/",
	"~/",
	"~/Dev/",
	"./"}

const (
	DefaultConfigFileName = "crucible.conf"
)

type ConfigPrinter interface {
	PrintConfig() string
}

type CoreConfigStruct struct {
	HttpPort      int
	HttpsPort     int
	WorkerThreads int
	RootContext   string
	CoreLogger    *LoggerConfig
	DataSources   map[string]*database.DataSourceConfigStruct
	Services      map[string]*ServiceConfigStruct
}

type ServiceConfigStruct struct {
	Location       string
	AllowedOrigins map[string]bool
	AllowedMethods map[string]bool
}

type LoggerConfig struct {
	Level    logging.LogLevel
	FileName string
}

var Conf *CoreConfigStruct

func LoadDefaultConfig() error {
	var configPath string
	for _, path := range DefaultConfigPaths {
		configPath = fmt.Sprintf("%s%s", path, DefaultConfigFileName)
		_, err := os.Open(configPath)
		if err == nil {
			return LoadConfig(configPath)
		}
	}

	return &ConfigError{"", "No default config file could be found", ""}
}

func LoadConfig(path string) error {
	configFile, err := os.Open(path)
	if err != nil {
		return &ConfigError{path, "Could not open config file for reading.", err.Error()}
	}
	defer configFile.Close()

	reader := bufio.NewReader(configFile)
	contents, err := ioutil.ReadAll(reader)
	if err != nil {
		return &ConfigError{path, "Error reading config file", err.Error()}
	}

	Conf = newCoreConfigStruct()

	err = json.Unmarshal(contents, Conf)
	if err != nil {
		return &ConfigError{path, "Error parsing config file", err.Error()}
	}

	fmt.Println("Loaded configuration:", path)
	fmt.Println(Conf.PrintConfig())

	database.LoadSessions(Conf.DataSources)

	return nil
}

func newCoreConfigStruct() (c *CoreConfigStruct) {
	c = new(CoreConfigStruct)
	c.HttpPort = 8787
	c.HttpsPort = 44443
	c.RootContext = "/"
	c.WorkerThreads = 1
	return
}

func newSiteConfigStruct() (s *ServiceConfigStruct) {
	s = new(ServiceConfigStruct)
	s.Location = "/"
	s.AllowedOrigins = make(map[string]bool)
	s.AllowedOrigins["localhost"] = true
	return
}

func newLoggerConfig() (c *logging.LoggerConfig) {
	c = new(logging.LoggerConfig)
	c.FileName = "log"
	c.Level = logging.Info
	return
}

func (c *CoreConfigStruct) PrintConfig() (s string) {
	s = fmt.Sprintf("HttpPort: %d\n", c.HttpPort)
	s += fmt.Sprintf("HttpsPort: %d\n", c.HttpsPort)
	s += fmt.Sprintf("RootContext: %s\n", c.RootContext)
	s += fmt.Sprintf("WorkerThreads: %d\n", c.WorkerThreads)
	for key, value := range c.DataSources {
		s += fmt.Sprintf("DataSourceName: %s\n", key)
		s += value.PrintConfig()
	}
	for key, value := range c.Services {
		s += fmt.Sprintf("ServiceName: %s\n", key)
		s += value.PrintConfig()
	}
	return
}

func (c *ServiceConfigStruct) PrintConfig() (s string) {
	s = fmt.Sprintf("\t> Location: %s\n", c.Location)
	var allowedMethods string
	for key := range c.AllowedMethods {
		allowedMethods += fmt.Sprintf("%s,", key)
	}
	s += fmt.Sprintf("\t> AllowedMethods: %s\n", allowedMethods[:len(allowedMethods)-1])

	var allowedOrigins string
	for key := range c.AllowedOrigins {
		allowedOrigins += fmt.Sprintf("%s,", key)
	}
	s += fmt.Sprintf("\t> AllowedOrigins: %s\n", allowedOrigins[:len(allowedOrigins)-1])
	return
}

type ConfigError struct {
	ConfigFile string
	What       string
	Err        string
}

func (e *ConfigError) Error() string {
	return fmt.Sprintf("Configuration error in %s > %s > %s\n", e.ConfigFile, e.What, e.Err)
}
<|endoftext|>


<commit_before>package authboss

import (
	"html/template"
	"io"
	"io/ioutil"
	"net/http"
	"time"

	"golang.org/x/crypto/bcrypt"
)

// Config holds all the configuration for both authboss and it's modules.
type Config struct {
	// MountPath is the path to mount authboss's routes at (eg /auth).
	MountPath string
	// ViewsPath is the path to search for overridden templates.
	ViewsPath string
	// RootURL is the scheme+host+port of the web application (eg https://www.happiness.com:8080) for url generation.  No trailing slash.
	RootURL string
	// BCryptCost is the cost of the bcrypt password hashing function.
	BCryptCost int

	// PrimaryID is the primary identifier of the user. Set to one of:
	// authboss.StoreEmail, authboss.StoreUsername (StoreEmail is default)
	PrimaryID string

	// Layout that all authboss views will be inserted into.
	Layout *template.Template
	// LayoutHTMLEmail is for emails going out in HTML form, authbosses e-mail templates
	// will be inserted into this layout.
	LayoutHTMLEmail *template.Template
	// LayoutTextEmail is for emails going out in text form, authbosses e-mail templates
	// will be inserted into this layout.
	LayoutTextEmail *template.Template
	// LayoutDataMaker is a function that can provide authboss with the layout's
	// template data. It will be merged with the data being provided for the current
	// view in order to render the templates.
	LayoutDataMaker ViewDataMaker

	// OAuth2Providers lists all providers that can be used. See
	// OAuthProvider documentation for more details.
	OAuth2Providers map[string]OAuth2Provider

	// ErrorHandler handles would be 500 errors.
	ErrorHandler http.Handler
	// BadRequestHandler handles would be 400 errors.
	BadRequestHandler http.Handler
	// NotFoundHandler handles would be 404 errors.
	NotFoundHandler http.Handler

	// AuthLoginOKPath is the redirect path after a successful authentication.
	AuthLoginOKPath string
	// AuthLoginFailPath is the redirect path after a failed authentication.
	AuthLoginFailPath string
	// AuthLogoutOKPath is the redirect path after a log out.
	AuthLogoutOKPath string

	// RecoverOKPath is the redirect path after a successful recovery of a password.
	RecoverOKPath string
	// RecoverTokenDuration controls how long a token sent via email for password
	// recovery is valid for.
	RecoverTokenDuration time.Duration

	// RegisterOKPath is the redirect path after a successful registration.
	RegisterOKPath string

	// Policies control validation of form fields and are automatically run
	// against form posts that include the fields.
	Policies []Validator
	// ConfirmFields are fields that are supposed to be submitted with confirmation
	// fields alongside them, passwords, emails etc.
	ConfirmFields []string
	// PreserveFields are fields used with registration that are to be rendered when
	// post fails.
	PreserveFields []string

	// ExpireAfter controls the time an account is idle before being logged out
	// by the ExpireMiddleware.
	ExpireAfter time.Duration

	// LockAfter this many tries.
	LockAfter int
	// LockWindow is the waiting time before the number of attemps are reset.
	LockWindow time.Duration
	// LockDuration is how long an account is locked for.
	LockDuration time.Duration

	// EmailFrom is the email address authboss e-mails come from.
	EmailFrom string
	// EmailSubjectPrefix is used to add something to the front of the authboss
	// email subjects.
	EmailSubjectPrefix string

	// XSRFName is the name of the xsrf token to put in the hidden form fields.
	XSRFName string
	// XSRFMaker is a function that returns an xsrf token for the current non-POST request.
	XSRFMaker XSRF

	// Storer is the interface through which Authboss accesses the web apps database.
	Storer Storer
	// OAuth2Storer is a different kind of storer only meant for OAuth2.
	OAuth2Storer OAuth2Storer
	// CookieStoreMaker must be defined to provide an interface capapable of storing cookies
	// for the given response, and reading them from the request.
	CookieStoreMaker CookieStoreMaker
	// SessionStoreMaker must be defined to provide an interface capable of storing session-only
	// values for the given response, and reading them from the request.
	SessionStoreMaker SessionStoreMaker
	// LogWriter is written to when errors occur, as well as on startup to show which modules are loaded
	// and which routes they registered. By default writes to io.Discard.
	LogWriter io.Writer
	// Mailer is the mailer being used to send e-mails out. Authboss defines two loggers for use
	// LogMailer and SMTPMailer, the default is a LogMailer to io.Discard.
	Mailer Mailer
}

// Defaults sets the configuration's default values.
func (c *Config) Defaults() {
	c.MountPath = "/"
	c.ViewsPath = "./"
	c.RootURL = "http://localhost:8080"
	c.BCryptCost = bcrypt.DefaultCost

	c.PrimaryID = StoreEmail

	c.Layout = template.Must(template.New("").Parse(`<!DOCTYPE html><html><body>{{template "authboss" .}}</body></html>`))
	c.LayoutHTMLEmail = template.Must(template.New("").Parse(`<!DOCTYPE html><html><body>{{template "authboss" .}}</body></html>`))
	c.LayoutTextEmail = template.Must(template.New("").Parse(`{{template "authboss" .}}`))

	c.AuthLoginOKPath = "/"
	c.AuthLoginFailPath = "/"
	c.AuthLogoutOKPath = "/"

	c.RecoverOKPath = "/"
	c.RecoverTokenDuration = time.Duration(24) * time.Hour

	c.RegisterOKPath = "/"

	c.Policies = []Validator{
		Rules{
			FieldName:       "username",
			Required:        true,
			MinLength:       2,
			MaxLength:       4,
			AllowWhitespace: false,
		},
		Rules{
			FieldName:       "password",
			Required:        true,
			MinLength:       4,
			MaxLength:       8,
			AllowWhitespace: false,
		},
	}
	c.ConfirmFields = []string{
		StorePassword, ConfirmPrefix + StorePassword,
	}

	c.ExpireAfter = 60 * time.Minute

	c.LockAfter = 3
	c.LockWindow = 5 * time.Minute
	c.LockDuration = 5 * time.Hour

	c.LogWriter = NewDefaultLogger()
	c.Mailer = LogMailer(ioutil.Discard)
}
<commit_msg>Fix default validators to be inline with default primary ID<commit_after>package authboss

import (
	"html/template"
	"io"
	"io/ioutil"
	"net/http"
	"time"

	"golang.org/x/crypto/bcrypt"
)

// Config holds all the configuration for both authboss and it's modules.
type Config struct {
	// MountPath is the path to mount authboss's routes at (eg /auth).
	MountPath string
	// ViewsPath is the path to search for overridden templates.
	ViewsPath string
	// RootURL is the scheme+host+port of the web application (eg https://www.happiness.com:8080) for url generation.  No trailing slash.
	RootURL string
	// BCryptCost is the cost of the bcrypt password hashing function.
	BCryptCost int

	// PrimaryID is the primary identifier of the user. Set to one of:
	// authboss.StoreEmail, authboss.StoreUsername (StoreEmail is default)
	PrimaryID string

	// Layout that all authboss views will be inserted into.
	Layout *template.Template
	// LayoutHTMLEmail is for emails going out in HTML form, authbosses e-mail templates
	// will be inserted into this layout.
	LayoutHTMLEmail *template.Template
	// LayoutTextEmail is for emails going out in text form, authbosses e-mail templates
	// will be inserted into this layout.
	LayoutTextEmail *template.Template
	// LayoutDataMaker is a function that can provide authboss with the layout's
	// template data. It will be merged with the data being provided for the current
	// view in order to render the templates.
	LayoutDataMaker ViewDataMaker

	// OAuth2Providers lists all providers that can be used. See
	// OAuthProvider documentation for more details.
	OAuth2Providers map[string]OAuth2Provider

	// ErrorHandler handles would be 500 errors.
	ErrorHandler http.Handler
	// BadRequestHandler handles would be 400 errors.
	BadRequestHandler http.Handler
	// NotFoundHandler handles would be 404 errors.
	NotFoundHandler http.Handler

	// AuthLoginOKPath is the redirect path after a successful authentication.
	AuthLoginOKPath string
	// AuthLoginFailPath is the redirect path after a failed authentication.
	AuthLoginFailPath string
	// AuthLogoutOKPath is the redirect path after a log out.
	AuthLogoutOKPath string

	// RecoverOKPath is the redirect path after a successful recovery of a password.
	RecoverOKPath string
	// RecoverTokenDuration controls how long a token sent via email for password
	// recovery is valid for.
	RecoverTokenDuration time.Duration

	// RegisterOKPath is the redirect path after a successful registration.
	RegisterOKPath string

	// Policies control validation of form fields and are automatically run
	// against form posts that include the fields.
	Policies []Validator
	// ConfirmFields are fields that are supposed to be submitted with confirmation
	// fields alongside them, passwords, emails etc.
	ConfirmFields []string
	// PreserveFields are fields used with registration that are to be rendered when
	// post fails.
	PreserveFields []string

	// ExpireAfter controls the time an account is idle before being logged out
	// by the ExpireMiddleware.
	ExpireAfter time.Duration

	// LockAfter this many tries.
	LockAfter int
	// LockWindow is the waiting time before the number of attemps are reset.
	LockWindow time.Duration
	// LockDuration is how long an account is locked for.
	LockDuration time.Duration

	// EmailFrom is the email address authboss e-mails come from.
	EmailFrom string
	// EmailSubjectPrefix is used to add something to the front of the authboss
	// email subjects.
	EmailSubjectPrefix string

	// XSRFName is the name of the xsrf token to put in the hidden form fields.
	XSRFName string
	// XSRFMaker is a function that returns an xsrf token for the current non-POST request.
	XSRFMaker XSRF

	// Storer is the interface through which Authboss accesses the web apps database.
	Storer Storer
	// OAuth2Storer is a different kind of storer only meant for OAuth2.
	OAuth2Storer OAuth2Storer
	// CookieStoreMaker must be defined to provide an interface capapable of storing cookies
	// for the given response, and reading them from the request.
	CookieStoreMaker CookieStoreMaker
	// SessionStoreMaker must be defined to provide an interface capable of storing session-only
	// values for the given response, and reading them from the request.
	SessionStoreMaker SessionStoreMaker
	// LogWriter is written to when errors occur, as well as on startup to show which modules are loaded
	// and which routes they registered. By default writes to io.Discard.
	LogWriter io.Writer
	// Mailer is the mailer being used to send e-mails out. Authboss defines two loggers for use
	// LogMailer and SMTPMailer, the default is a LogMailer to io.Discard.
	Mailer Mailer
}

// Defaults sets the configuration's default values.
func (c *Config) Defaults() {
	c.MountPath = "/"
	c.ViewsPath = "./"
	c.RootURL = "http://localhost:8080"
	c.BCryptCost = bcrypt.DefaultCost

	c.PrimaryID = StoreEmail

	c.Layout = template.Must(template.New("").Parse(`<!DOCTYPE html><html><body>{{template "authboss" .}}</body></html>`))
	c.LayoutHTMLEmail = template.Must(template.New("").Parse(`<!DOCTYPE html><html><body>{{template "authboss" .}}</body></html>`))
	c.LayoutTextEmail = template.Must(template.New("").Parse(`{{template "authboss" .}}`))

	c.AuthLoginOKPath = "/"
	c.AuthLoginFailPath = "/"
	c.AuthLogoutOKPath = "/"

	c.RecoverOKPath = "/"
	c.RecoverTokenDuration = time.Duration(24) * time.Hour

	c.RegisterOKPath = "/"

	c.Policies = []Validator{
		Rules{
			FieldName:       "email",
			Required:        true,
			AllowWhitespace: false,
		},
		Rules{
			FieldName:       "password",
			Required:        true,
			MinLength:       4,
			MaxLength:       8,
			AllowWhitespace: false,
		},
	}
	c.ConfirmFields = []string{
		StorePassword, ConfirmPrefix + StorePassword,
	}

	c.ExpireAfter = 60 * time.Minute

	c.LockAfter = 3
	c.LockWindow = 5 * time.Minute
	c.LockDuration = 5 * time.Hour

	c.LogWriter = NewDefaultLogger()
	c.Mailer = LogMailer(ioutil.Discard)
}
<|endoftext|>


<commit_before>package google

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"

	"code.google.com/p/google-api-go-client/compute/v1"
	// oauth2 "github.com/rasa/oauth2-fork-b3f9a68"
	"github.com/golang/oauth2"

	// oauth2 "github.com/rasa/oauth2-fork-b3f9a68/google"
	"github.com/golang/oauth2/google"
)

const clientScopes string = "https://www.googleapis.com/auth/compute"

// Config is the configuration structure used to instantiate the Google
// provider.
type Config struct {
	AccountFile string
	Project     string
	Region      string

	clientCompute *compute.Service
}

func (c *Config) loadAndValidate() error {
	var account accountFile

	// TODO: validation that it isn't blank
	if c.AccountFile == "" {
		c.AccountFile = os.Getenv("GOOGLE_ACCOUNT_FILE")
	}
	if c.Project == "" {
		c.Project = os.Getenv("GOOGLE_PROJECT")
	}
	if c.Region == "" {
		c.Region = os.Getenv("GOOGLE_REGION")
	}

	var f *oauth2.Options
	var err error

	if c.AccountFile != "" {
		if err := loadJSON(&account, c.AccountFile); err != nil {
			return fmt.Errorf(
				"Error loading account file '%s': %s",
				c.AccountFile,
				err)
		}

		// Get the token for use in our requests
		log.Printf("[INFO] Requesting Google token...")
		log.Printf("[INFO]   -- Email: %s", account.ClientEmail)
		log.Printf("[INFO]   -- Scopes: %s", clientScopes)
		log.Printf("[INFO]   -- Private Key Length: %d", len(account.PrivateKey))

		f, err = oauth2.New(
			oauth2.JWTClient(account.ClientEmail, []byte(account.PrivateKey)),
			oauth2.Scope(clientScopes),
			google.JWTEndpoint())

	} else {
		log.Printf("[INFO] Requesting Google token via GCE Service Role...")
		f, err = oauth2.New(google.ComputeEngineAccount(""))

	}

	if err != nil {
		return fmt.Errorf("Error retrieving auth token: %s", err)
	}

	log.Printf("[INFO] Instantiating GCE client...")
	c.clientCompute, err = compute.New(&http.Client{Transport: f.NewTransport()})
	if err != nil {
		return err
	}

	return nil
}

// accountFile represents the structure of the account file JSON file.
type accountFile struct {
	PrivateKeyId string `json:"private_key_id"`
	PrivateKey   string `json:"private_key"`
	ClientEmail  string `json:"client_email"`
	ClientId     string `json:"client_id"`
}

func loadJSON(result interface{}, path string) error {
	f, err := os.Open(path)
	if err != nil {
		return err
	}
	defer f.Close()

	dec := json.NewDecoder(f)
	return dec.Decode(result)
}
<commit_msg>Use new oauth2 golang library<commit_after>package google

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"

	"code.google.com/p/google-api-go-client/compute/v1"

	"golang.org/x/oauth2"
	"golang.org/x/oauth2/google"
	"golang.org/x/oauth2/jwt"
)

// Config is the configuration structure used to instantiate the Google
// provider.
type Config struct {
	AccountFile string
	Project     string
	Region      string

	clientCompute *compute.Service
}

func (c *Config) loadAndValidate() error {
	var account accountFile

	// TODO: validation that it isn't blank
	if c.AccountFile == "" {
		c.AccountFile = os.Getenv("GOOGLE_ACCOUNT_FILE")
	}
	if c.Project == "" {
		c.Project = os.Getenv("GOOGLE_PROJECT")
	}
	if c.Region == "" {
		c.Region = os.Getenv("GOOGLE_REGION")
	}

	var client *http.Client

	if c.AccountFile != "" {
		if err := loadJSON(&account, c.AccountFile); err != nil {
			return fmt.Errorf(
				"Error loading account file '%s': %s",
				c.AccountFile,
				err)
		}

		clientScopes := []string{"https://www.googleapis.com/auth/compute"}

		// Get the token for use in our requests
		log.Printf("[INFO] Requesting Google token...")
		log.Printf("[INFO]   -- Email: %s", account.ClientEmail)
		log.Printf("[INFO]   -- Scopes: %s", clientScopes)
		log.Printf("[INFO]   -- Private Key Length: %d", len(account.PrivateKey))

		conf := jwt.Config{
			Email:      account.ClientEmail,
			PrivateKey: []byte(account.PrivateKey),
			Scopes:     clientScopes,
			TokenURL:   "https://accounts.google.com/o/oauth2/token",
		}

		// Initiate an http.Client. The following GET request will be
		// authorized and authenticated on the behalf of
		// your service account.
		client = conf.Client(oauth2.NoContext)

	} else {
		log.Printf("[INFO] Requesting Google token via GCE Service Role...")
		client = &http.Client{
			Transport: &oauth2.Transport{
				// Fetch from Google Compute Engine's metadata server to retrieve
				// an access token for the provided account.
				// If no account is specified, "default" is used.
				Source: google.ComputeTokenSource(""),
			},
		}

	}

	log.Printf("[INFO] Instantiating GCE client...")
	var err error
	c.clientCompute, err = compute.New(client)
	if err != nil {
		return err
	}

	return nil
}

// accountFile represents the structure of the account file JSON file.
type accountFile struct {
	PrivateKeyId string `json:"private_key_id"`
	PrivateKey   string `json:"private_key"`
	ClientEmail  string `json:"client_email"`
	ClientId     string `json:"client_id"`
}

func loadJSON(result interface{}, path string) error {
	f, err := os.Open(path)
	if err != nil {
		return err
	}
	defer f.Close()

	dec := json.NewDecoder(f)
	return dec.Decode(result)
}
<|endoftext|>


<commit_before>package via

import (
	"os"
	"path"
	"strings"
)

var (
	cache  Cache
	home   = os.Getenv("HOME")
	cfile  = path.Join(home, "via.json")
	config = &Config{
		Arch:      "arm",
		OS:        "linux",
		Cache:     "$HOME/via/cache",
		DB:        "/data/data/gnuoid/var/db/via",
		Identity:  "test user <test@test.com>",
		Plans:     "$HOME/via/plans",
		PlansRepo: "https://code.google.com/p/via.plans",
		Repo:      "$HOME/via/repo",
		Root:      "/",
		Flags: []string{
			//"--target=arm-linux-gnueabi",
			"--host=arm-linux-gnueabi",
			"--disable-multilib",
			"--disable-dependency-tracking",
			"--disable-nls",
			"--with-shared",
			"--prefix=$PREFIX",
		},
		Env: map[string]string{
			"PREFIX":      "/data/data/gnuoid",
			"MAKEFLAGS":   "-j3 -sw",
			"CFLAGS":      "-pipe -O2 -static-libgcc -I/data/data/gnuoid/include",
			"LD_RUN_PATH": "/data/data/gnuoid/lib",
			"LDFLAGS":     "-L/data/data/gnuoid/lib",
		},
		Remove: []string{
			"data/data/gnuoid/share/doc",
			"data/data/gnuoid/share/info",
			"data/data/gnuoid/share/man",
		},
	}
	join = path.Join
)

func init() {
	/*
		if !file.Exists(cfile) {
			elog.Println("WARNING no config was found writing new one to", cfile)
			elog.Println("please review it.")
			err := json.Write(&config, cfile)
			if err != nil {
				elog.Fatal(err)
			}
			return
		}
		config = &Config{}
		err := json.Read(&config, cfile)
		if err != nil {
			elog.Fatal(err)
		}
		// TODO: provide Lint for master config
		err = json.Write(&config, cfile)
		if err != nil {
			elog.Fatal(err)
		}
	*/
	cache = Cache(os.ExpandEnv(string(config.Cache)))
	config.Plans = os.ExpandEnv(config.Plans)
	config.Repo = os.ExpandEnv(config.Repo)
	for i, j := range config.Env {
		os.Setenv(i, j)
	}
}

type Config struct {
	Identity  string
	Arch      string
	OS        string
	Root      string
	PlansRepo string

	// Paths
	Cache Cache
	DB    DB
	Plans string
	Repo  string

	// Toolchain
	Flags Flags

	Env    map[string]string
	Remove []string
}

type Flags []string

func (f Flags) String() string {
	return strings.Join(f, " ")
}

type DB string

func (d DB) Installed() string {
	return join(config.Root, string(d), "installed")
}

type Cache string

func (c Cache) Pkgs() string {
	return path.Join(string(c), "pkg")
}

func (c Cache) Srcs() string {
	return path.Join(string(c), "src")
}

func (c Cache) Builds() string {
	return path.Join(string(c), "bld")
}

func (c Cache) Stages() string {
	return path.Join(string(c), "stg")
}
<commit_msg>config: keep -q<commit_after>package via

import (
	"os"
	"path"
	"strings"
)

var (
	cache  Cache
	home   = os.Getenv("HOME")
	cfile  = path.Join(home, "via.json")
	config = &Config{
		Arch:      "arm",
		OS:        "linux",
		Cache:     "$HOME/via/cache",
		DB:        "/data/data/gnuoid/var/db/via",
		Identity:  "test user <test@test.com>",
		Plans:     "$HOME/via/plans",
		PlansRepo: "https://code.google.com/p/via.plans",
		Repo:      "$HOME/via/repo",
		Root:      "/",
		Flags: []string{
			//"--target=arm-linux-gnueabi",
			"--host=arm-linux-gnueabi",
			"--disable-multilib",
			"--disable-dependency-tracking",
			"--disable-nls",
			"--with-shared",
			"--prefix=$PREFIX",
			"-q",
		},
		Env: map[string]string{
			"PREFIX":      "/data/data/gnuoid",
			"MAKEFLAGS":   "-j3 -sw",
			"CFLAGS":      "-pipe -O2 -static-libgcc -I/data/data/gnuoid/include",
			"LD_RUN_PATH": "/data/data/gnuoid/lib",
			"LDFLAGS":     "-L/data/data/gnuoid/lib",
		},
		Remove: []string{
			"data/data/gnuoid/share/doc",
			"data/data/gnuoid/share/info",
			"data/data/gnuoid/share/man",
		},
	}
	join = path.Join
)

func init() {
	/*
		if !file.Exists(cfile) {
			elog.Println("WARNING no config was found writing new one to", cfile)
			elog.Println("please review it.")
			err := json.Write(&config, cfile)
			if err != nil {
				elog.Fatal(err)
			}
			return
		}
		config = &Config{}
		err := json.Read(&config, cfile)
		if err != nil {
			elog.Fatal(err)
		}
		// TODO: provide Lint for master config
		err = json.Write(&config, cfile)
		if err != nil {
			elog.Fatal(err)
		}
	*/
	cache = Cache(os.ExpandEnv(string(config.Cache)))
	config.Plans = os.ExpandEnv(config.Plans)
	config.Repo = os.ExpandEnv(config.Repo)
	for i, j := range config.Env {
		os.Setenv(i, j)
	}
}

type Config struct {
	Identity  string
	Arch      string
	OS        string
	Root      string
	PlansRepo string

	// Paths
	Cache Cache
	DB    DB
	Plans string
	Repo  string

	// Toolchain
	Flags Flags

	Env    map[string]string
	Remove []string
}

type Flags []string

func (f Flags) String() string {
	return strings.Join(f, " ")
}

type DB string

func (d DB) Installed() string {
	return join(config.Root, string(d), "installed")
}

type Cache string

func (c Cache) Pkgs() string {
	return path.Join(string(c), "pkg")
}

func (c Cache) Srcs() string {
	return path.Join(string(c), "src")
}

func (c Cache) Builds() string {
	return path.Join(string(c), "bld")
}

func (c Cache) Stages() string {
	return path.Join(string(c), "stg")
}
<|endoftext|>


<commit_before>package storage

import (
	zklib "github.com/samuel/go-zookeeper/zk"

	"github.com/zenoss/glog"
	"github.com/zenoss/serviced/coordinator/client"
	"github.com/zenoss/serviced/coordinator/client/zookeeper"
	"github.com/zenoss/serviced/domain/host"

	"encoding/json"
	"fmt"
	"os"
	"testing"
	"time"
)

func TestClient(t *testing.T) {

	zookeeper.EnsureZkFatjar()
	basePath := "/basePath"
	tc, err := zklib.StartTestCluster(1)
	if err != nil {
		t.Fatalf("could not start test zk cluster: %s", err)
	}
	defer os.RemoveAll(tc.Path)
	defer tc.Stop()
	time.Sleep(time.Second)

	servers := []string{fmt.Sprintf("127.0.0.1:%d", tc.Servers[0].Port)}

	dsnBytes, err := json.Marshal(zookeeper.DSN{Servers: servers, Timeout: time.Second * 15})
	if err != nil {
		t.Fatal("unexpected error creating zk DSN: %s", err)
	}
	dsn := string(dsnBytes)
	zclient, err := client.New("zookeeper", dsn, basePath, nil)

	conn, err := zclient.GetConnection()
	if err != nil {
		t.Fatal("unexpected error getting connection")
	}

	h := host.New()
	h.ID = "nodeID"
	h.IPAddr = "192.168.1.5"
	defer func(old func(string, os.FileMode) error) {
		mkdirAll = old
	}(mkdirAll)
	c, err := NewClient(h, zclient, "/opt/serviced/var")
	if err != nil {
		t.Fatalf("unexpected error creating client: %s", err)
	}
	defer c.Close()
	time.Sleep(time.Second * 5)

	nodePath := fmt.Sprintf("/storage/clients/%s", h.IPAddr)
	glog.Infof("about to check for %s", nodePath)
	if exists, err := conn.Exists(nodePath); err != nil {
		t.Fatalf("did not expect error checking for existence of %s: %s", nodePath, err)
	} else {
		if !exists {
			t.Fatalf("could not find %s", nodePath)
		}
	}
}
<commit_msg>create tempdir for src nfs mount in unit test<commit_after>package storage

import (
	zklib "github.com/samuel/go-zookeeper/zk"

	"github.com/zenoss/glog"
	"github.com/zenoss/serviced/coordinator/client"
	"github.com/zenoss/serviced/coordinator/client/zookeeper"
	"github.com/zenoss/serviced/domain/host"

	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"testing"
	"time"
)

func TestClient(t *testing.T) {

	zookeeper.EnsureZkFatjar()
	basePath := "/basePath"
	tc, err := zklib.StartTestCluster(1)
	if err != nil {
		t.Fatalf("could not start test zk cluster: %s", err)
	}
	defer os.RemoveAll(tc.Path)
	defer tc.Stop()
	time.Sleep(time.Second)

	servers := []string{fmt.Sprintf("127.0.0.1:%d", tc.Servers[0].Port)}

	dsnBytes, err := json.Marshal(zookeeper.DSN{Servers: servers, Timeout: time.Second * 15})
	if err != nil {
		t.Fatal("unexpected error creating zk DSN: %s", err)
	}
	dsn := string(dsnBytes)
	zclient, err := client.New("zookeeper", dsn, basePath, nil)

	conn, err := zclient.GetConnection()
	if err != nil {
		t.Fatal("unexpected error getting connection")
	}

	h := host.New()
	h.ID = "nodeID"
	h.IPAddr = "192.168.1.5"
	defer func(old func(string, os.FileMode) error) {
		mkdirAll = old
	}(mkdirAll)
	dir, err := ioutil.TempDir("", "serviced_var_")
	if err != nil {
		t.Fatalf("could not create tempdir: %s", err)
	}
	defer os.RemoveAll(dir)
	c, err := NewClient(h, zclient, dir)
	if err != nil {
		t.Fatalf("unexpected error creating client: %s", err)
	}
	defer c.Close()
	time.Sleep(time.Second * 5)

	nodePath := fmt.Sprintf("/storage/clients/%s", h.IPAddr)
	glog.Infof("about to check for %s", nodePath)
	if exists, err := conn.Exists(nodePath); err != nil {
		t.Fatalf("did not expect error checking for existence of %s: %s", nodePath, err)
	} else {
		if !exists {
			t.Fatalf("could not find %s", nodePath)
		}
	}
}
<|endoftext|>


<commit_before>//  Copyright (c) 2014 Couchbase, 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.

package main

import (
	"os"

	"github.com/blevesearch/bleve"

	log "github.com/couchbaselabs/clog"
)

func (pindex *PIndex) Run(mgr PIndexManager) {
	close := true
	cleanup := true

	var err error = nil

	if pindex.IndexType == "bleve" {
		close, cleanup, err = RunBleveStream(mgr, pindex, pindex.Stream,
			pindex.Impl.(bleve.Index))
		if err != nil {
			log.Printf("error: RunBleveStream, close: %b, cleanup: %b, err: %v",
				close, cleanup, err)
		} else {
			log.Printf("done: RunBleveStream, close: %b, cleanup: %b",
				close, cleanup)
		}
	} else {
		log.Printf("error: PIndex.Run() saw unknown IndexType: %s", pindex.IndexType)
	}

	// NOTE: We expect the PIndexImpl to handle any inflight, concurrent
	// queries, access and Close() correctly with its own locking.
	if close {
		pindex.Impl.Close()
	}

	if cleanup {
		os.RemoveAll(pindex.Path)
	}
}

func RunBleveStream(mgr PIndexManager, pindex *PIndex, stream Stream,
	bindex bleve.Index) (bool, bool, error) {
	for m := range stream {
		// TODO: probably need things like stream reset/rollback
		// and snapshot kinds of ops here, too.

		// TODO: maybe need a more batchy API?  Perhaps, yet another
		// goroutine that clumps up up updates into bigger batches?

		switch m := m.(type) {
		case *StreamUpdate:
			bindex.Index(string(m.Id()), m.Body())

		case *StreamDelete:
			bindex.Delete(string(m.Id()))

		case *StreamEnd:
			// Perhaps the datasource exited or is restarting?  We'll
			// keep our stream open in case a new feed is hooked up.
			if m.doneCh != nil {
				close(m.doneCh)
			}

		case *StreamFlush:
			// TODO: Need to delete all records here.  So, why not
			// implement this the same as rollback to zero?
			if m.doneCh != nil {
				close(m.doneCh)
			}

		case *StreamRollback:
			// TODO: Implement partial rollback one day.
			//
			// For now, always rollback to zero, in which we close the
			// pindex and have the janitor rebuild from scratch.
			pindex.Impl.Close()
			os.RemoveAll(pindex.Path)
			mgr.ClosePIndex(pindex)

			if m.doneCh != nil {
				close(m.doneCh)
			}
			return false, false, nil

		case *StreamSnapshot:
			// TODO: Need to ACK some snapshot?
			if m.doneCh != nil {
				close(m.doneCh)
			}
		}
	}

	return true, true, nil
}
<commit_msg>theoretical deadlock during stream rollback<commit_after>//  Copyright (c) 2014 Couchbase, 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.

package main

import (
	"os"

	"github.com/blevesearch/bleve"

	log "github.com/couchbaselabs/clog"
)

func (pindex *PIndex) Run(mgr PIndexManager) {
	close := true
	cleanup := true

	var err error = nil

	if pindex.IndexType == "bleve" {
		close, cleanup, err = RunBleveStream(mgr, pindex, pindex.Stream,
			pindex.Impl.(bleve.Index))
		if err != nil {
			log.Printf("error: RunBleveStream, close: %b, cleanup: %b, err: %v",
				close, cleanup, err)
		} else {
			log.Printf("done: RunBleveStream, close: %b, cleanup: %b",
				close, cleanup)
		}
	} else {
		log.Printf("error: PIndex.Run() saw unknown IndexType: %s", pindex.IndexType)
	}

	// NOTE: We expect the PIndexImpl to handle any inflight, concurrent
	// queries, access and Close() correctly with its own locking.
	if close {
		pindex.Impl.Close()
	}

	if cleanup {
		os.RemoveAll(pindex.Path)
	}
}

func RunBleveStream(mgr PIndexManager, pindex *PIndex, stream Stream,
	bindex bleve.Index) (bool, bool, error) {
	for m := range stream {
		// TODO: probably need things like stream reset/rollback
		// and snapshot kinds of ops here, too.

		// TODO: maybe need a more batchy API?  Perhaps, yet another
		// goroutine that clumps up up updates into bigger batches?

		switch m := m.(type) {
		case *StreamUpdate:
			bindex.Index(string(m.Id()), m.Body())

		case *StreamDelete:
			bindex.Delete(string(m.Id()))

		case *StreamEnd:
			// Perhaps the datasource exited or is restarting?  We'll
			// keep our stream open in case a new feed is hooked up.
			if m.doneCh != nil {
				close(m.doneCh)
			}

		case *StreamFlush:
			// TODO: Need to delete all records here.  So, why not
			// implement this the same as rollback to zero?
			if m.doneCh != nil {
				close(m.doneCh)
			}

		case *StreamRollback:
			// TODO: Implement partial rollback one day.
			//
			// For now, always rollback to zero, in which we close the
			// pindex and have the janitor rebuild from scratch.
			pindex.Impl.Close()
			os.RemoveAll(pindex.Path)

			// NOTE: Without the goroutine, then deadlock would be
			// possible in that telling the manager & janitor to close
			// us means any feeds will also synchronously close -- but
			// the feed that sent us a StreamRollback request might be
			// blocked awaiting a reply from us.
			go mgr.ClosePIndex(pindex)

			if m.doneCh != nil {
				close(m.doneCh)
			}
			return false, false, nil

		case *StreamSnapshot:
			// TODO: Need to ACK some snapshot?
			if m.doneCh != nil {
				close(m.doneCh)
			}
		}
	}

	return true, true, nil
}
<|endoftext|>


<commit_before>//see LICENSE
package main

import (
	"fmt"
	"html/template"
	"math/rand"
	"net/http"
	"os"
	"path/filepath"
	"regexp"
	"strings"

	log "github.com/Sirupsen/logrus"
	"github.com/codegangsta/cli"
	"github.com/dc0d/cx"
	"github.com/dimfeld/httptreemux"
)

func main() {
	app := cli.NewApp()
	app.Name = "dirsh"
	app.Usage = "share files in current directory (on http via a specific port)"
	app.Version = "0.2.1"
	app.Flags = []cli.Flag{
		cli.IntFlag{
			Name:   "port, p",
			Value:  9099,
			Usage:  "port to serve http on",
			EnvVar: "DIRSH_PORT",
		},
	}
	app.Action = func(c *cli.Context) {
		workerRegistry.Add(1)

		port := c.Int("port")
		go serve(port)
	}
	app.Run(os.Args)

	workerRegistry.Wait()
}

func serve(port int) {
	defer workerRegistry.Done()

	dir, err := os.Getwd()
	if err != nil {
		log.Error(err)
	}

	mux := httptreemux.New()

	mux.GET("/dir/*path", func(res http.ResponseWriter, req *http.Request, params map[string]string) {
		cx.Plumb(nil, reqLogger(), recoverPlumbing(), serveContent(`/dir`, dir)).ServeHTTP(res, req)
	})

	mux.GET("/", func(res http.ResponseWriter, req *http.Request, params map[string]string) {
		cx.Plumb(nil, reqLogger(), recoverPlumbing(), listFiles(dir)).ServeHTTP(res, req)
	})

	mux.GET("/preview/:mediatype/*path", func(res http.ResponseWriter, req *http.Request, params map[string]string) {
		mediaType := params["mediatype"]

		src := req.URL.String()
		r2 := prefixRegexp.FindAllStringSubmatch(src, -1)[0]
		n1 := prefixRegexp.SubexpNames()

		md := map[string]string{}
		for i, n := range r2 {
			md[n1[i]] = n
		}

		urlRest, ok := md["url_rest"]
		if ok {
			src = urlRest
		}

		cx.Plumb(nil, reqLogger(), recoverPlumbing(), playMedia(mediaType, src)).ServeHTTP(res, req)
	})

	log.Info(`started to serve dir `, dir, ` on url http://ocalhost:`, port)

	strPort := fmt.Sprintf(":%v", port)
	http.ListenAndServe(strPort, mux)
}

var (
	prefixRegexp = regexp.MustCompile(`/[^/]+/[^/]+(?P<url_rest>.*)`)
)

func playMedia(mediaType, src string) cx.MiddlewareFunc {
	return func(next http.Handler) http.Handler {
		var fh http.HandlerFunc = func(res http.ResponseWriter, req *http.Request) {
			if mediaType == "none" {
				mediaType = ``
			}

			data := struct {
				Src, MediaType string
			}{
				Src:       src,
				MediaType: mediaType,
			}

			pt := template.New("PlayerTemplate")
			t, err := pt.Parse(playerTemplate)
			if err != nil {
				log.Error(err)
			}
			err = t.Execute(res, data)
			if err != nil {
				log.Error(err)
			}

			// this is a final action
			// next.ServeHTTP(res, req)
		}

		return fh
	}
}

func listFiles(searchDir string) cx.MiddlewareFunc {
	return func(next http.Handler) http.Handler {
		var fh http.HandlerFunc = func(res http.ResponseWriter, req *http.Request) {
			data := fileItems(searchDir)
			pt := template.New("FileListTemplate")
			t, err := pt.Parse(fileListTemplate)
			if err != nil {
				log.Error(err)
			}
			err = t.Execute(res, data)
			if err != nil {
				log.Error(err)
			}

			// this is a final action
			// next.ServeHTTP(res, req)
		}

		return fh
	}
}

func fileItems(searchDir string) []*FileItem {
	fileList := listFilesInside(searchDir)
	classes := []string{"orange", "blue", "green", "purple", "gold"}

	var data []*FileItem
	for _, f := range fileList {
		colorIndex := rand.Intn(5)
		liClass := classes[colorIndex]

		s, _ := filepath.Rel(searchDir, f)
		_, fn := filepath.Split(f)
		ext := strings.ToLower(filepath.Ext(fn))
		src := fmt.Sprintf("/dir/%s", s)

		item := &FileItem{s, fn, liClass, ext, src, decideType(ext)}
		data = append(data, item)
	}

	return data
}

func decideType(ext string) (res string) {
	switch ext {
	case ".3gpp":
		res = "video/3gpp"
	case ".ogv":
		res = "video/ogg"
	case ".webm":
		res = "video/webm"
	case ".mp4":
		res = "video/mp4"
	// case ".mkv":
	default:
		res = `none`
	}

	res = strings.Replace(res, "/", "%2f", -1)

	return res
}

//FileItem +
type FileItem struct {
	Path, Name, Class, Ext, Src, MediaType string
}

const (
	//TODO: theme is aweful! fix it!

	fileListTemplate = `<!doctype html>

  <meta charset="utf-8">
  <title>Shared Content</title>

    {{range .}}

                {{.Name}}
                +

    {{end}}

`

	playerTemplate = `<!doctype html>

  <meta charset="utf-8">
  <title>Shared Content</title>

    <video width="100%%" height="100%%" controls>
        <source src="{{.Src}}" type="{{.MediaType}}">
        Your browser does not support the video tag.
    </video>

`
)
<commit_msg>a tolerable theme used<commit_after>//see LICENSE
package main

import (
	"fmt"
	"html/template"
	"math/rand"
	"net/http"
	"os"
	"path/filepath"
	"regexp"
	"strings"

	log "github.com/Sirupsen/logrus"
	"github.com/codegangsta/cli"
	"github.com/dc0d/cx"
	"github.com/dimfeld/httptreemux"
)

func main() {
	app := cli.NewApp()
	app.Name = "dirsh"
	app.Usage = "share files in current directory (on http via a specific port)"
	app.Version = "0.2.3"
	app.Flags = []cli.Flag{
		cli.IntFlag{
			Name:   "port, p",
			Value:  9099,
			Usage:  "port to serve http on",
			EnvVar: "DIRSH_PORT",
		},
	}
	app.Action = func(c *cli.Context) {
		workerRegistry.Add(1)

		port := c.Int("port")
		go serve(port)
	}
	app.Run(os.Args)

	workerRegistry.Wait()
}

func serve(port int) {
	defer workerRegistry.Done()

	dir, err := os.Getwd()
	if err != nil {
		log.Error(err)
	}

	mux := httptreemux.New()

	mux.GET("/dir/*path", func(res http.ResponseWriter, req *http.Request, params map[string]string) {
		cx.Plumb(nil, reqLogger(), recoverPlumbing(), serveContent(`/dir`, dir)).ServeHTTP(res, req)
	})

	mux.GET("/", func(res http.ResponseWriter, req *http.Request, params map[string]string) {
		cx.Plumb(nil, reqLogger(), recoverPlumbing(), listFiles(dir)).ServeHTTP(res, req)
	})

	mux.GET("/preview/:mediatype/*path", func(res http.ResponseWriter, req *http.Request, params map[string]string) {
		mediaType := params["mediatype"]

		src := req.URL.String()
		r2 := prefixRegexp.FindAllStringSubmatch(src, -1)[0]
		n1 := prefixRegexp.SubexpNames()

		md := map[string]string{}
		for i, n := range r2 {
			md[n1[i]] = n
		}

		urlRest, ok := md["url_rest"]
		if ok {
			src = urlRest
		}

		cx.Plumb(nil, reqLogger(), recoverPlumbing(), playMedia(mediaType, src)).ServeHTTP(res, req)
	})

	log.Info(`started to serve dir `, dir, ` on url http://ocalhost:`, port)

	strPort := fmt.Sprintf(":%v", port)
	http.ListenAndServe(strPort, mux)
}

var (
	prefixRegexp = regexp.MustCompile(`/[^/]+/[^/]+(?P<url_rest>.*)`)
)

func playMedia(mediaType, src string) cx.MiddlewareFunc {
	return func(next http.Handler) http.Handler {
		var fh http.HandlerFunc = func(res http.ResponseWriter, req *http.Request) {
			if mediaType == "none" {
				mediaType = ``
			}

			data := struct {
				Src, MediaType string
			}{
				Src:       src,
				MediaType: mediaType,
			}

			pt := template.New("PlayerTemplate")
			t, err := pt.Parse(playerTemplate)
			if err != nil {
				log.Error(err)
			}
			err = t.Execute(res, data)
			if err != nil {
				log.Error(err)
			}

			// this is a final action
			// next.ServeHTTP(res, req)
		}

		return fh
	}
}

func listFiles(searchDir string) cx.MiddlewareFunc {
	return func(next http.Handler) http.Handler {
		var fh http.HandlerFunc = func(res http.ResponseWriter, req *http.Request) {
			data := fileItems(searchDir)
			pt := template.New("FileListTemplate")
			t, err := pt.Parse(fileListTemplate)
			if err != nil {
				log.Error(err)
			}
			err = t.Execute(res, data)
			if err != nil {
				log.Error(err)
			}

			// this is a final action
			// next.ServeHTTP(res, req)
		}

		return fh
	}
}

func fileItems(searchDir string) []*FileItem {
	fileList := listFilesInside(searchDir)
	classes := []string{"orange", "blue", "green", "purple", "gold"}

	var data []*FileItem
	for _, f := range fileList {
		colorIndex := rand.Intn(5)
		liClass := classes[colorIndex]

		s, _ := filepath.Rel(searchDir, f)
		_, fn := filepath.Split(f)
		ext := strings.ToLower(filepath.Ext(fn))
		src := fmt.Sprintf("/dir/%s", s)

		item := &FileItem{s, fn, liClass, ext, src, decideType(ext)}
		data = append(data, item)
	}

	return data
}

func decideType(ext string) (res string) {
	switch ext {
	case ".3gpp":
		res = "video/3gpp"
	case ".ogv":
		res = "video/ogg"
	case ".webm":
		res = "video/webm"
	case ".mp4":
		res = "video/mp4"
	// case ".mkv":
	default:
		res = `none`
	}

	res = strings.Replace(res, "/", "%2f", -1)

	return res
}

//FileItem +
type FileItem struct {
	Path, Name, Class, Ext, Src, MediaType string
}

const (
	//TODO: theme is aweful! fix it!

	fileListTemplate = `<!doctype html>

  <meta charset="utf-8">
  <title>Shared Content</title>

    {{range .}}

                {{.Name}}
                +

    {{end}}

`

	playerTemplate = `<!doctype html>

  <meta charset="utf-8">
  <title>Shared Content</title>

    <video width="100%%" height="100%%" controls>
        <source src="{{.Src}}" type="{{.MediaType}}">
        Your browser does not support the video tag.
    </video>

`
)
<|endoftext|>


<commit_before>// Copyright 2015 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package factom

import (
	"fmt"
	"bytes"
	"crypto/sha256"
	"encoding/binary"
	"encoding/hex"
	"time"

	"golang.org/x/crypto/sha3"
)

type Chain struct {
	ChainID    string
	FirstEntry Entry
	Name       []string
}

func (c *Chain) Names() []string {
	names := make([]string, 0)
	for _, v := range c.Name {
		p, err := hex.DecodeString(v)
		if err != nil {
			return nil
		}
		names = append(names, string(p))
	}
	return names
}

type DBInfo struct {
	BTCBlockHash   string
	BTCBlockHeight int64
	BTCTxHash      string
	BTCTxOffset    int64
	DBHash         string
	DBMerkleRoot   string
}

type DBlock struct {
	DBHash string
	Header struct {
		BlockID       int
		EntryCount    int
		MerkleRoot    string
		PrevBlockHash string
		TimeStamp     int64
	}
	DBEntries []struct {
		ChainID    string
		MerkleRoot string
	}
}

func (d DBlock) Time() time.Time {
	return time.Unix(d.Header.TimeStamp, 0)
}

type EBlock struct {
	Header struct {
		BlockID       int
		PrevBlockHash string
		TimeStamp     int64
	}
	EBEntries []EBEntry
}

func (e EBlock) Time() time.Time {
	return time.Unix(e.Header.TimeStamp, 0)
}

type EBEntry struct {
	TimeStamp int64
	Hash      string
}

func (e EBEntry) Time() time.Time {
	return time.Unix(e.TimeStamp, 0)
}

type Entry struct {
	ChainID string
	ExtIDs  []string
	Data    string
}

func (e *Entry) Hash() [32]byte {
	a, err := e.MarshalBinary()
	if err != nil {
		return [32]byte{byte(0)}
	}
	fmt.Println("a:", hex.EncodeToString(a[:]))
	b := sha3.Sum256(a)
	fmt.Println("b:", hex.EncodeToString(b[:]))
	c := append(a, b[:]...)

	return sha256.Sum256(c)
}

func (e *Entry) MarshalBinary() ([]byte, error) {
	buf := new(bytes.Buffer)
	d, err := hex.DecodeString(e.Data)
	if err != nil {
		return buf.Bytes(), err
	}
	x, err := e.MarshalExtIDsBinary()
	if err != nil {
		return buf.Bytes(), err
	}

	// Header
	// 1 byte Version
	buf.Write([]byte{0})

	// 32 byte chainid
	if p, err := hex.DecodeString(e.ChainID); err != nil {
		return buf.Bytes(), err
	} else {
		buf.Write(p)
	}

	// 2 byte size of extids
	if err := binary.Write(buf, binary.BigEndian, int16(len(x))); err != nil {
		return buf.Bytes(), err
	}

	// 2 byte payload size
	if err := binary.Write(buf, binary.BigEndian, int16(len(x) + len(d)));
		err != nil {
		return buf.Bytes(), err
	}

	// Payload
	// extids
	buf.Write(x)

	// content
	buf.Write(d)

	return buf.Bytes(), nil
}

func (e *Entry) MarshalExtIDsBinary() ([]byte, error) {
	buf := new(bytes.Buffer)

	for _, v := range e.ExtIDs {
		p, err := hex.DecodeString(v)
		if err != nil {
			return buf.Bytes(), err
		}
		binary.Write(buf, binary.BigEndian, int16(len(p)))
		buf.Write(p)
	}

	return buf.Bytes(), nil
}
<commit_msg>removed debugging output<commit_after>// Copyright 2015 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package factom

import (
	"bytes"
	"crypto/sha256"
	"encoding/binary"
	"encoding/hex"
	"time"

	"golang.org/x/crypto/sha3"
)

type Chain struct {
	ChainID    string
	FirstEntry Entry
	Name       []string
}

func (c *Chain) Names() []string {
	names := make([]string, 0)
	for _, v := range c.Name {
		p, err := hex.DecodeString(v)
		if err != nil {
			return nil
		}
		names = append(names, string(p))
	}
	return names
}

type DBInfo struct {
	BTCBlockHash   string
	BTCBlockHeight int64
	BTCTxHash      string
	BTCTxOffset    int64
	DBHash         string
	DBMerkleRoot   string
}

type DBlock struct {
	DBHash string
	Header struct {
		BlockID       int
		EntryCount    int
		MerkleRoot    string
		PrevBlockHash string
		TimeStamp     int64
	}
	DBEntries []struct {
		ChainID    string
		MerkleRoot string
	}
}

func (d DBlock) Time() time.Time {
	return time.Unix(d.Header.TimeStamp, 0)
}

type EBlock struct {
	Header struct {
		BlockID       int
		PrevBlockHash string
		TimeStamp     int64
	}
	EBEntries []EBEntry
}

func (e EBlock) Time() time.Time {
	return time.Unix(e.Header.TimeStamp, 0)
}

type EBEntry struct {
	TimeStamp int64
	Hash      string
}

func (e EBEntry) Time() time.Time {
	return time.Unix(e.TimeStamp, 0)
}

type Entry struct {
	ChainID string
	ExtIDs  []string
	Data    string
}

func (e *Entry) Hash() [32]byte {
	a, err := e.MarshalBinary()
	if err != nil {
		return [32]byte{byte(0)}
	}
	b := sha3.Sum256(a)
	c := append(a, b[:]...)
	return sha256.Sum256(c)
}

func (e *Entry) MarshalBinary() ([]byte, error) {
	buf := new(bytes.Buffer)
	d, err := hex.DecodeString(e.Data)
	if err != nil {
		return buf.Bytes(), err
	}
	x, err := e.MarshalExtIDsBinary()
	if err != nil {
		return buf.Bytes(), err
	}

	// Header
	// 1 byte Version
	buf.Write([]byte{0})

	// 32 byte chainid
	if p, err := hex.DecodeString(e.ChainID); err != nil {
		return buf.Bytes(), err
	} else {
		buf.Write(p)
	}

	// 2 byte size of extids
	if err := binary.Write(buf, binary.BigEndian, int16(len(x))); err != nil {
		return buf.Bytes(), err
	}

	// 2 byte payload size
	if err := binary.Write(buf, binary.BigEndian, int16(len(x)+len(d))); err != nil {
		return buf.Bytes(), err
	}

	// Payload
	// extids
	buf.Write(x)

	// data
	buf.Write(d)

	return buf.Bytes(), nil
}

func (e *Entry) MarshalExtIDsBinary() ([]byte, error) {
	buf := new(bytes.Buffer)

	for _, v := range e.ExtIDs {
		p, err := hex.DecodeString(v)
		if err != nil {
			return buf.Bytes(), err
		}
		// 2 byte length of extid
		binary.Write(buf, binary.BigEndian, int16(len(p)))
		// extid
		buf.Write(p)
	}

	return buf.Bytes(), nil
}
<|endoftext|>


<commit_before>/* Copyright (c) 2015, Daniel Martí <mvdan@mvdan.cc> */
/* See LICENSE for licensing information */

package main

import (
	"go/importer"
	"go/types"
	"regexp"
)

var pkgs = [...]string{
	"encoding",
	"encoding/binary",
	"encoding/gob",
	"encoding/json",
	"encoding/xml",
	"flag",
	"fmt",
	"hash",
	"io",
	"net",
	"net/http",
	"sort",
	"sync",
}

type funcSign struct {
	params  []types.Type
	results []types.Type
}

type ifaceSign struct {
	t *types.Interface

	funcs map[string]funcSign
}

var (
	stdIfaces map[string]ifaceSign
	ownIfaces map[string]ifaceSign
)

func typesInit() error {
	stdIfaces = make(map[string]ifaceSign)
	ownIfaces = make(map[string]ifaceSign)
	imp := importer.Default()
	for _, path := range pkgs {
		pkg, err := imp.Import(path)
		if err != nil {
			return err
		}
		grabFromScope(stdIfaces, pkg.Scope(), false, path)
	}
	grabFromScope(stdIfaces, types.Universe, true, "")
	return nil
}

var exported = regexp.MustCompile(`^[A-Z]`)

func grabFromScope(ifaces map[string]ifaceSign, scope *types.Scope, unexported bool, impPath string) {
	for _, name := range scope.Names() {
		tn, ok := scope.Lookup(name).(*types.TypeName)
		if !ok {
			continue
		}
		if !unexported && !exported.MatchString(tn.Name()) {
			continue
		}
		t := tn.Type()
		if impPath != "" {
			name = impPath + "." + name
		}
		if _, e := ifaces[name]; e {
			continue
		}
		iface, ok := t.Underlying().(*types.Interface)
		if !ok {
			continue
		}
		if iface.NumMethods() == 0 {
			continue
		}
		ifsign := ifaceSign{
			t:     iface,
			funcs: make(map[string]funcSign, iface.NumMethods()),
		}
		for i := 0; i < iface.NumMethods(); i++ {
			f := iface.Method(i)
			fname := f.Name()
			sign := f.Type().(*types.Signature)
			ifsign.funcs[fname] = funcSign{
				params:  typeList(sign.Params()),
				results: typeList(sign.Results()),
			}
		}
		ifaces[name] = ifsign
	}
}

func typeList(t *types.Tuple) []types.Type {
	var l []types.Type
	for i := 0; i < t.Len(); i++ {
		v := t.At(i)
		l = append(l, v.Type())
	}
	return l
}
<commit_msg>Add more std packages<commit_after>/* Copyright (c) 2015, Daniel Martí <mvdan@mvdan.cc> */
/* See LICENSE for licensing information */

package main

import (
	"go/importer"
	"go/types"
	"regexp"
)

var pkgs = [...]string{
	"crypto",
	"encoding",
	"encoding/binary",
	"encoding/gob",
	"encoding/json",
	"encoding/xml",
	"flag",
	"fmt",
	"hash",
	"image",
	"io",
	"net",
	"net/http",
	"os",
	"reflect",
	"runtime",
	"sort",
	"sync",
}

type funcSign struct {
	params  []types.Type
	results []types.Type
}

type ifaceSign struct {
	t *types.Interface

	funcs map[string]funcSign
}

var (
	stdIfaces map[string]ifaceSign
	ownIfaces map[string]ifaceSign
)

func typesInit() error {
	stdIfaces = make(map[string]ifaceSign)
	ownIfaces = make(map[string]ifaceSign)
	imp := importer.Default()
	for _, path := range pkgs {
		pkg, err := imp.Import(path)
		if err != nil {
			return err
		}
		grabFromScope(stdIfaces, pkg.Scope(), false, path)
	}
	grabFromScope(stdIfaces, types.Universe, true, "")
	return nil
}

var exported = regexp.MustCompile(`^[A-Z]`)

func grabFromScope(ifaces map[string]ifaceSign, scope *types.Scope, unexported bool, impPath string) {
	for _, name := range scope.Names() {
		tn, ok := scope.Lookup(name).(*types.TypeName)
		if !ok {
			continue
		}
		if !unexported && !exported.MatchString(tn.Name()) {
			continue
		}
		t := tn.Type()
		if impPath != "" {
			name = impPath + "." + name
		}
		if _, e := ifaces[name]; e {
			continue
		}
		iface, ok := t.Underlying().(*types.Interface)
		if !ok {
			continue
		}
		if iface.NumMethods() == 0 {
			continue
		}
		ifsign := ifaceSign{
			t:     iface,
			funcs: make(map[string]funcSign, iface.NumMethods()),
		}
		for i := 0; i < iface.NumMethods(); i++ {
			f := iface.Method(i)
			fname := f.Name()
			sign := f.Type().(*types.Signature)
			ifsign.funcs[fname] = funcSign{
				params:  typeList(sign.Params()),
				results: typeList(sign.Results()),
			}
		}
		ifaces[name] = ifsign
	}
}

func typeList(t *types.Tuple) []types.Type {
	var l []types.Type
	for i := 0; i < t.Len(); i++ {
		v := t.At(i)
		l = append(l, v.Type())
	}
	return l
}
<|endoftext|>


<commit_before>package test

import (
	"fmt"
	oc_bgp "github.com/CiscoDevNet/ydk-go/ydk/models/ydktest/openconfig_bgp"
	"github.com/CiscoDevNet/ydk-go/ydk/providers"
	"github.com/CiscoDevNet/ydk-go/ydk/services"
	"github.com/CiscoDevNet/ydk-go/ydk/types/protocol"
	encoding "github.com/CiscoDevNet/ydk-go/ydk/types/encoding_format"
	"github.com/stretchr/testify/suite"
	"testing"
)

type OpenDaylightProviderTestSuite struct {
	suite.Suite
	Provider providers.OpenDaylightServiceProvider
	CRUD     services.CrudService
}

func (suite *OpenDaylightProviderTestSuite) SetupSuite() {
	suite.CRUD = services.CrudService{}
	suite.Provider = providers.OpenDaylightServiceProvider{
		Path:           TestHome,
		Address:        "localhost",
		Username:       "admin",
		Password:       "admin",
		Port:           12306,
		EncodingFormat: encoding.JSON,
		Protocol:       protocol.Restconf}
	suite.Provider.Connect()
}

func (suite *OpenDaylightProviderTestSuite) BeforeTest(suiteName, testName string) {
	fmt.Printf("%v: %v ...\n", suiteName, testName)
}

func (suite *OpenDaylightProviderTestSuite) TearDownSuite() {
	suite.Provider.Disconnect()
}

func (suite *OpenDaylightProviderTestSuite) TestReadODL() {
	bgp := oc_bgp.Bgp{}

	nodeIDs := suite.Provider.GetNodeIDs()

	fmt.Printf("nodeIDs: %v\n", nodeIDs)

	provider := suite.Provider.GetNodeProvider("xr")

	entity := suite.CRUD.Read(provider, &bgp)

	bgpRead := entity.(*oc_bgp.Bgp)

	fmt.Println("BGP configuration: ")
	fmt.Printf("AS: %v\n", bgpRead.Global.Config.As)
	fmt.Printf("Router ID: %v\n", bgpRead.Global.Config.RouterId)

	suite.Equal(bgpRead.Global.Config.As, "65172")
	suite.Equal(bgpRead.Global.Config.RouterId, "1.2.3.4")
}

func (suite *OpenDaylightProviderTestSuite) TestCreateODL() {
	bgp := oc_bgp.Bgp{}
	bgp.Global.Config.As = 65172
	bgp.Global.Config.RouterId = "1.2.3.4"

	neighbor := oc_bgp.Bgp_Neighbors_Neighbor{}
	neighbor.NeighborAddress = "6.7.8.9"
	neighbor.Config.NeighborAddress = "6.7.8.9"
	neighbor.Config.PeerAs = 65001
	neighbor.Config.LocalAs = 65001
	neighbor.Config.PeerGroup = "IBGP"
	bgp.Neighbors.Neighbor = append(bgp.Neighbors.Neighbor, neighbor)

	peerGroup := oc_bgp.Bgp_PeerGroups_PeerGroup{}
	peerGroup.PeerGroupName = "IBGP"
	peerGroup.Config.PeerGroupName = "IBGP"
	peerGroup.Config.Description = "test description"
	peerGroup.Config.PeerAs = 65001
	peerGroup.Config.LocalAs = 65001
	bgp.PeerGroups.PeerGroup = append(bgp.PeerGroups.PeerGroup, peerGroup)

	provider := suite.Provider.GetNodeProvider("xr")
	result := suite.CRUD.Create(provider, &bgp)

	suite.Equal(result, true)
}

func TestOpenDaylightProviderTestSuite(t *testing.T) {
	suite.Run(t, new(OpenDaylightProviderTestSuite))
}
<commit_msg>update odl provider test to replace Read() with ReadConfig()<commit_after>package test

import (
	"fmt"
	oc_bgp "github.com/CiscoDevNet/ydk-go/ydk/models/ydktest/openconfig_bgp"
	"github.com/CiscoDevNet/ydk-go/ydk"
	"github.com/CiscoDevNet/ydk-go/ydk/providers"
	"github.com/CiscoDevNet/ydk-go/ydk/services"
	"github.com/CiscoDevNet/ydk-go/ydk/types/protocol"
	encoding "github.com/CiscoDevNet/ydk-go/ydk/types/encoding_format"
	"github.com/stretchr/testify/suite"
	"testing"
)

type OpenDaylightProviderTestSuite struct {
	suite.Suite
	Provider providers.OpenDaylightServiceProvider
	CRUD     services.CrudService
}

func (suite *OpenDaylightProviderTestSuite) SetupSuite() {
	suite.CRUD = services.CrudService{}
	suite.Provider = providers.OpenDaylightServiceProvider{
		Path:           TestHome,
		Address:        "localhost",
		Username:       "admin",
		Password:       "admin",
		Port:           12306,
		EncodingFormat: encoding.JSON,
		Protocol:       protocol.Restconf}
	suite.Provider.Connect()
}

func (suite *OpenDaylightProviderTestSuite) BeforeTest(suiteName, testName string) {
	fmt.Printf("%v: %v ...\n", suiteName, testName)
}

func (suite *OpenDaylightProviderTestSuite) TearDownSuite() {
	suite.Provider.Disconnect()
}

func (suite *OpenDaylightProviderTestSuite) TestReadODL() {
	bgp := oc_bgp.Bgp{}

	nodeIDs := suite.Provider.GetNodeIDs()

	fmt.Printf("nodeIDs: %v\n", nodeIDs)

	provider := suite.Provider.GetNodeProvider("xr")

	entity := suite.CRUD.ReadConfig(provider, &bgp)

	bgpRead := entity.(*oc_bgp.Bgp)

	fmt.Println("BGP configuration: ")
	fmt.Printf("AS: %v\n", bgpRead.Global.Config.As)
	fmt.Printf("Router ID: %v\n", bgpRead.Global.Config.RouterId)

	suite.Equal(bgpRead.Global.Config.As, "65172")
	suite.Equal(bgpRead.Global.Config.RouterId, "1.2.3.4")
}

func (suite *OpenDaylightProviderTestSuite) TestCreateODL() {
	bgp := oc_bgp.Bgp{}
	bgp.Global.Config.As = 65172
	bgp.Global.Config.RouterId = "1.2.3.4"

	neighbor := oc_bgp.Bgp_Neighbors_Neighbor{}
	neighbor.NeighborAddress = "6.7.8.9"
	neighbor.Config.NeighborAddress = "6.7.8.9"
	neighbor.Config.PeerAs = 65001
	neighbor.Config.LocalAs = 65001
	neighbor.Config.PeerGroup = "IBGP"
	bgp.Neighbors.Neighbor = append(bgp.Neighbors.Neighbor, neighbor)

	peerGroup := oc_bgp.Bgp_PeerGroups_PeerGroup{}
	peerGroup.PeerGroupName = "IBGP"
	peerGroup.Config.PeerGroupName = "IBGP"
	peerGroup.Config.Description = "test description"
	peerGroup.Config.PeerAs = 65001
	peerGroup.Config.LocalAs = 65001
	bgp.PeerGroups.PeerGroup = append(bgp.PeerGroups.PeerGroup, peerGroup)

	provider := suite.Provider.GetNodeProvider("xr")
	result := suite.CRUD.Create(provider, &bgp)

	suite.Equal(result, true)
}

func TestOpenDaylightProviderTestSuite(t *testing.T) {
	if testing.Verbose() {
		ydk.EnableLogging(ydk.Debug)
	}
	suite.Run(t, new(OpenDaylightProviderTestSuite))
}
<|endoftext|>


<commit_before>package lfu

import (
	"testing"
)

func BenchmarkInsert(b *testing.B) {
	cache := NewLFU()
	for i := 0; i < b.N; i++ {
		cache.Insert(i, []int{i})
	}
}

func BenchmarkGet_EmptyCache(b *testing.B) {
	cache := NewLFU()
	for i := 0; i < b.N; i++ {
		cache.Get(i)
	}
}

func BenchmarkGet_AllMisses(b *testing.B) {
	cache := NewLFU()
	total := 1000000

	// Insert 1 million items
	for i := 0; i < total; i++ {
		cache.Insert(i, []int{i})
	}

	b.ResetTimer()
	// Attempt to get items that are not in cache
	for i := 0; i < b.N; i++ {
		cache.Get(i + total)
	}
}

func BenchmarkGet_AllHits(b *testing.B) {
	cache := NewLFU()
	total := 1000000

	// Insert 1 million items
	for i := 0; i < total; i++ {
		cache.Insert(i, []int{i})
	}

	b.ResetTimer()
	// Attempt to get items that are not in cache
	for i := 0; i < b.N; i++ {
		cache.Get(i)
	}
}
<commit_msg>Add parallel benchmarks<commit_after>package lfu

import (
	"testing"
)

func BenchmarkInsert(b *testing.B) {
	cache := NewLFU()
	for i := 0; i < b.N; i++ {
		cache.Insert(i, []int{i})
	}
}

func BenchmarkParallelInsert(b *testing.B) {
	cache := NewLFU()
	i := 0
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			i++
			cache.Insert(i, []byte("user:data:cached"))
		}
	})
}

func BenchmarkGet_EmptyCache(b *testing.B) {
	cache := NewLFU()
	for i := 0; i < b.N; i++ {
		cache.Get(i)
	}
}

func BenchmarkGet_AllMisses(b *testing.B) {
	cache := NewLFU()
	total := 1000000

	// Insert 1 million items
	for i := 0; i < total; i++ {
		cache.Insert(i, []int{i})
	}

	b.ResetTimer()
	// Attempt to get items that are not in cache
	for i := 0; i < b.N; i++ {
		cache.Get(i + total)
	}
}

func BenchmarkGet_AllHits(b *testing.B) {
	cache := NewLFU()
	total := 1000000

	// Insert 1 million items
	for i := 0; i < total; i++ {
		cache.Insert(i, []int{i})
	}

	b.ResetTimer()
	// Attempt to get items that are not in cache
	for i := 0; i < b.N; i++ {
		cache.Get(i)
	}
}

func BenchmarkParallelGet(b *testing.B) {
	cache := NewLFU()
	total := 1000000

	// Insert 1 million items
	for i := 0; i < total; i++ {
		cache.Insert(i, []int{i})
	}

	b.ResetTimer()

	i := 0
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			i++
			cache.Insert(i, []byte("user:data:cached"))
		}
	})
}
<|endoftext|>


<commit_before>/*
   Copyright 2020 Docker Compose CLI authors

   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.
*/

package ecs

import (
	"fmt"

	"github.com/awslabs/goformation/v4/cloudformation"
	"github.com/awslabs/goformation/v4/cloudformation/ec2"
	"github.com/awslabs/goformation/v4/cloudformation/ecs"
	"github.com/compose-spec/compose-go/types"
)

func (b *ecsAPIService) createNFSmountIngress(securityGroups []string, project *types.Project, n string, template *cloudformation.Template) error {
	target := securityGroups[0]
	for _, s := range project.Services {
		for _, v := range s.Volumes {
			if v.Source != n {
				continue
			}
			var source string
			for net := range s.Networks {
				network := project.Networks[net]
				if ext, ok := network.Extensions[extensionSecurityGroup]; ok {
					source = ext.(string)
				} else {
					source = networkResourceName(net)
				}
				break
			}
			name := fmt.Sprintf("%sNFSMount%s", normalizeResourceName(s.Name), n)
			template.Resources[name] = &ec2.SecurityGroupIngress{
				Description:           fmt.Sprintf("Allow NFS mount for %s on %s", s.Name, n),
				GroupId:               target,
				SourceSecurityGroupId: cloudformation.Ref(source),
				IpProtocol:            "tcp",
				FromPort:              2049,
				ToPort:                2049,
			}
			service := template.Resources[serviceResourceName(s.Name)].(*ecs.Service)
			service.AWSCloudFormationDependsOn = append(service.AWSCloudFormationDependsOn, name)
		}
	}
	return nil
}
<commit_msg>normalize volume name<commit_after>/*
   Copyright 2020 Docker Compose CLI authors

   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.
*/

package ecs

import (
	"fmt"

	"github.com/awslabs/goformation/v4/cloudformation"
	"github.com/awslabs/goformation/v4/cloudformation/ec2"
	"github.com/awslabs/goformation/v4/cloudformation/ecs"
	"github.com/compose-spec/compose-go/types"
)

func (b *ecsAPIService) createNFSmountIngress(securityGroups []string, project *types.Project, n string, template *cloudformation.Template) error {
	target := securityGroups[0]
	for _, s := range project.Services {
		for _, v := range s.Volumes {
			if v.Source != n {
				continue
			}
			var source string
			for net := range s.Networks {
				network := project.Networks[net]
				if ext, ok := network.Extensions[extensionSecurityGroup]; ok {
					source = ext.(string)
				} else {
					source = networkResourceName(net)
				}
				break
			}
			name := fmt.Sprintf("%sNFSMount%s", normalizeResourceName(s.Name), normalizeResourceName(n))
			template.Resources[name] = &ec2.SecurityGroupIngress{
				Description:           fmt.Sprintf("Allow NFS mount for %s on %s", s.Name, n),
				GroupId:               target,
				SourceSecurityGroupId: cloudformation.Ref(source),
				IpProtocol:            "tcp",
				FromPort:              2049,
				ToPort:                2049,
			}
			service := template.Resources[serviceResourceName(s.Name)].(*ecs.Service)
			service.AWSCloudFormationDependsOn = append(service.AWSCloudFormationDependsOn, name)
		}
	}
	return nil
}
<|endoftext|>