wiki/main.go

72 lines
1.6 KiB
Go
Raw Normal View History

2017-02-10 18:52:07 +01:00
package main
2017-02-10 22:00:26 +01:00
import (
"net/http"
"time"
2017-02-10 22:00:26 +01:00
"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)
2017-02-10 22:00:26 +01:00
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"
r := new(storage.Revision)
r.ID = bson.NewObjectId()
r.PublishDate = time.Now()
r.AuthorIP = "127.0.0.1"
r.TermID = t2.ID.Hex()
r.Text = "# Main Page"
t2.CurrentRevisionID = r.ID.Hex()
2017-02-10 22:00:26 +01:00
if e := s.CreateTerm(t1); e != nil {
return e
}
if e := s.CreateTerm(t2); e != nil {
return e
}
if e := s.CreateRevision(r); e != nil {
return e
}
2017-02-10 22:00:26 +01:00
} else {
return e
}
}
return nil
}