Exemplo n.º 1
0
func TestConnectionURL(t *testing.T) {

	c := ConnectionURL{}

	// Default connection string is only the protocol.
	if c.String() != "" {
		t.Fatal(`Expecting default connectiong string to be empty, got:`, c.String())
	}

	// Adding a database name.
	c.Database = "myfilename"

	if c.String() != "mongodb://myfilename" {
		t.Fatal(`Test failed, got:`, c.String())
	}

	// Adding an option.
	c.Options = map[string]string{
		"cache": "foobar",
		"mode":  "ro",
	}

	// Adding username and password
	c.User = "******"
	c.Password = "******"

	// Setting host.
	c.Address = db.Host("localhost")

	if c.String() != "mongodb://*****:*****@localhost/myfilename?cache=foobar&mode=ro" {
		t.Fatal(`Test failed, got:`, c.String())
	}

	// Setting host and port.
	c.Address = db.HostPort("localhost", 27017)

	if c.String() != "mongodb://*****:*****@localhost:27017/myfilename?cache=foobar&mode=ro" {
		t.Fatal(`Test failed, got:`, c.String())
	}

	// Setting cluster.
	c.Address = Cluster(db.Host("localhost"), db.Host("1.2.3.4"), db.HostPort("example.org", 1234))

	if c.String() != "mongodb://*****:*****@localhost,1.2.3.4,example.org:1234/myfilename?cache=foobar&mode=ro" {
		t.Fatal(`Test failed, got:`, c.String())
	}

	// Setting another database.
	c.Database = "another_database"

	if c.String() != "mongodb://*****:*****@localhost,1.2.3.4,example.org:1234/another_database?cache=foobar&mode=ro" {
		t.Fatal(`Test failed, got:`, c.String())
	}

}
Exemplo n.º 2
0
// ParseURL parses s into a ConnectionURL struct.
func ParseURL(s string) (u ConnectionURL, err error) {
	o := make(values)

	if strings.HasPrefix(s, "postgres://") {
		s, err = pq.ParseURL(s)
		if err != nil {
			return u, err
		}
	}

	if err := parseOpts(s, o); err != nil {
		return u, err
	}

	u.User = o.Get("user")
	u.Password = o.Get("password")

	h := o.Get("host")
	p, _ := strconv.Atoi(o.Get("port"))

	if p > 0 {
		u.Address = db.HostPort(h, uint(p))
	} else {
		u.Address = db.Host(h)
	}

	u.Database = o.Get("dbname")

	u.Options = map[string]string{
		"sslmode": o.Get("sslmode"),
	}

	return u, err
}
Exemplo n.º 3
0
func main() {

	r := gin.Default()
	settings := mongo.ConnectionURL{
		Address:  db.Host("ds031763.mongolab.com:31763"), // MongoDB hostname.
		Database: "dirty-chat",                           // Database name.
		User:     "******",                              // Optional user name.
		Password: "******",
	}

	var err error

	Mng, err = db.Open(mongo.Adapter, settings)
	if err != nil {
		fmt.Println(err.Error())
	}

	defer Mng.Close()

	Store = sessions.NewCookieStore([]byte("nebdr84"))
	r.Use(sessions.Middleware("my_session", Store))
	r.Use(csrf.Middleware(csrf.Options{Secret: "nebdr84", IgnoreMethods: []string{"GET"}}))
	r.Use(static.Serve("/", static.LocalFile("assets", false)))
	r.Use(AuthInspector())
	r.Use(GlobalResources())
	rnd = render.New(render.Options{
		Directory:       "templates",                 // Specify what path to load the templates from.
		Layout:          "layout",                    // Specify a layout template. Layouts can call {{ yield }} to render the current template or {{ block "css" }} to render a block from the current template
		Extensions:      []string{".tmpl", ".html"},  // Specify extensions to load for templates.
		Delims:          render.Delims{"{[{", "}]}"}, // Sets delimiters to the specified strings.
		Charset:         "UTF-8",                     // Sets encoding for json and html content-types. Default is "UTF-8".
		IndentJSON:      true,                        // Output human readable JSON.
		IndentXML:       false,
		PrefixJSON:      []byte(")]}',\n"), // Prefixes JSON responses with the given bytes.
		HTMLContentType: "text/html",       // Output XHTML content type instead of default "text/html".
		IsDevelopment:   true,              // Render will now recompile the templates on every HTML response.
		UnEscapeHTML:    true,              // Replace ensure '&<>' are output correctly (JSON only).
		StreamingJSON:   true,              // Streams the JSON response via json.Encoder.
	})

	r.LoadHTMLGlob("templates/*.html")

	r.Any("/", indexHandler)
	r.GET("/login", ShowLogin)
	r.POST("/login", Login)
	// r.GET("user/:name", controllers.ShowUser)
	//r.POST("user/:name", controllers.EditUser)

	r.GET("/sex", controllers.IndexSex)
	r.GET("/sex/:name/:edit", controllers.EditSex)
	r.DELETE("/sex/:name", controllers.DeleteSex)
	r.POST("/sex", controllers.CreateSex)
	r.POST("/sex/:name", controllers.UpdateSex)
	r.GET("/sex.json", controllers.IndexSexJson)

	r.Run(":3000")
}
Exemplo n.º 4
0
func TestConnectionURL(t *testing.T) {

	c := ConnectionURL{}

	// Default connection string is empty.
	if c.String() != "" {
		t.Fatal(`Expecting default connectiong string to be empty, got:`, c.String())
	}

	// Adding a host.
	c.Address = db.Host("localhost")

	if c.String() != "host=localhost sslmode=disable" {
		t.Fatal(`Test failed, got:`, c.String())
	}

	// Adding a username.
	c.User = "******"

	if c.String() != "user=Anakin host=localhost sslmode=disable" {
		t.Fatal(`Test failed, got:`, c.String())
	}

	// Adding a password with special characters.
	c.Password = "******"

	if c.String() != `user=Anakin password=Some\ Sort\ of\ \'\ Password host=localhost sslmode=disable` {
		t.Fatal(`Test failed, got:`, c.String())
	}

	// Adding a port.
	c.Address = db.HostPort("localhost", 1234)

	if c.String() != `user=Anakin password=Some\ Sort\ of\ \'\ Password host=localhost port=1234 sslmode=disable` {
		t.Fatal(`Test failed, got:`, c.String())
	}

	// Adding a database.
	c.Database = "MyDatabase"

	if c.String() != `user=Anakin password=Some\ Sort\ of\ \'\ Password host=localhost port=1234 dbname=MyDatabase sslmode=disable` {
		t.Fatal(`Test failed, got:`, c.String())
	}

	// Adding options.
	c.Options = map[string]string{
		"sslmode": "verify-full",
	}

	if c.String() != `user=Anakin password=Some\ Sort\ of\ \'\ Password host=localhost port=1234 dbname=MyDatabase sslmode=verify-full` {
		t.Fatal(`Test failed, got:`, c.String())
	}

}
Exemplo n.º 5
0
	ContentLength uint      `json:"content_length" db:",json"`
	Host          string    `json:"host" db:",json"`
	URL           string    `json:"url" db:",json"`
	Scheme        string    `json:"scheme" db:",json"`
	Path          string    `json:"path" db:",path"`
	Header        Header    `json:"header,omitempty" db:",json"`
	Body          []byte    `json:"body,omitempty" db:",json"`
	RequestHeader Header    `json:"request_header,omitempty" db:",json"`
	RequestBody   []byte    `json:"request_body,omitempty" db:",json"`
	DateStart     time.Time `json:"date_start" db:",json"`
	DateEnd       time.Time `json:"date_end" db:",json"`
	TimeTaken     int64     `json:"time_taken" db:",json"`
}

var settings = mongo.ConnectionURL{
	Address:  db.Host(Host), // MongoDB hostname.
	Database: Database,      // Database name.
	User:     User,          // Optional user name.
	Password: Password,      // Optional user password.
}

var (
	flagAddress     = flag.String("l", defaultAddress, "Bind address.")
	flagPort        = flag.Uint("p", defaultPort, "Port to bind to, default is 3129")
	flagSSLPort     = flag.Uint("s", defaultSSLPort, "Port to bind to (SSL mode), default is 3128.")
	flagSSLCertFile = flag.String("c", "", "Path to root CA certificate.")
	flagSSLKeyFile  = flag.String("k", "", "Path to root CA key.")
)

var (
	sess db.Database