cpolis/cmd/main.go

85 lines
2.8 KiB
Go
Raw Normal View History

2024-02-18 07:20:51 +01:00
package main
import (
2024-03-03 09:16:49 +01:00
"encoding/gob"
"log"
"net/http"
2024-02-24 11:41:01 +01:00
"os"
2024-03-09 10:27:04 +01:00
"streifling.com/jason/cpolis/cmd/control"
"streifling.com/jason/cpolis/cmd/model"
"streifling.com/jason/cpolis/cmd/view"
)
2024-02-18 07:20:51 +01:00
2024-03-03 09:16:49 +01:00
func init() {
2024-03-09 10:27:04 +01:00
gob.Register(model.User{})
2024-03-03 09:16:49 +01:00
}
2024-02-18 07:20:51 +01:00
func main() {
2024-03-29 09:16:41 +01:00
args, err := control.HandleCliArgs()
if err != nil {
log.Fatalln(err)
}
2024-03-29 09:16:41 +01:00
logFile, err := os.OpenFile(args.LogFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
2024-02-24 11:41:01 +01:00
if err != nil {
log.Fatalln(err)
}
defer logFile.Close()
log.SetOutput(logFile)
2024-02-24 11:41:01 +01:00
2024-03-29 09:16:41 +01:00
db, err := model.OpenDB(args.DBName)
if err != nil {
log.Fatalln(err)
}
2024-02-22 19:27:41 +01:00
defer db.Close()
2024-03-29 09:16:41 +01:00
key, err := control.LoadKey(args.KeyFile)
2024-03-03 09:16:49 +01:00
if err != nil {
2024-03-09 10:27:04 +01:00
key, err = control.NewKey()
2024-03-03 09:16:49 +01:00
if err != nil {
log.Fatalln(err)
}
2024-03-29 09:16:41 +01:00
control.SaveKey(key, args.KeyFile)
2024-03-03 09:16:49 +01:00
}
2024-03-09 10:27:04 +01:00
store := control.NewCookieStore(key)
2024-03-03 09:16:49 +01:00
mux := http.NewServeMux()
mux.Handle("/web/static/", http.StripPrefix("/web/static/",
2024-03-29 09:16:41 +01:00
http.FileServer(http.Dir(args.WebDir+"/static/"))))
mux.HandleFunc("/", view.HomePage(args, db, store))
mux.HandleFunc("GET /create-tag", view.CreateTag(args))
mux.HandleFunc("GET /create-user", view.CreateUser(args))
mux.HandleFunc("GET /edit-user", view.EditUser(args, db, store))
mux.HandleFunc("GET /hub", view.ShowHub(args, db, store))
mux.HandleFunc("GET /logout", view.Logout(args, store))
mux.HandleFunc("GET /publish-article/{id}", view.PublishArticle(args, db, store))
mux.HandleFunc("GET /publish-issue", view.PublishLatestIssue(args, db, store))
mux.HandleFunc("GET /reject-article/{id}", view.RejectArticle(args, db, store))
mux.HandleFunc("GET /rejected-articles", view.ShowRejectedArticles(args, db, store))
mux.HandleFunc("GET /review-rejected-article/{id}", view.ReviewRejectedArticle(args, db, store))
mux.HandleFunc("GET /review-unpublished-article/{id}", view.ReviewUnpublishedArticle(args, db, store))
mux.HandleFunc("GET /rss", view.ShowRSS(
args,
2024-03-09 10:12:46 +01:00
db,
"Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt",
"https://distrikt-ni-st.de",
"Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität",
))
mux.HandleFunc("GET /this-issue", view.ShowCurrentArticles(args, db))
mux.HandleFunc("GET /unpublished-articles", view.ShowUnpublishedArticles(args, db))
mux.HandleFunc("GET /write-article", view.WriteArticle(args, db))
2024-03-01 11:30:31 +01:00
mux.HandleFunc("POST /add-first-user", view.AddFirstUser(args, db, store))
mux.HandleFunc("POST /add-tag", view.AddTag(args, db, store))
mux.HandleFunc("POST /add-user", view.AddUser(args, db, store))
mux.HandleFunc("POST /login", view.Login(args, db, store))
mux.HandleFunc("POST /resubmit-article/{id}", view.ResubmitArticle(args, db, store))
mux.HandleFunc("POST /submit-article", view.SubmitArticle(args, db, store))
mux.HandleFunc("POST /update-user", view.UpdateUser(args, db, store))
mux.HandleFunc("POST /upload-image", view.UploadImage(args))
log.Fatalln(http.ListenAndServe(args.Port, mux))
2024-02-18 07:20:51 +01:00
}