main.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package main
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/Sirupsen/logrus"
  6. "github.com/dalu/wiki/public"
  7. "github.com/dalu/wiki/storage"
  8. "github.com/dalu/wiki/storage/mongo"
  9. "github.com/dalu/wiki/template"
  10. "github.com/dalu/wiki/wiki"
  11. "gopkg.in/mgo.v2"
  12. "gopkg.in/mgo.v2/bson"
  13. )
  14. func main() {
  15. log := logrus.New()
  16. ms, e := mgo.Dial("localhost")
  17. if e != nil {
  18. log.Fatal("could not connect to mongodb", e.Error())
  19. }
  20. defer ms.Close()
  21. mongostore := mongo.New(ms, mongo.Config{Database: "wiki"})
  22. if e := initializeWiki(mongostore); e != nil {
  23. log.Fatal("initializeWiki: ", e.Error())
  24. }
  25. renderer := template.New(true)
  26. mux := http.NewServeMux()
  27. wikiHandler := wiki.NewWikiHandler(mongostore, renderer, log)
  28. mux.Handle(wiki.DefaultMountPath, http.StripPrefix(wiki.DefaultMountPath, wikiHandler))
  29. mux.Handle(public.CSSMountpoint, public.CSSHandler)
  30. mux.Handle(public.JSMountpoint, public.JSHandler)
  31. server := &http.Server{
  32. Handler: mux,
  33. Addr: ":8080",
  34. WriteTimeout: 15 * time.Second,
  35. ReadTimeout: 15 * time.Second,
  36. }
  37. log.Fatal(server.ListenAndServe())
  38. }
  39. func initializeWiki(s storage.Interface) error {
  40. _, e := s.GetTermByName("")
  41. if e != nil {
  42. if e == mgo.ErrNotFound {
  43. t1 := new(storage.Term)
  44. t1.ID = bson.NewObjectId()
  45. t1.Language = "en" //TODO: create a Main_Page for all languages
  46. t1.Redirect = "Main_Page"
  47. t2 := new(storage.Term)
  48. t2.ID = bson.NewObjectId()
  49. t2.Language = "en"
  50. t2.Name = "Main Page"
  51. t2.Slug = "Main_Page"
  52. r := new(storage.Revision)
  53. r.ID = bson.NewObjectId()
  54. r.PublishDate = time.Now()
  55. r.AuthorIP = "127.0.0.1"
  56. r.TermID = t2.ID.Hex()
  57. r.Text = "This is the main apge"
  58. t2.CurrentRevisionID = r.ID.Hex()
  59. if e := s.CreateTerm(t1); e != nil {
  60. return e
  61. }
  62. if e := s.CreateTerm(t2); e != nil {
  63. return e
  64. }
  65. if e := s.CreateRevision(r); e != nil {
  66. return e
  67. }
  68. } else {
  69. return e
  70. }
  71. }
  72. return nil
  73. }