Example #1
0
func newTestServer(t *testing.T) (*httptest.Server, chan bool) {
	done := make(chan bool)
	tmpd, err := ioutil.TempDir("/tmp", "erickson")
	if err != nil {
		t.Fatal(err)
	}
	go func() {
		<-done
		if err := os.RemoveAll(tmpd); err != nil {
			t.Fatal(err)
		}
	}()
	db, err := db.NewBoltDB(tmpd + "/erickson.db")
	if err != nil {
		t.Fatal(err)
	}
	u, err := review.NewUser("bob", "supersecret")
	if err != nil {
		t.Fatal(err)
	}
	if err := db.CreateUser(u); err != nil {
		t.Fatal(err)
	}
	key := []byte("12345678901234567890123456789012")
	return httptest.NewServer(server.NewRootHandler(db, tmpd, key, mail.Nil, "")), done
}
Example #2
0
func TestServer(t *testing.T) {
	f, err := ioutil.TempFile("/tmp", "erickson")
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(f.Name())

	db, err := db.NewBoltDB(f.Name())
	if err != nil {
		t.Fatal(err)
	}

	user, err := review.NewUser("testuser", "testpassword")
	if err != nil {
		t.Fatal(err)
	}
	if err := db.CreateUser(user); err != nil {
		t.Fatal(err)
	}

	wd, err := os.Getwd()
	if err != nil {
		t.Fatal(err)
	}
	handler := NewRootHandler(db, wd+"./..", []byte("12345678901234567890123456789012"), mail.Nil, "")

	server := httptest.NewUnstartedServer(handler)
	cert, err := tls.LoadX509KeyPair("test.crt", "test.key")
	if err != nil {
		t.Fatal(err)
	}
	tlsConfig := &tls.Config{
		InsecureSkipVerify: true, // testing only
		Certificates:       []tls.Certificate{cert},
	}
	server.TLS = tlsConfig
	server.StartTLS()
	defer server.Close()

	*handler.URL = server.URL
	url := server.URL

	client := newClient(t, url)
	url = create(t, client, url)
	read(t, client, url)
	annotate(t, client, url+"/rev/0")
	update(t, client, url)
	destroy(t, client, url)
}
Example #3
0
func exec() {
	scfg := config.Server
	db, err := db.NewBoltDB(scfg.Database)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	smtpAuth := smtp.PlainAuth("", config.Mail.Username, config.Mail.Password, config.Mail.Server)
	mailer := mail.NewMailer(config.Mail.Server, config.Mail.Username, smtpAuth)

	handler := server.NewRootHandler(db, ".", []byte(scfg.SessionKey), mailer, scfg.URLRoot)
	if scfg.TLSCert != "" && scfg.TLSKey != "" && len(scfg.SessionKey) == 32 {
		CSRF := csrf.Protect([]byte(scfg.SessionKey))
		log.Fatal(http.ListenAndServeTLS(":"+scfg.Port, scfg.TLSCert, scfg.TLSKey, CSRF(handler)))
	} else {
		log.Fatal(http.ListenAndServe(":"+scfg.Port, handler))
	}
}