wiki/main.go
2017-02-10 22:00:26 +01:00

60 lines
1.3 KiB
Go

package main
import (
"net/http"
"github.com/Sirupsen/logrus"
"github.com/dalu/wiki/wiki"
"github.com/dalu/wiki/wiki/storage"
"github.com/dalu/wiki/wiki/storage/mongo"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func main() {
log := logrus.New()
ms, e := mgo.Dial("localhost")
if e != nil {
log.Fatal("could not connect to mongodb", e.Error())
}
defer ms.Close()
mongostore := mongo.New(ms, mongo.Config{Database: "wiki"})
if e := initializeWiki(mongostore); e != nil {
log.Fatal("initializeWiki: ", e.Error())
}
mux := http.NewServeMux()
wikiHandler := wiki.NewWikiHandler(mongostore, log)
mux.Handle(wiki.DefaultMountPath, http.StripPrefix(wiki.DefaultMountPath, wikiHandler))
log.Fatal(http.ListenAndServe(":8080", mux))
}
func initializeWiki(s storage.WikiStorage) error {
_, e := s.GetTermByName("")
if e != nil {
if e == mgo.ErrNotFound {
t1 := new(storage.Term)
t1.ID = bson.NewObjectId()
t1.Language = "en" //TODO: create a Main_Page for all languages
t1.Redirect = "Main_Page"
t2 := new(storage.Term)
t2.ID = bson.NewObjectId()
t2.Language = "en"
t2.Name = "Main Page"
t2.Slug = "Main_Page"
if e := s.CreateTerm(t1); e != nil {
return e
}
if e := s.CreateTerm(t2); e != nil {
return e
}
} else {
return e
}
}
return nil
}