wiki/main.go

86 lines
1.9 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"
2017-02-13 22:21:57 +01:00
"github.com/dalu/wiki/public"
"github.com/dalu/wiki/storage"
"github.com/dalu/wiki/storage/mongo"
"github.com/dalu/wiki/template"
2017-02-10 22:00:26 +01:00
"github.com/dalu/wiki/wiki"
"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())
}
renderer := template.New(true)
2017-02-10 22:00:26 +01:00
mux := http.NewServeMux()
wikiHandler := wiki.NewWikiHandler(mongostore, renderer, log)
2017-02-10 22:00:26 +01:00
mux.Handle(wiki.DefaultMountPath, http.StripPrefix(wiki.DefaultMountPath, wikiHandler))
2017-02-13 22:21:57 +01:00
mux.Handle(public.CSSMountpoint, public.CSSHandler)
mux.Handle(public.JSMountpoint, public.JSHandler)
2017-02-11 23:13:16 +01:00
server := &http.Server{
2017-02-13 22:21:57 +01:00
Handler: mux,
Addr: ":8080",
2017-02-11 23:13:16 +01:00
WriteTimeout: 15 * time.Second,
2017-02-13 22:21:57 +01:00
ReadTimeout: 15 * time.Second,
2017-02-11 23:13:16 +01:00
}
log.Fatal(server.ListenAndServe())
2017-02-10 22:00:26 +01:00
}
func initializeWiki(s storage.Interface) error {
2017-02-10 22:00:26 +01:00
_, 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()
2017-02-13 22:21:57 +01:00
r.Text = "This is the main apge"
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
}