Shorten lines by referencing frontend as f and backend as b
This commit is contained in:
parent
081e880fb6
commit
b2db128aa9
@ -13,10 +13,10 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"streifling.com/jason/cpolis/cmd/backend"
|
b "streifling.com/jason/cpolis/cmd/backend"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ShowHub(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
func ShowHub(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
session, err := s.Get(r, "cookie")
|
session, err := s.Get(r, "cookie")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -30,7 +30,7 @@ func ShowHub(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.Han
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteArticle(c *backend.Config, db *backend.DB) http.HandlerFunc {
|
func WriteArticle(c *b.Config, db *b.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
tags, err := db.GetTagList()
|
tags, err := db.GetTagList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -44,7 +44,7 @@ func WriteArticle(c *backend.Config, db *backend.DB) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SubmitArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
func SubmitArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
session, err := s.Get(r, "cookie")
|
session, err := s.Get(r, "cookie")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -53,7 +53,7 @@ func SubmitArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) ht
|
|||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
article := &backend.Article{
|
article := &b.Article{
|
||||||
Title: r.PostFormValue("article-title"),
|
Title: r.PostFormValue("article-title"),
|
||||||
Description: r.PostFormValue("article-description"),
|
Description: r.PostFormValue("article-description"),
|
||||||
Content: r.PostFormValue("article-content"),
|
Content: r.PostFormValue("article-content"),
|
||||||
@ -92,7 +92,7 @@ func SubmitArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) ht
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResubmitArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
func ResubmitArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -106,10 +106,10 @@ func ResubmitArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore)
|
|||||||
content := r.PostFormValue("article-content")
|
content := r.PostFormValue("article-content")
|
||||||
|
|
||||||
if err = db.UpdateAttributes(
|
if err = db.UpdateAttributes(
|
||||||
&backend.Attribute{Table: "articles", ID: id, AttName: "title", Value: title},
|
&b.Attribute{Table: "articles", ID: id, AttName: "title", Value: title},
|
||||||
&backend.Attribute{Table: "articles", ID: id, AttName: "description", Value: description},
|
&b.Attribute{Table: "articles", ID: id, AttName: "description", Value: description},
|
||||||
&backend.Attribute{Table: "articles", ID: id, AttName: "content", Value: content},
|
&b.Attribute{Table: "articles", ID: id, AttName: "content", Value: content},
|
||||||
&backend.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: false},
|
&b.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: false},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
@ -146,7 +146,7 @@ func ResubmitArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ShowUnpublishedArticles(c *backend.Config, db *backend.DB) http.HandlerFunc {
|
func ShowUnpublishedArticles(c *b.Config, db *b.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
unpublishedArticles, err := db.GetCertainArticles(false, false)
|
unpublishedArticles, err := db.GetCertainArticles(false, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -161,11 +161,11 @@ func ShowUnpublishedArticles(c *backend.Config, db *backend.DB) http.HandlerFunc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ShowRejectedArticles(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
func ShowRejectedArticles(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
type htmlData struct {
|
type htmlData struct {
|
||||||
MyIDs map[int64]bool
|
MyIDs map[int64]bool
|
||||||
RejectedArticles []*backend.Article
|
RejectedArticles []*b.Article
|
||||||
}
|
}
|
||||||
data := new(htmlData)
|
data := new(htmlData)
|
||||||
|
|
||||||
@ -196,13 +196,13 @@ func ShowRejectedArticles(c *backend.Config, db *backend.DB, s *backend.CookieSt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReviewUnpublishedArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
func ReviewUnpublishedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
type htmlData struct {
|
type htmlData struct {
|
||||||
Title string
|
Title string
|
||||||
Description string
|
Description string
|
||||||
Content template.HTML
|
Content template.HTML
|
||||||
Tags []*backend.Tag
|
Tags []*b.Tag
|
||||||
ID int64
|
ID int64
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -223,21 +223,21 @@ func ReviewUnpublishedArticle(c *backend.Config, db *backend.DB, s *backend.Cook
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data.Title, err = backend.ConvertToPlain(article.Title)
|
data.Title, err = b.ConvertToPlain(article.Title)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data.Description, err = backend.ConvertToPlain(article.Description)
|
data.Description, err = b.ConvertToPlain(article.Description)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := backend.ConvertToHTML(article.Content)
|
content, err := b.ConvertToHTML(article.Content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
@ -258,12 +258,12 @@ func ReviewUnpublishedArticle(c *backend.Config, db *backend.DB, s *backend.Cook
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReviewRejectedArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
func ReviewRejectedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
type htmlData struct {
|
type htmlData struct {
|
||||||
Selected map[int64]bool
|
Selected map[int64]bool
|
||||||
Article *backend.Article
|
Article *b.Article
|
||||||
Tags []*backend.Tag
|
Tags []*b.Tag
|
||||||
}
|
}
|
||||||
data := new(htmlData)
|
data := new(htmlData)
|
||||||
|
|
||||||
@ -305,7 +305,7 @@ func ReviewRejectedArticle(c *backend.Config, db *backend.DB, s *backend.CookieS
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func PublishArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
func PublishArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -328,22 +328,22 @@ func PublishArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) h
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err = db.UpdateAttributes(
|
if err = db.UpdateAttributes(
|
||||||
&backend.Attribute{Table: "articles", ID: id, AttName: "published", Value: true},
|
&b.Attribute{Table: "articles", ID: id, AttName: "published", Value: true},
|
||||||
&backend.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: false},
|
&b.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")},
|
&b.Attribute{Table: "articles", ID: id, AttName: "created", Value: time.Now().Format("2006-01-02 15:04:05")},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
feed, err := backend.GenerateRSS(db, c.Title, c.Link, c.Description)
|
feed, err := b.GenerateRSS(db, c.Title, c.Link, c.Description)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err = backend.SaveRSS(c.RSSFile, feed); err != nil {
|
if err = b.SaveRSS(c.RSSFile, feed); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@ -355,7 +355,7 @@ func PublishArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) h
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RejectArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
func RejectArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -372,7 +372,7 @@ func RejectArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) ht
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err = db.UpdateAttributes(
|
if err = db.UpdateAttributes(
|
||||||
&backend.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: true},
|
&b.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: true},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
@ -385,7 +385,7 @@ func RejectArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) ht
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ShowCurrentArticles(c *backend.Config, db *backend.DB) http.HandlerFunc {
|
func ShowCurrentArticles(c *b.Config, db *b.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
articles, err := db.GetCurrentIssueArticles()
|
articles, err := db.GetCurrentIssueArticles()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -399,7 +399,7 @@ func ShowCurrentArticles(c *backend.Config, db *backend.DB) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func UploadImage(c *backend.Config) http.HandlerFunc {
|
func UploadImage(c *b.Config) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
file, header, err := r.FormFile("article-image")
|
file, header, err := r.FormFile("article-image")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -440,7 +440,7 @@ func UploadImage(c *backend.Config) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func PreviewArticle(c *backend.Config, s *backend.CookieStore) http.HandlerFunc {
|
func PreviewArticle(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
type htmlData struct {
|
type htmlData struct {
|
||||||
Title string
|
Title string
|
||||||
@ -451,21 +451,21 @@ func PreviewArticle(c *backend.Config, s *backend.CookieStore) http.HandlerFunc
|
|||||||
var err error
|
var err error
|
||||||
data := new(htmlData)
|
data := new(htmlData)
|
||||||
|
|
||||||
data.Title, err = backend.ConvertToPlain(r.PostFormValue("article-title"))
|
data.Title, err = b.ConvertToPlain(r.PostFormValue("article-title"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data.Description, err = backend.ConvertToPlain(r.PostFormValue("article-description"))
|
data.Description, err = b.ConvertToPlain(r.PostFormValue("article-description"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := backend.ConvertToHTML(r.PostFormValue("article-content"))
|
content, err := b.ConvertToHTML(r.PostFormValue("article-content"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
@ -4,17 +4,17 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"streifling.com/jason/cpolis/cmd/backend"
|
b "streifling.com/jason/cpolis/cmd/backend"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateTag(c *backend.Config) http.HandlerFunc {
|
func CreateTag(c *b.Config) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-tag.html")
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-tag.html")
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddTag(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
func AddTag(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
db.AddTag(r.PostFormValue("tag"))
|
db.AddTag(r.PostFormValue("tag"))
|
||||||
|
|
||||||
|
@ -5,10 +5,10 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"streifling.com/jason/cpolis/cmd/backend"
|
b "streifling.com/jason/cpolis/cmd/backend"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ServeImage(c *backend.Config, s *backend.CookieStore) http.HandlerFunc {
|
func ServeImage(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
absFilepath, err := filepath.Abs(c.PicsDir)
|
absFilepath, err := filepath.Abs(c.PicsDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -5,10 +5,10 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"streifling.com/jason/cpolis/cmd/backend"
|
b "streifling.com/jason/cpolis/cmd/backend"
|
||||||
)
|
)
|
||||||
|
|
||||||
func PublishLatestIssue(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
func PublishLatestIssue(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if err := db.PublishLatestIssue(); err != nil {
|
if err := db.PublishLatestIssue(); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
@ -6,10 +6,10 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"streifling.com/jason/cpolis/cmd/backend"
|
b "streifling.com/jason/cpolis/cmd/backend"
|
||||||
)
|
)
|
||||||
|
|
||||||
func saveSession(w http.ResponseWriter, r *http.Request, s *backend.CookieStore, u *backend.User) error {
|
func saveSession(w http.ResponseWriter, r *http.Request, s *b.CookieStore, u *b.User) error {
|
||||||
session, err := s.Get(r, "cookie")
|
session, err := s.Get(r, "cookie")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error getting session: %v", err)
|
return fmt.Errorf("error getting session: %v", err)
|
||||||
@ -26,7 +26,7 @@ func saveSession(w http.ResponseWriter, r *http.Request, s *backend.CookieStore,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func HomePage(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
func HomePage(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
numRows, err := db.CountEntries("users")
|
numRows, err := db.CountEntries("users")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -53,7 +53,7 @@ func HomePage(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.Ha
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Login(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
func Login(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userName := r.PostFormValue("username")
|
userName := r.PostFormValue("username")
|
||||||
password := r.PostFormValue("password")
|
password := r.PostFormValue("password")
|
||||||
@ -88,7 +88,7 @@ func Login(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.Handl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Logout(c *backend.Config, s *backend.CookieStore) http.HandlerFunc {
|
func Logout(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
session, err := s.Get(r, "cookie")
|
session, err := s.Get(r, "cookie")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -7,15 +7,15 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"streifling.com/jason/cpolis/cmd/backend"
|
b "streifling.com/jason/cpolis/cmd/backend"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserData struct {
|
type UserData struct {
|
||||||
*backend.User
|
*b.User
|
||||||
Msg string
|
Msg string
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkUserStrings(user *backend.User) (string, int, bool) {
|
func checkUserStrings(user *b.User) (string, int, bool) {
|
||||||
userLen := 15
|
userLen := 15
|
||||||
nameLen := 50
|
nameLen := 50
|
||||||
|
|
||||||
@ -30,14 +30,14 @@ func checkUserStrings(user *backend.User) (string, int, bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateUser(c *backend.Config) http.HandlerFunc {
|
func CreateUser(c *b.Config) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddUser(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
func AddUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
role, err := strconv.Atoi(r.PostFormValue("role"))
|
role, err := strconv.Atoi(r.PostFormValue("role"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -47,7 +47,7 @@ func AddUser(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.Han
|
|||||||
}
|
}
|
||||||
|
|
||||||
htmlData := UserData{
|
htmlData := UserData{
|
||||||
User: &backend.User{
|
User: &b.User{
|
||||||
UserName: r.PostFormValue("username"),
|
UserName: r.PostFormValue("username"),
|
||||||
FirstName: r.PostFormValue("first-name"),
|
FirstName: r.PostFormValue("first-name"),
|
||||||
LastName: r.PostFormValue("last-name"),
|
LastName: r.PostFormValue("last-name"),
|
||||||
@ -107,7 +107,7 @@ func AddUser(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.Han
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func EditSelf(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
func EditSelf(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
session, err := s.Get(r, "cookie")
|
session, err := s.Get(r, "cookie")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -128,7 +128,7 @@ func EditSelf(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.Ha
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateSelf(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
func UpdateSelf(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
session, err := s.Get(r, "cookie")
|
session, err := s.Get(r, "cookie")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -138,7 +138,7 @@ func UpdateSelf(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.
|
|||||||
}
|
}
|
||||||
|
|
||||||
userData := UserData{
|
userData := UserData{
|
||||||
User: &backend.User{
|
User: &b.User{
|
||||||
ID: session.Values["id"].(int64),
|
ID: session.Values["id"].(int64),
|
||||||
UserName: r.PostFormValue("username"),
|
UserName: r.PostFormValue("username"),
|
||||||
FirstName: r.PostFormValue("first-name"),
|
FirstName: r.PostFormValue("first-name"),
|
||||||
@ -198,16 +198,16 @@ func UpdateSelf(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddFirstUser(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
func AddFirstUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
htmlData := UserData{
|
htmlData := UserData{
|
||||||
User: &backend.User{
|
User: &b.User{
|
||||||
UserName: r.PostFormValue("username"),
|
UserName: r.PostFormValue("username"),
|
||||||
FirstName: r.PostFormValue("first-name"),
|
FirstName: r.PostFormValue("first-name"),
|
||||||
LastName: r.PostFormValue("last-name"),
|
LastName: r.PostFormValue("last-name"),
|
||||||
Role: backend.Admin,
|
Role: b.Admin,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
pass := r.PostFormValue("password")
|
pass := r.PostFormValue("password")
|
||||||
@ -273,11 +273,11 @@ func AddFirstUser(c *backend.Config, db *backend.DB, s *backend.CookieStore) htt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ShowAllUsers(c *backend.Config, db *backend.DB, s *backend.CookieStore, action string) http.HandlerFunc {
|
func ShowAllUsers(c *b.Config, db *b.DB, s *b.CookieStore, action string) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var err error
|
var err error
|
||||||
type htmlData struct {
|
type htmlData struct {
|
||||||
Users map[int64]*backend.User
|
Users map[int64]*b.User
|
||||||
Action string
|
Action string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -302,7 +302,7 @@ func ShowAllUsers(c *backend.Config, db *backend.DB, s *backend.CookieStore, act
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func EditUser(c *backend.Config, db *backend.DB) http.HandlerFunc {
|
func EditUser(c *b.Config, db *b.DB) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -323,7 +323,7 @@ func EditUser(c *backend.Config, db *backend.DB) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateUser(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
func UpdateUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -340,7 +340,7 @@ func UpdateUser(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.
|
|||||||
}
|
}
|
||||||
|
|
||||||
userData := UserData{
|
userData := UserData{
|
||||||
User: &backend.User{
|
User: &b.User{
|
||||||
ID: id,
|
ID: id,
|
||||||
UserName: r.PostFormValue("username"),
|
UserName: r.PostFormValue("username"),
|
||||||
FirstName: r.PostFormValue("first-name"),
|
FirstName: r.PostFormValue("first-name"),
|
||||||
@ -407,7 +407,7 @@ func UpdateUser(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteUser(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
func DeleteUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
82
cmd/main.go
82
cmd/main.go
@ -6,16 +6,16 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"streifling.com/jason/cpolis/cmd/backend"
|
b "streifling.com/jason/cpolis/cmd/backend"
|
||||||
"streifling.com/jason/cpolis/cmd/frontend"
|
f "streifling.com/jason/cpolis/cmd/frontend"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
gob.Register(backend.User{})
|
gob.Register(b.User{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config, err := backend.HandleConfig()
|
config, err := b.HandleConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
@ -27,60 +27,58 @@ func main() {
|
|||||||
defer logFile.Close()
|
defer logFile.Close()
|
||||||
log.SetOutput(logFile)
|
log.SetOutput(logFile)
|
||||||
|
|
||||||
db, err := backend.OpenDB(config.DBName)
|
db, err := b.OpenDB(config.DBName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
key, err := backend.LoadKey(config.KeyFile)
|
key, err := b.LoadKey(config.KeyFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
key, err = backend.NewKey()
|
key, err = b.NewKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
backend.SaveKey(key, config.KeyFile)
|
b.SaveKey(key, config.KeyFile)
|
||||||
}
|
}
|
||||||
store := backend.NewCookieStore(key)
|
store := b.NewCookieStore(key)
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.Handle("/web/static/", http.StripPrefix("/web/static/",
|
mux.Handle("/web/static/", http.StripPrefix("/web/static/",
|
||||||
http.FileServer(http.Dir(config.WebDir+"/static/"))))
|
http.FileServer(http.Dir(config.WebDir+"/static/"))))
|
||||||
mux.HandleFunc("/", frontend.HomePage(config, db, store))
|
mux.HandleFunc("/", f.HomePage(config, db, store))
|
||||||
|
|
||||||
mux.HandleFunc("GET /create-tag", frontend.CreateTag(config))
|
mux.HandleFunc("GET /create-tag", f.CreateTag(config))
|
||||||
mux.HandleFunc("GET /create-user", frontend.CreateUser(config))
|
mux.HandleFunc("GET /create-user", f.CreateUser(config))
|
||||||
mux.HandleFunc("GET /edit-self", frontend.EditSelf(config, db, store))
|
mux.HandleFunc("GET /edit-self", f.EditSelf(config, db, store))
|
||||||
mux.HandleFunc("GET /edit-user/{id}", frontend.EditUser(config, db))
|
mux.HandleFunc("GET /edit-user/{id}", f.EditUser(config, db))
|
||||||
mux.HandleFunc("GET /delete-user/{id}", frontend.DeleteUser(config, db, store))
|
mux.HandleFunc("GET /delete-user/{id}", f.DeleteUser(config, db, store))
|
||||||
mux.HandleFunc("GET /hub", frontend.ShowHub(config, db, store))
|
mux.HandleFunc("GET /hub", f.ShowHub(config, db, store))
|
||||||
mux.HandleFunc("GET /logout", frontend.Logout(config, store))
|
mux.HandleFunc("GET /logout", f.Logout(config, store))
|
||||||
mux.HandleFunc("GET /pics/{pic}", frontend.ServeImage(config, store))
|
mux.HandleFunc("GET /pics/{pic}", f.ServeImage(config, store))
|
||||||
mux.HandleFunc("GET /publish-article/{id}", frontend.PublishArticle(config, db, store))
|
mux.HandleFunc("GET /publish-article/{id}", f.PublishArticle(config, db, store))
|
||||||
mux.HandleFunc("GET /publish-issue", frontend.PublishLatestIssue(config, db, store))
|
mux.HandleFunc("GET /publish-issue", f.PublishLatestIssue(config, db, store))
|
||||||
mux.HandleFunc("GET /reject-article/{id}", frontend.RejectArticle(config, db, store))
|
mux.HandleFunc("GET /reject-article/{id}", f.RejectArticle(config, db, store))
|
||||||
mux.HandleFunc("GET /rejected-articles", frontend.ShowRejectedArticles(config, db, store))
|
mux.HandleFunc("GET /rejected-articles", f.ShowRejectedArticles(config, db, store))
|
||||||
mux.HandleFunc("GET /review-rejected-article/{id}", frontend.ReviewRejectedArticle(config, db, store))
|
mux.HandleFunc("GET /review-rejected-article/{id}", f.ReviewRejectedArticle(config, db, store))
|
||||||
mux.HandleFunc("GET /review-unpublished-article/{id}", frontend.ReviewUnpublishedArticle(config, db, store))
|
mux.HandleFunc("GET /review-unpublished-article/{id}", f.ReviewUnpublishedArticle(config, db, store))
|
||||||
mux.HandleFunc("GET /rss", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("GET /rss", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, config.RSSFile) })
|
||||||
http.ServeFile(w, r, config.RSSFile)
|
mux.HandleFunc("GET /show-all-users-edit", f.ShowAllUsers(config, db, store, "edit-user"))
|
||||||
})
|
mux.HandleFunc("GET /show-all-users-delete", f.ShowAllUsers(config, db, store, "delete-user"))
|
||||||
mux.HandleFunc("GET /show-all-users-edit", frontend.ShowAllUsers(config, db, store, "edit-user"))
|
mux.HandleFunc("GET /this-issue", f.ShowCurrentArticles(config, db))
|
||||||
mux.HandleFunc("GET /show-all-users-delete", frontend.ShowAllUsers(config, db, store, "delete-user"))
|
mux.HandleFunc("GET /unpublished-articles", f.ShowUnpublishedArticles(config, db))
|
||||||
mux.HandleFunc("GET /this-issue", frontend.ShowCurrentArticles(config, db))
|
mux.HandleFunc("GET /write-article", f.WriteArticle(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", frontend.AddFirstUser(config, db, store))
|
mux.HandleFunc("POST /add-first-user", f.AddFirstUser(config, db, store))
|
||||||
mux.HandleFunc("POST /add-tag", frontend.AddTag(config, db, store))
|
mux.HandleFunc("POST /add-tag", f.AddTag(config, db, store))
|
||||||
mux.HandleFunc("POST /add-user", frontend.AddUser(config, db, store))
|
mux.HandleFunc("POST /add-user", f.AddUser(config, db, store))
|
||||||
mux.HandleFunc("POST /login", frontend.Login(config, db, store))
|
mux.HandleFunc("POST /login", f.Login(config, db, store))
|
||||||
mux.HandleFunc("POST /preview-article", frontend.PreviewArticle(config, store))
|
mux.HandleFunc("POST /preview-article", f.PreviewArticle(config, store))
|
||||||
mux.HandleFunc("POST /resubmit-article/{id}", frontend.ResubmitArticle(config, db, store))
|
mux.HandleFunc("POST /resubmit-article/{id}", f.ResubmitArticle(config, db, store))
|
||||||
mux.HandleFunc("POST /submit-article", frontend.SubmitArticle(config, db, store))
|
mux.HandleFunc("POST /submit-article", f.SubmitArticle(config, db, store))
|
||||||
mux.HandleFunc("POST /update-self", frontend.UpdateSelf(config, db, store))
|
mux.HandleFunc("POST /update-self", f.UpdateSelf(config, db, store))
|
||||||
mux.HandleFunc("POST /update-user/{id}", frontend.UpdateUser(config, db, store))
|
mux.HandleFunc("POST /update-user/{id}", f.UpdateUser(config, db, store))
|
||||||
mux.HandleFunc("POST /upload-image", frontend.UploadImage(config))
|
mux.HandleFunc("POST /upload-image", f.UploadImage(config))
|
||||||
|
|
||||||
log.Fatalln(http.ListenAndServe(config.Port, mux))
|
log.Fatalln(http.ListenAndServe(config.Port, mux))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user