Read config file in addition to cli arguments

This commit is contained in:
2024-04-12 07:17:13 +02:00
parent 4b90ec9652
commit 0f0471b84c
13 changed files with 200 additions and 132 deletions

View File

@ -17,7 +17,7 @@ import (
"streifling.com/jason/cpolis/cmd/model"
)
func ShowHub(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
func ShowHub(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session, err := s.Get(r, "cookie")
if err != nil {
@ -31,7 +31,7 @@ func ShowHub(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.Hand
}
}
func WriteArticle(c *control.CliArgs, db *model.DB) http.HandlerFunc {
func WriteArticle(c *control.Config, db *model.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
tags, err := db.GetTagList()
if err != nil {
@ -45,7 +45,7 @@ func WriteArticle(c *control.CliArgs, db *model.DB) http.HandlerFunc {
}
}
func SubmitArticle(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
func SubmitArticle(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session, err := s.Get(r, "cookie")
if err != nil {
@ -93,7 +93,7 @@ func SubmitArticle(c *control.CliArgs, db *model.DB, s *control.CookieStore) htt
}
}
func ResubmitArticle(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
func ResubmitArticle(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
@ -147,7 +147,7 @@ func ResubmitArticle(c *control.CliArgs, db *model.DB, s *control.CookieStore) h
}
}
func ShowUnpublishedArticles(c *control.CliArgs, db *model.DB) http.HandlerFunc {
func ShowUnpublishedArticles(c *control.Config, db *model.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
unpublishedArticles, err := db.GetCertainArticles(false, false)
if err != nil {
@ -162,7 +162,7 @@ func ShowUnpublishedArticles(c *control.CliArgs, db *model.DB) http.HandlerFunc
}
}
func ShowRejectedArticles(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
func ShowRejectedArticles(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
type htmlData struct {
MyIDs map[int64]bool
@ -197,7 +197,7 @@ func ShowRejectedArticles(c *control.CliArgs, db *model.DB, s *control.CookieSto
}
}
func ReviewUnpublishedArticle(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
func ReviewUnpublishedArticle(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
type htmlData struct {
Title string
@ -259,7 +259,7 @@ func ReviewUnpublishedArticle(c *control.CliArgs, db *model.DB, s *control.Cooki
}
}
func ReviewRejectedArticle(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
func ReviewRejectedArticle(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
type htmlData struct {
Selected map[int64]bool
@ -306,7 +306,7 @@ func ReviewRejectedArticle(c *control.CliArgs, db *model.DB, s *control.CookieSt
}
}
func PublishArticle(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
func PublishArticle(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
@ -356,7 +356,7 @@ func PublishArticle(c *control.CliArgs, db *model.DB, s *control.CookieStore) ht
}
}
func RejectArticle(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
func RejectArticle(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
@ -386,7 +386,7 @@ func RejectArticle(c *control.CliArgs, db *model.DB, s *control.CookieStore) htt
}
}
func ShowCurrentArticles(c *control.CliArgs, db *model.DB) http.HandlerFunc {
func ShowCurrentArticles(c *control.Config, db *model.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
articles, err := db.GetCurrentIssueArticles()
if err != nil {
@ -400,7 +400,7 @@ func ShowCurrentArticles(c *control.CliArgs, db *model.DB) http.HandlerFunc {
}
}
func UploadImage(c *control.CliArgs) http.HandlerFunc {
func UploadImage(c *control.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
file, header, err := r.FormFile("article-image")
if err != nil {

View File

@ -8,14 +8,14 @@ import (
"streifling.com/jason/cpolis/cmd/model"
)
func CreateTag(c *control.CliArgs) http.HandlerFunc {
func CreateTag(c *control.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-tag.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
}
}
func AddTag(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
func AddTag(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
db.AddTag(r.PostFormValue("tag"))

View File

@ -8,7 +8,7 @@ import (
"streifling.com/jason/cpolis/cmd/control"
)
func ServeImage(c *control.CliArgs, s *control.CookieStore) http.HandlerFunc {
func ServeImage(c *control.Config, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
absFilepath, err := filepath.Abs(c.PicsDir)
if err != nil {

View File

@ -9,7 +9,7 @@ import (
"streifling.com/jason/cpolis/cmd/model"
)
func PublishLatestIssue(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
func PublishLatestIssue(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := db.PublishLatestIssue(); err != nil {
log.Println(err)

View File

@ -27,7 +27,7 @@ func saveSession(w http.ResponseWriter, r *http.Request, s *control.CookieStore,
return nil
}
func HomePage(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
func HomePage(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
numRows, err := db.CountEntries("users")
if err != nil {
@ -54,7 +54,7 @@ func HomePage(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.Han
}
}
func Login(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
func Login(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userName := r.PostFormValue("username")
password := r.PostFormValue("password")
@ -89,7 +89,7 @@ func Login(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.Handle
}
}
func Logout(c *control.CliArgs, s *control.CookieStore) http.HandlerFunc {
func Logout(c *control.Config, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session, err := s.Get(r, "cookie")
if err != nil {

View File

@ -31,14 +31,14 @@ func checkUserStrings(user *model.User) (string, int, bool) {
}
}
func CreateUser(c *control.CliArgs) http.HandlerFunc {
func CreateUser(c *control.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
}
}
func AddUser(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
func AddUser(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
role, err := strconv.Atoi(r.PostFormValue("role"))
if err != nil {
@ -100,7 +100,7 @@ func AddUser(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.Hand
}
}
func EditSelf(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
func EditSelf(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session, err := s.Get(r, "cookie")
if err != nil {
@ -121,7 +121,7 @@ func EditSelf(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.Han
}
}
func UpdateSelf(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
func UpdateSelf(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session, err := s.Get(r, "cookie")
if err != nil {
@ -191,7 +191,7 @@ func UpdateSelf(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.H
}
}
func AddFirstUser(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
func AddFirstUser(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var err error
@ -266,7 +266,7 @@ func AddFirstUser(c *control.CliArgs, db *model.DB, s *control.CookieStore) http
}
}
func ShowAllUsers(c *control.CliArgs, db *model.DB) http.HandlerFunc {
func ShowAllUsers(c *control.Config, db *model.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
users, err := db.GetAllUsers()
if err != nil {
@ -280,7 +280,7 @@ func ShowAllUsers(c *control.CliArgs, db *model.DB) http.HandlerFunc {
}
}
func EditUser(c *control.CliArgs, db *model.DB) http.HandlerFunc {
func EditUser(c *control.Config, db *model.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
@ -301,7 +301,7 @@ func EditUser(c *control.CliArgs, db *model.DB) http.HandlerFunc {
}
}
func UpdateUser(c *control.CliArgs, db *model.DB, s *control.CookieStore) http.HandlerFunc {
func UpdateUser(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {