cpolis/cmd/main.go

85 lines
3.1 KiB
Go

package main
import (
"encoding/gob"
"log"
"net/http"
"os"
"streifling.com/jason/cpolis/cmd/control"
"streifling.com/jason/cpolis/cmd/model"
"streifling.com/jason/cpolis/cmd/view"
)
func init() {
gob.Register(model.User{})
}
func main() {
config, err := control.HandleConfig()
if err != nil {
log.Fatalln(err)
}
logFile, err := os.OpenFile(config.LogFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatalln(err)
}
defer logFile.Close()
log.SetOutput(logFile)
db, err := model.OpenDB(config.DBName)
if err != nil {
log.Fatalln(err)
}
defer db.Close()
key, err := control.LoadKey(config.KeyFile)
if err != nil {
key, err = control.NewKey()
if err != nil {
log.Fatalln(err)
}
control.SaveKey(key, config.KeyFile)
}
store := control.NewCookieStore(key)
mux := http.NewServeMux()
mux.Handle("/web/static/", http.StripPrefix("/web/static/",
http.FileServer(http.Dir(config.WebDir+"/static/"))))
mux.HandleFunc("/", view.HomePage(config, db, store))
mux.HandleFunc("GET /create-tag", view.CreateTag(config))
mux.HandleFunc("GET /create-user", view.CreateUser(config))
mux.HandleFunc("GET /edit-self", view.EditSelf(config, db, store))
mux.HandleFunc("GET /edit-user/{id}", view.EditUser(config, db))
mux.HandleFunc("GET /hub", view.ShowHub(config, db, store))
mux.HandleFunc("GET /logout", view.Logout(config, store))
mux.HandleFunc("GET /pics/{pic}", view.ServeImage(config, store))
mux.HandleFunc("GET /publish-article/{id}", view.PublishArticle(config, db, store))
mux.HandleFunc("GET /publish-issue", view.PublishLatestIssue(config, db, store))
mux.HandleFunc("GET /reject-article/{id}", view.RejectArticle(config, db, store))
mux.HandleFunc("GET /rejected-articles", view.ShowRejectedArticles(config, db, store))
mux.HandleFunc("GET /review-rejected-article/{id}", view.ReviewRejectedArticle(config, db, store))
mux.HandleFunc("GET /review-unpublished-article/{id}", view.ReviewUnpublishedArticle(config, db, store))
mux.HandleFunc("GET /rss", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, config.RSSFile)
})
mux.HandleFunc("GET /show-all-users", view.ShowAllUsers(config, db))
mux.HandleFunc("GET /this-issue", view.ShowCurrentArticles(config, db))
mux.HandleFunc("GET /unpublished-articles", view.ShowUnpublishedArticles(config, db))
mux.HandleFunc("GET /write-article", view.WriteArticle(config, db))
mux.HandleFunc("POST /add-first-user", view.AddFirstUser(config, db, store))
mux.HandleFunc("POST /add-tag", view.AddTag(config, db, store))
mux.HandleFunc("POST /add-user", view.AddUser(config, db, store))
mux.HandleFunc("POST /login", view.Login(config, db, store))
mux.HandleFunc("POST /resubmit-article/{id}", view.ResubmitArticle(config, db, store))
mux.HandleFunc("POST /submit-article", view.SubmitArticle(config, db, store))
mux.HandleFunc("POST /update-self", view.UpdateSelf(config, db, store))
mux.HandleFunc("POST /update-user/{id}", view.UpdateUser(config, db, store))
mux.HandleFunc("POST /upload-image", view.UploadImage(config))
log.Fatalln(http.ListenAndServe(config.Port, mux))
}