Exemplo n.º 1
0
func main() {
	log.Trace("Starting")
	flag.Parse()

	if *blog_dir == "" {
		log.Error("Must specify a directory where blogs are stored")
		time.Sleep(1000)
		os.Exit(1)
	}

	blogs := blog.New()
	blogReader := reader.New(blogs, *blog_dir, log)
	v := view.New(blogs, log)
	router := router.New(v, log)

	err := blogReader.ReadBlogs()
	if err != nil {
		log.Error("Error creating blog reader: %s", err)
		time.Sleep(1000)
		os.Exit(1)
	}

	http.Handle("/", router)
	http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./public"))))
	err = http.ListenAndServe(":"+*protocol, nil)
	if err != nil {
		log.Error("Problem with http server: %s", err)
		time.Sleep(1000)
		os.Exit(1)
	}

	log.Trace("Stopping")
}
Exemplo n.º 2
0
func Test_CanReadAndGetBlogs(t *testing.T) {
	blogs := blog.New()
	reader := reader.New(blogs, "../blogs", log)

	reader.ReadBlogs()
	blog1 := blogs.Get("example_1")
	blog2 := blogs.Get("example_2")
	blog3 := blogs.Get("example_3")
	blog4 := blogs.Get("example_4")

	if blog1 == nil || blog2 == nil || blog3 == nil || blog4 == nil {
		t.Error("Did not properly read blogs")
		return
	}

	if blog1.Title != "Example 1" {
		t.Error("Did not receive expected blog")
		return
	}

	if blog2.Title != "Example 2" {
		t.Error("Did not receive expected blog")
		return
	}

	if blog3.Title != "Example 3" {
		t.Error("Did not receive expected blog")
		return
	}

	if blog4.Title != "Example 4" {
		t.Error("Did not receive expected blog")
		return
	}
}
Exemplo n.º 3
0
func Test_LastSkipsFirstThree(t *testing.T) {
	blogs := blog.New()

	if len(blogs.Last()) != 0 {
		t.Error("Shouldn't have returned a blog")
		return
	}

	blogs.Add(&blog.Blog{Title: "one"})

	if len(blogs.Last()) != 0 {
		t.Error("Shouldn't have returned a blog")
		return
	}

	blogs.Add(&blog.Blog{Title: "two"})

	if len(blogs.Last()) != 0 {
		t.Error("Should have no blogs")
		return
	}

	blogs.Add(&blog.Blog{Title: "three"})

	if len(blogs.Last()) != 0 {
		t.Error("Should have no blogs")
		return
	}

	blogs.Add(&blog.Blog{Title: "four"})

	if len(blogs.Last()) != 1 {
		t.Error("Should have a blog")
		return
	}

	if !(blogs.Last()[0].Title == "one") {
		t.Error("Wrong blog")
		return
	}

	blogs.Add(&blog.Blog{Title: "five"})

	if len(blogs.Last()) != 2 {
		t.Error("Should have a blog")
		return
	}

	if !(blogs.Last()[0].Title == "two") {
		t.Error("Wrong blog")
		return
	}

	if !(blogs.Last()[1].Title == "one") {
		t.Error("Wrong blog")
		return
	}
}
Exemplo n.º 4
0
func Test_GetsNilIfDoesntExist(t *testing.T) {
	blogs := blog.New()
	blogs.Add(&blog.Blog{Title: "dont care"})

	if blogs.Get("title_1") != nil {
		t.Error("Expected nil for non-existant blog")
		return
	}
}
Exemplo n.º 5
0
func Test_MakeBlogWithFile(t *testing.T) {
	b := blog.New()
	r := reader.New(b, "dir", log)
	err := r.NewBlogFromFile("example.txt")

	if err != nil {
		t.Error("Unexpected error parsing the blog file:", err.Error())
		return
	}

	if !(b.Blogs()[0].Title == "The Title") {
		t.Error("Did not set the proper title")
		return
	}

	if !(b.Blogs()[0].Intro == "The Intro") {
		t.Error("Did not set the proper intro")
		return
	}

	if !(b.Blogs()[0].Subsections[0].Header == "Sub Title 1") {
		t.Error("Did not set the proper subtitle", b.Blogs()[0].Subsections[0].Header)
		return
	}

	if !(b.Blogs()[0].Subsections[0].Text == "Sub Text 1") {
		t.Error("Did not set the proper sub text")
		return
	}

	if !(b.Blogs()[0].Subsections[1].Header == "Sub Title 2") {
		t.Error("Did not set the proper subtitle")
		return
	}

	if !(b.Blogs()[0].Subsections[1].Text == "Sub Text 2") {
		t.Error("Did not set the proper sub text")
		return
	}

	if !(b.Blogs()[0].Outro == "The Outro") {
		t.Error("Did not set the proper outro")
		return
	}

	if !(b.Blogs()[0].Tags[0].Name == "Tag 1") {
		t.Error("Did not set the proper tag")
		return
	}

	if !(b.Blogs()[0].Tags[1].Name == "Tag 2") {
		t.Error("Did not set the proper tag")
		return
	}
}
Exemplo n.º 6
0
func Test_BlogRequiresIntro(t *testing.T) {
	r := reader.New(blog.New(), "dir", log)
	err := r.NewBlogFromFile("missing_intro.txt")

	if err == nil {
		t.Error("Expected error")
		return
	}

	if !(err.Error() == "Missing Section: Intro") {
		t.Error("Did not get the correct error")
		return
	}
}
Exemplo n.º 7
0
func Test_CanHaveMiltiLines(t *testing.T) {
	b := blog.New()
	r := reader.New(b, "dir", log)
	err := r.NewBlogFromFile("multiline_body.txt")

	if err != nil {
		t.Error("Unexpected error")
		return
	}

	if !(b.Blogs()[0].Intro == "The Intro, line 1\nThe Intro, line 2\nThe Intro, line 3") {
		t.Error("Unexpected Intro:", b.Blogs()[0].Intro)
		return
	}
}
Exemplo n.º 8
0
func Test_CanGetBlogs(t *testing.T) {
	blogs := blog.New()

	blog1 := &blog.Blog{Title: "Title 1"}
	blog2 := &blog.Blog{Title: "Title 2"}
	blog3 := &blog.Blog{Title: "Title 3"}

	blogs.Add(blog1)
	blogs.Add(blog2)
	blogs.Add(blog3)

	if blogs.Get("title_1") == nil {
		t.Error("Did not receive a blog")
		return
	}

	if blogs.Get("title_2") == nil {
		t.Error("Did not receive a blog")
		return
	}

	if blogs.Get("title_3") == nil {
		t.Error("Did not receive a blog")
		return
	}

	if blogs.Get("title_1") != blog1 {
		t.Error("Did not receive expected blog")
		return
	}

	if blogs.Get("title_2") != blog2 {
		t.Error("Did not receive expected blog")
		return
	}

	if blogs.Get("title_3") != blog3 {
		t.Error("Did not receive expected blog")
		return
	}
}
Exemplo n.º 9
0
func Test_AddABlog(t *testing.T) {
	blogs := blog.New()

	if len(blogs.Blogs()) != 0 {
		t.Error("Shouldn't have any blogs yet")
		return
	}

	blogs.Add(&blog.Blog{Title: "Example 1", Intro: "Intro"})

	if len(blogs.Blogs()) != 1 {
		t.Error("Should have a blog")
		return
	}

	if !(blogs.Blogs()[0].Title == "Example 1") {
		t.Error("Got the wrong blog title")
		return
	}

	blogs.Add(&blog.Blog{Title: "Example 2", Intro: "Intro"})

	if len(blogs.Blogs()) != 2 {
		t.Error("Should have two blogs")
		return
	}

	if !(blogs.Blogs()[0].Title == "Example 1") {
		t.Error("Got the wrong blog title")
		return
	}

	if !(blogs.Blogs()[1].Title == "Example 2") {
		t.Error("Got the wrong blog title")
		return
	}
}
Exemplo n.º 10
0
func Test_FirstGivesFirstThree(t *testing.T) {
	blogs := blog.New()

	if len(blogs.First()) != 0 {
		t.Error("Shouldn't have returned a blog")
		return
	}

	blogs.Add(&blog.Blog{Title: "one"})

	if len(blogs.First()) != 1 {
		t.Error("Should have just one blog")
		return
	}

	if !(blogs.First()[0].Title == "one") {
		t.Error("Wrong blog")
		return
	}

	blogs.Add(&blog.Blog{Title: "two"})

	if len(blogs.First()) != 2 {
		t.Error("Should have two blogs")
		return
	}

	if !(blogs.First()[1].Title == "one") {
		t.Error("Wrong blog")
		return
	}

	if !(blogs.First()[0].Title == "two") {
		t.Error("Wrong blog")
		return
	}

	blogs.Add(&blog.Blog{Title: "three"})

	if len(blogs.First()) != 3 {
		t.Error("Should have three blogs")
		return
	}

	if !(blogs.First()[2].Title == "one") {
		t.Error("Wrong blog")
		return
	}

	if !(blogs.First()[1].Title == "two") {
		t.Error("Wrong blog")
		return
	}

	if !(blogs.First()[0].Title == "three") {
		t.Error("Wrong blog")
		return
	}

	blogs.Add(&blog.Blog{Title: "four"})

	if len(blogs.First()) != 3 {
		t.Error("Should have three blogs")
		return
	}

	if !(blogs.First()[2].Title == "two") {
		t.Error("Wrong blog")
		return
	}

	if !(blogs.First()[1].Title == "three") {
		t.Error("Wrong blog")
		return
	}

	if !(blogs.First()[0].Title == "four") {
		t.Error("Wrong blog")
		return
	}
}