Files
comfychan/web/main.go
T

432 lines
12 KiB
Go

package main
import (
"database/sql"
"errors"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/dominicf2001/comfychan/internal/database"
"github.com/dominicf2001/comfychan/internal/util"
"github.com/dominicf2001/comfychan/web/views"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
_ "github.com/mattn/go-sqlite3"
)
var dev = true
func disableCacheInDevMode(next http.Handler) http.Handler {
if !dev {
return next
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store")
next.ServeHTTP(w, r)
})
}
func SumUniquePostIps(posts []database.Post) int {
uniqueIpHashes := map[string]bool{}
for _, post := range posts {
uniqueIpHashes[post.IpHash] = true
}
return len(uniqueIpHashes)
}
func main() {
// -----------------
// SETUP
// -----------------
db, err := sql.Open("sqlite3", "internal/database/comfychan.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Handle("/static/*",
disableCacheInDevMode(
http.StripPrefix("/static",
http.FileServer(http.Dir("web/static")))))
// -----------------
// -----------------
// MAIN ROUTES
// -----------------
// INDEX PAGE
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
views.Index().Render(r.Context(), w)
})
// MAIN BOARD PAGE
r.Get("/{slug}", func(w http.ResponseWriter, r *http.Request) {
slug := chi.URLParam(r, "slug")
board, err := database.GetBoard(db, slug)
if err != nil {
http.NotFound(w, r)
log.Printf("Board %q not found: %v", slug, err)
return
}
views.Board(board).Render(r.Context(), w)
})
// THREAD PAGE
r.Get("/{slug}/threads/{threadId}", func(w http.ResponseWriter, r *http.Request) {
slug := chi.URLParam(r, "slug")
threadIdStr := chi.URLParam(r, "threadId")
threadId, err := strconv.Atoi(threadIdStr)
if err != nil {
http.Error(w, "Invalid thread id", http.StatusBadRequest)
return
}
board, err := database.GetBoard(db, slug)
if err != nil {
http.NotFound(w, r)
log.Printf("Board %q not found: %v", slug, err)
return
}
thread, err := database.GetThread(db, threadId)
if err != nil {
http.Error(w, "Failed to get thread", http.StatusInternalServerError)
log.Printf("Failed to get thread %q: %v", threadIdStr, err)
return
}
posts, err := database.GetPosts(db, threadId)
if err != nil {
http.Error(w, "Failed to get posts", http.StatusInternalServerError)
log.Printf("Failed to get thread %q posts: %v", threadIdStr, err)
return
}
if len(posts) == 0 || posts[0].MediaPath == "" {
http.Error(w, "Malformed thread", http.StatusInternalServerError)
log.Printf("Thread %d has no posts or no OP image", threadId)
return
}
views.Thread(board, thread, posts).Render(r.Context(), w)
})
// CREATE THREAD
r.Post("/{slug}/threads", func(w http.ResponseWriter, r *http.Request) {
slug := chi.URLParam(r, "slug")
ip := util.GetIP(r)
// check cooldown
timeRemaining := util.GetRemainingCooldown(ip, util.ThreadCooldowns, util.THREAD_COOLDOWN)
if timeRemaining > 0 {
response := fmt.Sprintf("Please wait %.0f seconds", timeRemaining.Seconds())
io.Copy(io.Discard, r.Body)
http.Error(w, response, http.StatusTooManyRequests)
return
}
// parse form
r.Body = http.MaxBytesReader(w, r.Body, util.MAX_REQUEST_BYTES)
if err := r.ParseMultipartForm(util.FILE_MEM_LIMIT); err != nil {
var maxErr *http.MaxBytesError
if errors.As(err, &maxErr) {
http.Error(w, fmt.Sprintf("File too large (max %s)", util.FormatBytes(util.FILE_MEM_LIMIT)), http.StatusRequestEntityTooLarge)
return
}
if errors.Is(err, multipart.ErrMessageTooLarge) {
http.Error(w, fmt.Sprintf("File too large (max %s)", util.FormatBytes(util.FILE_MEM_LIMIT)), http.StatusRequestEntityTooLarge)
return
}
http.Error(w, "Failed to parse form", http.StatusBadRequest)
log.Printf("ParseMultipartForm: %v", err)
return
}
// validate inputs
subject := strings.ReplaceAll(strings.TrimSpace(r.FormValue("subject")), "\n", "")
body := strings.TrimSpace(r.FormValue("body"))
if len(subject) > util.MAX_SUBJECT_LEN {
http.Error(w, fmt.Sprintf("Subject exceeds %d characters", util.MAX_BODY_LEN), http.StatusBadRequest)
return
}
if body == "" {
http.Error(w, "Body is empty", http.StatusBadRequest)
return
}
if len(body) > util.MAX_BODY_LEN {
http.Error(w, fmt.Sprintf("Body exceeds %d characters", util.MAX_BODY_LEN), http.StatusBadRequest)
return
}
file, header, err := r.FormFile("file")
if err != nil {
http.Error(w, "Failed to retrive file from form", http.StatusBadRequest)
log.Printf("FormFile: %v", err)
return
}
defer file.Close()
if header.Size > util.FILE_MEM_LIMIT {
http.Error(w, fmt.Sprintf("File too large (max %s)", util.FormatBytes(util.FILE_MEM_LIMIT)), http.StatusRequestEntityTooLarge)
return
}
mediaType, err := util.DetectPostFileType(file)
if err != nil {
http.Error(w, "Failed to detect if file is a video", http.StatusInternalServerError)
return
}
file.Seek(0, io.SeekStart)
if mediaType == util.PostFileUnsupported {
http.Error(w, "Unsupported media type", http.StatusBadRequest)
return
}
filename := strconv.FormatInt(time.Now().UnixNano(), 10) + filepath.Ext(header.Filename)
err, savedMediaPath, savedThumbPath := util.SavePostFile(file, filename)
if err != nil {
http.Error(w, "Failed to save file", http.StatusInternalServerError)
log.Printf("savePostFile: %v", err)
return
}
if err := database.PutThread(db, slug, subject, body, savedMediaPath, savedThumbPath, util.HashIp(ip)); err != nil {
http.Error(w, "Failed to create thread", http.StatusInternalServerError)
log.Printf("PutThread: %v", err)
return
}
util.BeginCooldown(ip, util.ThreadCooldowns, util.THREAD_COOLDOWN)
})
// CREATE POST
r.Post("/{slug}/threads/{threadId}", func(w http.ResponseWriter, r *http.Request) {
slug := chi.URLParam(r, "slug")
ip := util.GetIP(r)
// check cooldown
timeRemaining := util.GetRemainingCooldown(ip, util.PostCooldowns, util.POST_COOLDOWN)
if timeRemaining > 0 {
response := fmt.Sprintf("Please wait %.0f seconds", timeRemaining.Seconds())
io.Copy(io.Discard, r.Body)
http.Error(w, response, http.StatusTooManyRequests)
return
}
// parse form
r.Body = http.MaxBytesReader(w, r.Body, util.MAX_REQUEST_BYTES)
if err := r.ParseMultipartForm(util.FILE_MEM_LIMIT); err != nil {
var maxErr *http.MaxBytesError
if errors.As(err, &maxErr) {
http.Error(w, fmt.Sprintf("File too large (max %s)", util.FormatBytes(util.FILE_MEM_LIMIT)), http.StatusRequestEntityTooLarge)
return
}
if errors.Is(err, multipart.ErrMessageTooLarge) {
http.Error(w, fmt.Sprintf("File too large (max %s)", util.FormatBytes(util.FILE_MEM_LIMIT)), http.StatusRequestEntityTooLarge)
return
}
http.Error(w, "Failed to parse form", http.StatusBadRequest)
log.Printf("ParseMultipartForm: %v", err)
return
}
// validate inputs
body := strings.TrimSpace(r.FormValue("body"))
mediaPath := ""
thumbPath := ""
if body == "" {
http.Error(w, "Body is empty", http.StatusBadRequest)
return
}
if len(body) > util.MAX_BODY_LEN {
http.Error(w, fmt.Sprintf("Body exceeds %d characters", util.MAX_BODY_LEN), http.StatusBadRequest)
return
}
file, header, err := r.FormFile("file")
if err != nil {
if !errors.Is(err, http.ErrMissingFile) {
http.Error(w, "Failed to retrieve file from form", http.StatusBadRequest)
log.Printf("FormFile: %v", err)
return
}
} else {
defer file.Close()
if header.Size > util.FILE_MEM_LIMIT {
http.Error(w, fmt.Sprintf("File too large (max %s)", util.FormatBytes(util.FILE_MEM_LIMIT)), http.StatusRequestEntityTooLarge)
return
}
mediaType, err := util.DetectPostFileType(file)
if err != nil {
http.Error(w, "Failed to detect if file is a video", http.StatusInternalServerError)
return
}
file.Seek(0, io.SeekStart)
if mediaType == util.PostFileUnsupported {
http.Error(w, "Unsupported media type", http.StatusBadRequest)
return
}
filename := strconv.FormatInt(time.Now().UnixNano(), 10) + filepath.Ext(header.Filename)
err, savedMediaPath, savedThumbPath := util.SavePostFile(file, filename)
if err != nil {
http.Error(w, "Failed to save file", http.StatusInternalServerError)
log.Printf("savePostFile: %v", err)
return
}
mediaPath = savedMediaPath
thumbPath = savedThumbPath
}
// put post into DB
threadIdStr := chi.URLParam(r, "threadId")
threadId, err := strconv.Atoi(threadIdStr)
if err != nil {
http.Error(w, "Invalid thread id", http.StatusBadRequest)
return
}
if err := database.PutPost(db, slug, threadId, body, mediaPath, thumbPath, util.HashIp(ip)); err != nil {
http.Error(w, "Failed to create post", http.StatusInternalServerError)
log.Printf("PutPost: %v", err)
return
}
util.BeginCooldown(ip, util.PostCooldowns, util.POST_COOLDOWN)
})
// -----------------
// -----------------
// PARTIALS (htmx)
// -----------------
// CATALOG
r.Get("/hx/{slug}/catalog", func(w http.ResponseWriter, r *http.Request) {
slug := chi.URLParam(r, "slug")
threads, err := database.GetThreads(db, slug)
if err != nil {
http.Error(w, "Failed to get threads", http.StatusInternalServerError)
log.Printf("GetThreads: %v", err)
return
}
previews := make([]views.CatalogThreadPreview, 0, len(threads))
for _, thread := range threads {
posts, err := database.GetPosts(db, thread.Id)
if err != nil {
http.Error(w, "Failed to get posts", http.StatusBadRequest)
log.Printf("GetOriginalPost: %v", err)
return
}
op := posts[0]
if op.MediaPath == "" {
http.Error(w, "Malformed thread", http.StatusInternalServerError)
log.Printf("Thread %d has no OP image", thread.Id)
return
}
previews = append(previews, views.CatalogThreadPreview{
Subject: thread.Subject,
Body: op.Body,
ThreadURL: fmt.Sprintf("/%s/threads/%d", slug, thread.Id),
ThumbPath: op.ThumbPath,
ReplyCount: len(posts),
IpCount: SumUniquePostIps(posts),
})
}
views.ThreadsCatalog(previews).Render(r.Context(), w)
})
// THREAD POSTS
r.Get("/hx/{slug}/threads/{threadId}/posts", func(w http.ResponseWriter, r *http.Request) {
// slug := chi.URLParam(r, "slug")
threadIdStr := chi.URLParam(r, "threadId")
threadId, err := strconv.Atoi(threadIdStr)
if err != nil {
http.Error(w, "Invalid thread id", http.StatusBadRequest)
return
}
thread, err := database.GetThread(db, threadId)
if err != nil {
http.Error(w, "Failed to get thread", http.StatusBadRequest)
log.Printf("GetThread: %v", err)
return
}
posts, err := database.GetPosts(db, threadId)
if err != nil {
http.Error(w, "Failed to get posts", http.StatusBadRequest)
log.Printf("GetPosts: %v", err)
return
}
if len(posts) == 0 || posts[0].MediaPath == "" {
http.Error(w, "Malformed thread", http.StatusInternalServerError)
log.Printf("Thread %d has no posts or no OP image", threadId)
return
}
// dont pass the op post. only replies
views.Posts(posts, thread).Render(r.Context(), w)
})
// -----------------
// -----------------
// CLEANUP
// -----------------
go func() {
for range time.Tick(10 * time.Minute) {
util.CooldownMutex.Lock()
defer util.CooldownMutex.Unlock()
for ip, cooldown := range util.PostCooldowns {
if time.Since(cooldown) >= util.POST_COOLDOWN {
delete(util.PostCooldowns, ip)
}
}
for ip, cooldown := range util.ThreadCooldowns {
if time.Since(cooldown) >= util.THREAD_COOLDOWN {
delete(util.ThreadCooldowns, ip)
}
}
}
}()
// -----------------
fmt.Println("Listening on :8080")
http.ListenAndServe(":8080", r)
}