65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
"streifling.com/jason/cpolis/cmd/data"
|
|
"streifling.com/jason/cpolis/cmd/ui"
|
|
)
|
|
|
|
func main() {
|
|
logFile, err := os.Create("tmp/cpolis.log")
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
defer logFile.Close()
|
|
log.SetOutput(logFile)
|
|
|
|
db, err := data.OpenDB("cpolis")
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
defer db.Close()
|
|
|
|
feed, err := data.OpenFeed("tmp/rss.gob")
|
|
if err != nil {
|
|
log.Println(err)
|
|
feed = data.NewFeed("Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt",
|
|
"https://distrikt-ni-st.de",
|
|
"Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität")
|
|
}
|
|
defer feed.Close()
|
|
|
|
articleList := data.NewArticleList()
|
|
defer articleList.Close()
|
|
|
|
mux := http.NewServeMux()
|
|
mux.Handle("/web/static/", http.StripPrefix("/web/static/", http.FileServer(http.Dir("web/static/"))))
|
|
|
|
numRows, err := db.CountEntries()
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
if numRows == 0 {
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
template.Must(template.ParseFiles("web/templates/index.html", "web/templates/add-user.html")).Execute(w, nil)
|
|
})
|
|
} else {
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
template.Must(template.ParseFiles("web/templates/index.html", "web/templates/login.html")).Execute(w, nil)
|
|
})
|
|
}
|
|
|
|
mux.HandleFunc("POST /add-user/", ui.AddUser(db))
|
|
mux.HandleFunc("POST /create-user/", ui.CreateUser())
|
|
mux.HandleFunc("POST /finish-article/", ui.FinishArticle(articleList))
|
|
mux.HandleFunc("POST /login/", ui.Login(db))
|
|
mux.HandleFunc("POST /write-article/", ui.WriteArticle())
|
|
mux.HandleFunc("/rss/", ui.ShowRSS(feed))
|
|
|
|
log.Fatalln(http.ListenAndServe(":8080", mux))
|
|
}
|