Change structure of code tor frontend and backend one
This commit is contained in:
parent
21fd3403b2
commit
d0c1e525d2
@ -1,4 +1,4 @@
|
||||
package model
|
||||
package backend
|
||||
|
||||
import (
|
||||
"context"
|
@ -1,4 +1,4 @@
|
||||
package model
|
||||
package backend
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,4 +1,4 @@
|
||||
package control
|
||||
package backend
|
||||
|
||||
import (
|
||||
"flag"
|
@ -1,4 +1,4 @@
|
||||
package model
|
||||
package backend
|
||||
|
||||
import (
|
||||
"bufio"
|
@ -1,4 +1,4 @@
|
||||
package model
|
||||
package backend
|
||||
|
||||
import (
|
||||
"context"
|
@ -1,4 +1,4 @@
|
||||
package control
|
||||
package backend
|
||||
|
||||
import (
|
||||
"bytes"
|
@ -1,4 +1,4 @@
|
||||
package control
|
||||
package backend
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -7,10 +7,9 @@ import (
|
||||
"time"
|
||||
|
||||
"git.streifling.com/jason/rss"
|
||||
"streifling.com/jason/cpolis/cmd/model"
|
||||
)
|
||||
|
||||
func GetChannel(db *model.DB, title, link, description string) (*rss.Channel, error) {
|
||||
func GetChannel(db *DB, title, link, description string) (*rss.Channel, error) {
|
||||
channel := &rss.Channel{
|
||||
Title: title,
|
||||
Link: link,
|
||||
@ -51,7 +50,7 @@ func GetChannel(db *model.DB, title, link, description string) (*rss.Channel, er
|
||||
return channel, nil
|
||||
}
|
||||
|
||||
func GenerateRSS(db *model.DB, title, link, desc string) (*string, error) {
|
||||
func GenerateRSS(db *DB, title, link, desc string) (*string, error) {
|
||||
channel := &rss.Channel{
|
||||
Title: title,
|
||||
Link: link,
|
@ -1,4 +1,4 @@
|
||||
package control
|
||||
package backend
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
@ -1,4 +1,4 @@
|
||||
package model
|
||||
package backend
|
||||
|
||||
import "fmt"
|
||||
|
@ -1,4 +1,4 @@
|
||||
package model
|
||||
package backend
|
||||
|
||||
import (
|
||||
"context"
|
@ -1,4 +1,4 @@
|
||||
package view
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -13,11 +13,10 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"streifling.com/jason/cpolis/cmd/control"
|
||||
"streifling.com/jason/cpolis/cmd/model"
|
||||
"streifling.com/jason/cpolis/cmd/backend"
|
||||
)
|
||||
|
||||
func ShowHub(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
func ShowHub(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := s.Get(r, "cookie")
|
||||
if err != nil {
|
||||
@ -31,7 +30,7 @@ func ShowHub(c *control.Config, db *model.DB, s *control.CookieStore) http.Handl
|
||||
}
|
||||
}
|
||||
|
||||
func WriteArticle(c *control.Config, db *model.DB) http.HandlerFunc {
|
||||
func WriteArticle(c *backend.Config, db *backend.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
tags, err := db.GetTagList()
|
||||
if err != nil {
|
||||
@ -45,7 +44,7 @@ func WriteArticle(c *control.Config, db *model.DB) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func SubmitArticle(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
func SubmitArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := s.Get(r, "cookie")
|
||||
if err != nil {
|
||||
@ -54,7 +53,7 @@ func SubmitArticle(c *control.Config, db *model.DB, s *control.CookieStore) http
|
||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
||||
}
|
||||
|
||||
article := &model.Article{
|
||||
article := &backend.Article{
|
||||
Title: r.PostFormValue("article-title"),
|
||||
Description: r.PostFormValue("article-description"),
|
||||
Content: r.PostFormValue("article-content"),
|
||||
@ -93,7 +92,7 @@ func SubmitArticle(c *control.Config, db *model.DB, s *control.CookieStore) http
|
||||
}
|
||||
}
|
||||
|
||||
func ResubmitArticle(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
func ResubmitArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
@ -107,10 +106,10 @@ func ResubmitArticle(c *control.Config, db *model.DB, s *control.CookieStore) ht
|
||||
content := r.PostFormValue("article-content")
|
||||
|
||||
if err = db.UpdateAttributes(
|
||||
&model.Attribute{Table: "articles", ID: id, AttName: "title", Value: title},
|
||||
&model.Attribute{Table: "articles", ID: id, AttName: "description", Value: description},
|
||||
&model.Attribute{Table: "articles", ID: id, AttName: "content", Value: content},
|
||||
&model.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: false},
|
||||
&backend.Attribute{Table: "articles", ID: id, AttName: "title", Value: title},
|
||||
&backend.Attribute{Table: "articles", ID: id, AttName: "description", Value: description},
|
||||
&backend.Attribute{Table: "articles", ID: id, AttName: "content", Value: content},
|
||||
&backend.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: false},
|
||||
); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -147,7 +146,7 @@ func ResubmitArticle(c *control.Config, db *model.DB, s *control.CookieStore) ht
|
||||
}
|
||||
}
|
||||
|
||||
func ShowUnpublishedArticles(c *control.Config, db *model.DB) http.HandlerFunc {
|
||||
func ShowUnpublishedArticles(c *backend.Config, db *backend.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
unpublishedArticles, err := db.GetCertainArticles(false, false)
|
||||
if err != nil {
|
||||
@ -162,11 +161,11 @@ func ShowUnpublishedArticles(c *control.Config, db *model.DB) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func ShowRejectedArticles(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
func ShowRejectedArticles(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
type htmlData struct {
|
||||
MyIDs map[int64]bool
|
||||
RejectedArticles []*model.Article
|
||||
RejectedArticles []*backend.Article
|
||||
}
|
||||
data := new(htmlData)
|
||||
|
||||
@ -197,13 +196,13 @@ func ShowRejectedArticles(c *control.Config, db *model.DB, s *control.CookieStor
|
||||
}
|
||||
}
|
||||
|
||||
func ReviewUnpublishedArticle(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
func ReviewUnpublishedArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
type htmlData struct {
|
||||
Title string
|
||||
Description string
|
||||
Content template.HTML
|
||||
Tags []*model.Tag
|
||||
Tags []*backend.Tag
|
||||
ID int64
|
||||
}
|
||||
|
||||
@ -224,21 +223,21 @@ func ReviewUnpublishedArticle(c *control.Config, db *model.DB, s *control.Cookie
|
||||
return
|
||||
}
|
||||
|
||||
data.Title, err = control.ConvertToPlain(article.Title)
|
||||
data.Title, err = backend.ConvertToPlain(article.Title)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data.Description, err = control.ConvertToPlain(article.Description)
|
||||
data.Description, err = backend.ConvertToPlain(article.Description)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
content, err := control.ConvertToHTML(article.Content)
|
||||
content, err := backend.ConvertToHTML(article.Content)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -259,12 +258,12 @@ func ReviewUnpublishedArticle(c *control.Config, db *model.DB, s *control.Cookie
|
||||
}
|
||||
}
|
||||
|
||||
func ReviewRejectedArticle(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
func ReviewRejectedArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
type htmlData struct {
|
||||
Selected map[int64]bool
|
||||
Article *model.Article
|
||||
Tags []*model.Tag
|
||||
Article *backend.Article
|
||||
Tags []*backend.Tag
|
||||
}
|
||||
data := new(htmlData)
|
||||
|
||||
@ -306,7 +305,7 @@ func ReviewRejectedArticle(c *control.Config, db *model.DB, s *control.CookieSto
|
||||
}
|
||||
}
|
||||
|
||||
func PublishArticle(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
func PublishArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
@ -329,22 +328,22 @@ func PublishArticle(c *control.Config, db *model.DB, s *control.CookieStore) htt
|
||||
}
|
||||
|
||||
if err = db.UpdateAttributes(
|
||||
&model.Attribute{Table: "articles", ID: id, AttName: "published", Value: true},
|
||||
&model.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: false},
|
||||
&model.Attribute{Table: "articles", ID: id, AttName: "created", Value: time.Now().Format("2006-01-02 15:04:05")},
|
||||
&backend.Attribute{Table: "articles", ID: id, AttName: "published", Value: true},
|
||||
&backend.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: false},
|
||||
&backend.Attribute{Table: "articles", ID: id, AttName: "created", Value: time.Now().Format("2006-01-02 15:04:05")},
|
||||
); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
feed, err := control.GenerateRSS(db, c.Title, c.Link, c.Description)
|
||||
feed, err := backend.GenerateRSS(db, c.Title, c.Link, c.Description)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if err = control.SaveRSS(c.RSSFile, feed); err != nil {
|
||||
if err = backend.SaveRSS(c.RSSFile, feed); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@ -356,7 +355,7 @@ func PublishArticle(c *control.Config, db *model.DB, s *control.CookieStore) htt
|
||||
}
|
||||
}
|
||||
|
||||
func RejectArticle(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
func RejectArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
@ -373,7 +372,7 @@ func RejectArticle(c *control.Config, db *model.DB, s *control.CookieStore) http
|
||||
}
|
||||
|
||||
if err = db.UpdateAttributes(
|
||||
&model.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: true},
|
||||
&backend.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: true},
|
||||
); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -386,7 +385,7 @@ func RejectArticle(c *control.Config, db *model.DB, s *control.CookieStore) http
|
||||
}
|
||||
}
|
||||
|
||||
func ShowCurrentArticles(c *control.Config, db *model.DB) http.HandlerFunc {
|
||||
func ShowCurrentArticles(c *backend.Config, db *backend.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
articles, err := db.GetCurrentIssueArticles()
|
||||
if err != nil {
|
||||
@ -400,7 +399,7 @@ func ShowCurrentArticles(c *control.Config, db *model.DB) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func UploadImage(c *control.Config) http.HandlerFunc {
|
||||
func UploadImage(c *backend.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
file, header, err := r.FormFile("article-image")
|
||||
if err != nil {
|
||||
@ -440,3 +439,56 @@ func UploadImage(c *control.Config) http.HandlerFunc {
|
||||
template.Must(tmpl, err).ExecuteTemplate(w, "editor-images", imgMD)
|
||||
}
|
||||
}
|
||||
|
||||
func PreviewArticle(c *backend.Config, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
type htmlData struct {
|
||||
Title string
|
||||
Description string
|
||||
Content template.HTML
|
||||
}
|
||||
|
||||
var err error
|
||||
data := new(htmlData)
|
||||
|
||||
data.Title, err = backend.ConvertToPlain(r.PostFormValue("article-title"))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data.Description, err = backend.ConvertToPlain(r.PostFormValue("article-description"))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
content, err := backend.ConvertToHTML(r.PostFormValue("article-content"))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data.Content = template.HTML(content)
|
||||
|
||||
session, err := s.Get(r, "cookie")
|
||||
if err != nil {
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
||||
}
|
||||
|
||||
session.Values["article"] = data
|
||||
if err = session.Save(r, w); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
tmpl.ExecuteTemplate(w, "preview", data)
|
||||
}
|
||||
}
|
@ -1,21 +1,20 @@
|
||||
package view
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
"streifling.com/jason/cpolis/cmd/control"
|
||||
"streifling.com/jason/cpolis/cmd/model"
|
||||
"streifling.com/jason/cpolis/cmd/backend"
|
||||
)
|
||||
|
||||
func CreateTag(c *control.Config) http.HandlerFunc {
|
||||
func CreateTag(c *backend.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.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
func AddTag(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
db.AddTag(r.PostFormValue("tag"))
|
||||
|
@ -1,14 +1,14 @@
|
||||
package view
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"streifling.com/jason/cpolis/cmd/control"
|
||||
"streifling.com/jason/cpolis/cmd/backend"
|
||||
)
|
||||
|
||||
func ServeImage(c *control.Config, s *control.CookieStore) http.HandlerFunc {
|
||||
func ServeImage(c *backend.Config, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
absFilepath, err := filepath.Abs(c.PicsDir)
|
||||
if err != nil {
|
@ -1,15 +1,14 @@
|
||||
package view
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"streifling.com/jason/cpolis/cmd/control"
|
||||
"streifling.com/jason/cpolis/cmd/model"
|
||||
"streifling.com/jason/cpolis/cmd/backend"
|
||||
)
|
||||
|
||||
func PublishLatestIssue(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
func PublishLatestIssue(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := db.PublishLatestIssue(); err != nil {
|
||||
log.Println(err)
|
@ -1,4 +1,4 @@
|
||||
package view
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -6,11 +6,10 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"streifling.com/jason/cpolis/cmd/control"
|
||||
"streifling.com/jason/cpolis/cmd/model"
|
||||
"streifling.com/jason/cpolis/cmd/backend"
|
||||
)
|
||||
|
||||
func saveSession(w http.ResponseWriter, r *http.Request, s *control.CookieStore, u *model.User) error {
|
||||
func saveSession(w http.ResponseWriter, r *http.Request, s *backend.CookieStore, u *backend.User) error {
|
||||
session, err := s.Get(r, "cookie")
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting session: %v", err)
|
||||
@ -27,7 +26,7 @@ func saveSession(w http.ResponseWriter, r *http.Request, s *control.CookieStore,
|
||||
return nil
|
||||
}
|
||||
|
||||
func HomePage(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
func HomePage(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
numRows, err := db.CountEntries("users")
|
||||
if err != nil {
|
||||
@ -54,7 +53,7 @@ func HomePage(c *control.Config, db *model.DB, s *control.CookieStore) http.Hand
|
||||
}
|
||||
}
|
||||
|
||||
func Login(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
func Login(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
userName := r.PostFormValue("username")
|
||||
password := r.PostFormValue("password")
|
||||
@ -89,7 +88,7 @@ func Login(c *control.Config, db *model.DB, s *control.CookieStore) http.Handler
|
||||
}
|
||||
}
|
||||
|
||||
func Logout(c *control.Config, s *control.CookieStore) http.HandlerFunc {
|
||||
func Logout(c *backend.Config, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := s.Get(r, "cookie")
|
||||
if err != nil {
|
@ -1,4 +1,4 @@
|
||||
package view
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -7,16 +7,15 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"streifling.com/jason/cpolis/cmd/control"
|
||||
"streifling.com/jason/cpolis/cmd/model"
|
||||
"streifling.com/jason/cpolis/cmd/backend"
|
||||
)
|
||||
|
||||
type UserData struct {
|
||||
*model.User
|
||||
*backend.User
|
||||
Msg string
|
||||
}
|
||||
|
||||
func checkUserStrings(user *model.User) (string, int, bool) {
|
||||
func checkUserStrings(user *backend.User) (string, int, bool) {
|
||||
userLen := 15
|
||||
nameLen := 50
|
||||
|
||||
@ -31,14 +30,14 @@ func checkUserStrings(user *model.User) (string, int, bool) {
|
||||
}
|
||||
}
|
||||
|
||||
func CreateUser(c *control.Config) http.HandlerFunc {
|
||||
func CreateUser(c *backend.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.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
func AddUser(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
role, err := strconv.Atoi(r.PostFormValue("role"))
|
||||
if err != nil {
|
||||
@ -48,7 +47,7 @@ func AddUser(c *control.Config, db *model.DB, s *control.CookieStore) http.Handl
|
||||
}
|
||||
|
||||
htmlData := UserData{
|
||||
User: &model.User{
|
||||
User: &backend.User{
|
||||
UserName: r.PostFormValue("username"),
|
||||
FirstName: r.PostFormValue("first-name"),
|
||||
LastName: r.PostFormValue("last-name"),
|
||||
@ -108,7 +107,7 @@ func AddUser(c *control.Config, db *model.DB, s *control.CookieStore) http.Handl
|
||||
}
|
||||
}
|
||||
|
||||
func EditSelf(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
func EditSelf(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := s.Get(r, "cookie")
|
||||
if err != nil {
|
||||
@ -129,7 +128,7 @@ func EditSelf(c *control.Config, db *model.DB, s *control.CookieStore) http.Hand
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateSelf(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
func UpdateSelf(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := s.Get(r, "cookie")
|
||||
if err != nil {
|
||||
@ -139,7 +138,7 @@ func UpdateSelf(c *control.Config, db *model.DB, s *control.CookieStore) http.Ha
|
||||
}
|
||||
|
||||
userData := UserData{
|
||||
User: &model.User{
|
||||
User: &backend.User{
|
||||
ID: session.Values["id"].(int64),
|
||||
UserName: r.PostFormValue("username"),
|
||||
FirstName: r.PostFormValue("first-name"),
|
||||
@ -199,16 +198,16 @@ func UpdateSelf(c *control.Config, db *model.DB, s *control.CookieStore) http.Ha
|
||||
}
|
||||
}
|
||||
|
||||
func AddFirstUser(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
func AddFirstUser(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
|
||||
htmlData := UserData{
|
||||
User: &model.User{
|
||||
User: &backend.User{
|
||||
UserName: r.PostFormValue("username"),
|
||||
FirstName: r.PostFormValue("first-name"),
|
||||
LastName: r.PostFormValue("last-name"),
|
||||
Role: model.Admin,
|
||||
Role: backend.Admin,
|
||||
},
|
||||
}
|
||||
pass := r.PostFormValue("password")
|
||||
@ -274,11 +273,11 @@ func AddFirstUser(c *control.Config, db *model.DB, s *control.CookieStore) http.
|
||||
}
|
||||
}
|
||||
|
||||
func ShowAllUsers(c *control.Config, db *model.DB, s *control.CookieStore, action string) http.HandlerFunc {
|
||||
func ShowAllUsers(c *backend.Config, db *backend.DB, s *backend.CookieStore, action string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
type htmlData struct {
|
||||
Users map[int64]*model.User
|
||||
Users map[int64]*backend.User
|
||||
Action string
|
||||
}
|
||||
|
||||
@ -303,7 +302,7 @@ func ShowAllUsers(c *control.Config, db *model.DB, s *control.CookieStore, actio
|
||||
}
|
||||
}
|
||||
|
||||
func EditUser(c *control.Config, db *model.DB) http.HandlerFunc {
|
||||
func EditUser(c *backend.Config, db *backend.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
@ -324,7 +323,7 @@ func EditUser(c *control.Config, db *model.DB) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateUser(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
func UpdateUser(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
@ -341,7 +340,7 @@ func UpdateUser(c *control.Config, db *model.DB, s *control.CookieStore) http.Ha
|
||||
}
|
||||
|
||||
userData := UserData{
|
||||
User: &model.User{
|
||||
User: &backend.User{
|
||||
ID: id,
|
||||
UserName: r.PostFormValue("username"),
|
||||
FirstName: r.PostFormValue("first-name"),
|
||||
@ -408,7 +407,7 @@ func UpdateUser(c *control.Config, db *model.DB, s *control.CookieStore) http.Ha
|
||||
}
|
||||
}
|
||||
|
||||
func DeleteUser(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
func DeleteUser(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
78
cmd/main.go
78
cmd/main.go
@ -6,17 +6,16 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"streifling.com/jason/cpolis/cmd/control"
|
||||
"streifling.com/jason/cpolis/cmd/model"
|
||||
"streifling.com/jason/cpolis/cmd/view"
|
||||
"streifling.com/jason/cpolis/cmd/backend"
|
||||
"streifling.com/jason/cpolis/cmd/frontend"
|
||||
)
|
||||
|
||||
func init() {
|
||||
gob.Register(model.User{})
|
||||
gob.Register(backend.User{})
|
||||
}
|
||||
|
||||
func main() {
|
||||
config, err := control.HandleConfig()
|
||||
config, err := backend.HandleConfig()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
@ -28,59 +27,60 @@ func main() {
|
||||
defer logFile.Close()
|
||||
log.SetOutput(logFile)
|
||||
|
||||
db, err := model.OpenDB(config.DBName)
|
||||
db, err := backend.OpenDB(config.DBName)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
key, err := control.LoadKey(config.KeyFile)
|
||||
key, err := backend.LoadKey(config.KeyFile)
|
||||
if err != nil {
|
||||
key, err = control.NewKey()
|
||||
key, err = backend.NewKey()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
control.SaveKey(key, config.KeyFile)
|
||||
backend.SaveKey(key, config.KeyFile)
|
||||
}
|
||||
store := control.NewCookieStore(key)
|
||||
store := backend.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("/", frontend.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 /delete-user/{id}", view.DeleteUser(config, db, store))
|
||||
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 /create-tag", frontend.CreateTag(config))
|
||||
mux.HandleFunc("GET /create-user", frontend.CreateUser(config))
|
||||
mux.HandleFunc("GET /edit-self", frontend.EditSelf(config, db, store))
|
||||
mux.HandleFunc("GET /edit-user/{id}", frontend.EditUser(config, db))
|
||||
mux.HandleFunc("GET /delete-user/{id}", frontend.DeleteUser(config, db, store))
|
||||
mux.HandleFunc("GET /hub", frontend.ShowHub(config, db, store))
|
||||
mux.HandleFunc("GET /logout", frontend.Logout(config, store))
|
||||
mux.HandleFunc("GET /pics/{pic}", frontend.ServeImage(config, store))
|
||||
mux.HandleFunc("GET /publish-article/{id}", frontend.PublishArticle(config, db, store))
|
||||
mux.HandleFunc("GET /publish-issue", frontend.PublishLatestIssue(config, db, store))
|
||||
mux.HandleFunc("GET /reject-article/{id}", frontend.RejectArticle(config, db, store))
|
||||
mux.HandleFunc("GET /rejected-articles", frontend.ShowRejectedArticles(config, db, store))
|
||||
mux.HandleFunc("GET /review-rejected-article/{id}", frontend.ReviewRejectedArticle(config, db, store))
|
||||
mux.HandleFunc("GET /review-unpublished-article/{id}", frontend.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-edit", view.ShowAllUsers(config, db, store, "edit-user"))
|
||||
mux.HandleFunc("GET /show-all-users-delete", view.ShowAllUsers(config, db, store, "delete-user"))
|
||||
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("GET /show-all-users-edit", frontend.ShowAllUsers(config, db, store, "edit-user"))
|
||||
mux.HandleFunc("GET /show-all-users-delete", frontend.ShowAllUsers(config, db, store, "delete-user"))
|
||||
mux.HandleFunc("GET /this-issue", frontend.ShowCurrentArticles(config, db))
|
||||
mux.HandleFunc("GET /unpublished-articles", frontend.ShowUnpublishedArticles(config, db))
|
||||
mux.HandleFunc("GET /write-article", frontend.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))
|
||||
mux.HandleFunc("POST /add-first-user", frontend.AddFirstUser(config, db, store))
|
||||
mux.HandleFunc("POST /add-tag", frontend.AddTag(config, db, store))
|
||||
mux.HandleFunc("POST /add-user", frontend.AddUser(config, db, store))
|
||||
mux.HandleFunc("POST /login", frontend.Login(config, db, store))
|
||||
mux.HandleFunc("POST /preview-article", frontend.PreviewArticle(config, store))
|
||||
mux.HandleFunc("POST /resubmit-article/{id}", frontend.ResubmitArticle(config, db, store))
|
||||
mux.HandleFunc("POST /submit-article", frontend.SubmitArticle(config, db, store))
|
||||
mux.HandleFunc("POST /update-self", frontend.UpdateSelf(config, db, store))
|
||||
mux.HandleFunc("POST /update-user/{id}", frontend.UpdateUser(config, db, store))
|
||||
mux.HandleFunc("POST /upload-image", frontend.UploadImage(config))
|
||||
|
||||
log.Fatalln(http.ListenAndServe(config.Port, mux))
|
||||
}
|
||||
|
@ -1,7 +1,12 @@
|
||||
{{define "page-content"}}
|
||||
<h2>Editor</h2>
|
||||
|
||||
<form>
|
||||
<form id="edit-area">
|
||||
<div class="btn-area">
|
||||
<button class="btn" disabled hx-get="/hub" hx-target="#edit-area">Schreiben</button>
|
||||
<button class="btn" hx-post="/preview-article" hx-target="#edit-area">Vorschau</button>
|
||||
</div>
|
||||
|
||||
<div class="flex flex-col gap-y-1">
|
||||
<label for="article-title">Titel</label>
|
||||
<input name="article-title" type="text" />
|
||||
@ -55,7 +60,6 @@
|
||||
|
||||
document.body.removeChild(textarea);
|
||||
}
|
||||
|
||||
</script>
|
||||
{{end}}
|
||||
|
||||
@ -68,3 +72,23 @@
|
||||
</div>
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
|
||||
{{define "preview"}}
|
||||
<span>Titel</span>
|
||||
<div class="bg-white border mb-3 px-2 py-2 rounded-md w-full">
|
||||
{{.Title}}
|
||||
</div>
|
||||
|
||||
<span>Beschreibung</span>
|
||||
<div class="bg-white border mb-3 px-2 py-2 rounded-md w-full">
|
||||
{{.Description}}
|
||||
</div>
|
||||
|
||||
<span>Artikel</span>
|
||||
<div class="bg-white border mb-3 px-2 py-2 rounded-md w-full">
|
||||
<div class="prose">
|
||||
{{.Content}}
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
|
Loading…
x
Reference in New Issue
Block a user