2024-02-18 07:20:51 +01:00
|
|
|
package main
|
|
|
|
|
2024-02-18 10:07:49 +01:00
|
|
|
import (
|
2024-03-03 09:16:49 +01:00
|
|
|
"encoding/gob"
|
2024-02-18 10:07:49 +01:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-02-24 11:41:01 +01:00
|
|
|
"os"
|
2024-02-18 10:07:49 +01:00
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
"streifling.com/jason/cpolis/cmd/backend"
|
|
|
|
"streifling.com/jason/cpolis/cmd/frontend"
|
2024-02-18 10:07:49 +01:00
|
|
|
)
|
2024-02-18 07:20:51 +01:00
|
|
|
|
2024-03-03 09:16:49 +01:00
|
|
|
func init() {
|
2024-07-13 13:58:36 +02:00
|
|
|
gob.Register(backend.User{})
|
2024-03-03 09:16:49 +01:00
|
|
|
}
|
|
|
|
|
2024-02-18 07:20:51 +01:00
|
|
|
func main() {
|
2024-07-13 13:58:36 +02:00
|
|
|
config, err := backend.HandleConfig()
|
2024-03-29 09:07:17 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2024-04-12 07:17:13 +02:00
|
|
|
logFile, err := os.OpenFile(config.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()
|
2024-03-29 09:07:17 +01:00
|
|
|
log.SetOutput(logFile)
|
2024-02-24 11:41:01 +01:00
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
db, err := backend.OpenDB(config.DBName)
|
2024-02-18 16:37:13 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2024-02-22 19:27:41 +01:00
|
|
|
defer db.Close()
|
2024-02-18 16:37:13 +01:00
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
key, err := backend.LoadKey(config.KeyFile)
|
2024-03-03 09:16:49 +01:00
|
|
|
if err != nil {
|
2024-07-13 13:58:36 +02:00
|
|
|
key, err = backend.NewKey()
|
2024-03-03 09:16:49 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2024-07-13 13:58:36 +02:00
|
|
|
backend.SaveKey(key, config.KeyFile)
|
2024-03-03 09:16:49 +01:00
|
|
|
}
|
2024-07-13 13:58:36 +02:00
|
|
|
store := backend.NewCookieStore(key)
|
2024-03-03 09:16:49 +01:00
|
|
|
|
2024-02-18 14:31:28 +01:00
|
|
|
mux := http.NewServeMux()
|
2024-03-07 15:31:00 +01:00
|
|
|
mux.Handle("/web/static/", http.StripPrefix("/web/static/",
|
2024-04-12 07:17:13 +02:00
|
|
|
http.FileServer(http.Dir(config.WebDir+"/static/"))))
|
2024-07-13 13:58:36 +02:00
|
|
|
mux.HandleFunc("/", frontend.HomePage(config, db, store))
|
2024-02-24 10:56:12 +01:00
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
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))
|
2024-04-07 10:58:07 +02:00
|
|
|
mux.HandleFunc("GET /rss", func(w http.ResponseWriter, r *http.Request) {
|
2024-04-12 07:17:13 +02:00
|
|
|
http.ServeFile(w, r, config.RSSFile)
|
2024-04-07 10:58:07 +02:00
|
|
|
})
|
2024-07-13 13:58:36 +02:00
|
|
|
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))
|
2024-03-01 11:30:31 +01:00
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
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))
|
2024-02-18 10:07:49 +01:00
|
|
|
|
2024-04-12 07:17:13 +02:00
|
|
|
log.Fatalln(http.ListenAndServe(config.Port, mux))
|
2024-02-18 07:20:51 +01:00
|
|
|
}
|