add backend supporting opengraph and comments

This commit is contained in:
2024-11-29 19:14:12 -07:00
parent 09a0c44e50
commit d2efb2f0cb
15 changed files with 633 additions and 167 deletions

151
backend/datastore/mapper.go Normal file
View File

@ -0,0 +1,151 @@
package datastore
import (
"database/sql"
"fmt"
"git.sa.vin/any-remark/backend/types"
"math/rand"
)
type Mapper interface {
GetOpenGraphData(url string) (string, error)
SaveOpenGraphData(url, opengraph string) error
AddComment(req types.AddCommentRequest) error
GetComments(url string) ([]types.Comment, error)
}
type mapper struct {
db *sql.DB
}
func NewMapper(db *sql.DB) Mapper {
// Empty function for NewMapper
return &mapper{
db: db,
}
}
func (m *mapper) GetOpenGraphData(url string) (string, error) {
// get the opengraph data from the db table named "webpages" using the url
query := `SELECT opengraph FROM webpages WHERE url = ?`
rows, err := m.db.Query(query, url)
if err != nil {
if err == sql.ErrNoRows {
return "", fmt.Errorf("no data found in db for url: %s", url)
}
return "", fmt.Errorf("error getting webpage data from db: %s", err)
}
defer rows.Close()
// get the opengraph data from the rows array
var opengraph string
for rows.Next() {
err = rows.Scan(&opengraph)
if err != nil {
return "", fmt.Errorf("error scanning opengraph data: %s", err)
}
return opengraph, nil
}
return "", fmt.Errorf("no data found in db for url: %s", url)
}
// SaveOpenGraphData saves the opengraph data to the db
func (m *mapper) SaveOpenGraphData(url, opengraph string) error {
// generate a unique id for the webpage make it 24 bytes long
id := "wp_" + generateID(24)
// insert the opengraph data to the db table named "webpages" using the url
query := `INSERT INTO webpages (id, url, opengraph) VALUES (?, ?, ?)`
_, err := m.db.Exec(query, id, url, opengraph)
if err != nil {
return fmt.Errorf("error inserting webpage data to db: %s", err)
}
return nil
}
func (m *mapper) GetWebpageID(url string) (string, error) {
// get the id of the webpage from the db table named "webpages" using the url
query := `SELECT id FROM webpages WHERE url = ?`
rows, err := m.db.Query(query, url)
if err != nil {
if err == sql.ErrNoRows {
return "", fmt.Errorf("no data found in db for url: %s", url)
}
return "", fmt.Errorf("error getting webpage data from db: %s", err)
}
defer rows.Close()
// get the id from the rows array
var id string
for rows.Next() {
err = rows.Scan(&id)
if err != nil {
return "", fmt.Errorf("error scanning webpage id: %s", err)
}
return id, nil
}
return "", fmt.Errorf("no data found in db for url: %s", url)
}
func (m *mapper) AddComment(req types.AddCommentRequest) error {
webpageID, err := m.GetWebpageID(req.URL)
if err != nil {
return fmt.Errorf("error getting webpage id: %s", err)
}
// generate a unique id for the comment make it 24 bytes long
id := "c_" + generateID(24)
// insert the comment data to the db table named "comments" using the url
query := `INSERT INTO comments (id, content, webpage_id, commenter) VALUES (?, ?, ?, ?)`
_, err = m.db.Exec(query, id, req.Comment, webpageID, req.UserID)
if err != nil {
return fmt.Errorf("error inserting comment data to db: %s", err)
}
return nil
}
func (m *mapper) GetComments(url string) ([]types.Comment, error) {
webpageID, err := m.GetWebpageID(url)
if err != nil {
return nil, fmt.Errorf("error getting webpage id: %s", err)
}
// TODO: add up vote and down vote counts
// TODO: attach avatar url to commenter
// TODO: provide order by recent or most upvoted
// TODO: bubble up the current user's comments
// get the comments from the db table named "comments" using the webpage_id
query := `SELECT id, content, commenter FROM comments WHERE webpage_id = ?`
rows, err := m.db.Query(query, webpageID)
if err != nil {
if err == sql.ErrNoRows {
return nil, fmt.Errorf("no data found in db for url: %s", url)
}
return nil, fmt.Errorf("error getting comments from db: %s", err)
}
defer rows.Close()
// get the comments from the rows array
var comments []types.Comment
for rows.Next() {
var comment types.Comment
err = rows.Scan(&comment.ID, &comment.Content, &comment.Commenter)
if err != nil {
return nil, fmt.Errorf("error scanning comments: %s", err)
}
comments = append(comments, comment)
}
return comments, nil
}
func generateID(n int) string {
// generate a unique id for the webpage make it 24 bytes
// long using the following character set
charSet := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
charSetLen := len(charSet)
id := make([]byte, n)
for i := range id {
// choose a random character from the character set
id[i] = charSet[rand.Intn(charSetLen)]
}
return string(id)
}

77
backend/main.go Normal file
View File

@ -0,0 +1,77 @@
package main
import (
"embed"
"git.sa.vin/any-remark/backend/datastore"
"git.sa.vin/any-remark/backend/server"
"github.com/gorilla/mux"
"github.com/payne8/go-libsql-dual-driver"
"github.com/rs/cors"
"log"
"net/http"
"os"
)
//go:embed migrations/*.sql
var migrationFiles embed.FS
func main() {
// create an http server for the following routes
// /api/v1/opengraph
// /x/:id -- short links
// /api/v1/shorten -- short link creation
logger := log.New(os.Stdout, "any-remark", log.LstdFlags)
primaryUrl := os.Getenv("LIBSQL_DATABASE_URL")
authToken := os.Getenv("LIBSQL_AUTH_TOKEN")
tdb, err := libsqldb.NewLibSqlDB(
primaryUrl,
libsqldb.WithMigrationFiles(migrationFiles),
libsqldb.WithAuthToken(authToken),
libsqldb.WithLocalDBName("local.db"), // will not be used for remote-only
)
if err != nil {
logger.Printf("failed to open db %s: %s", primaryUrl, err)
log.Fatalln(err)
return
}
err = tdb.Migrate()
if err != nil {
logger.Printf("failed to migrate db %s: %s", primaryUrl, err)
log.Fatalln(err)
return
}
mapper := datastore.NewMapper(tdb.DB)
s := server.NewServer(mapper)
r := mux.NewRouter()
// add allow cors middleware
//r.Use(server.CorsMiddleware)
r.HandleFunc("/api/v1/opengraph", s.OpenGraphHandler).Methods("GET")
r.HandleFunc("/api/v1/shorten", s.ShortenLinkHandler).Methods("POST")
r.HandleFunc("/api/v1/comments", s.ListComments).Methods("GET")
r.HandleFunc("/api/v1/comment", s.AddComment).Methods("POST")
r.HandleFunc("/x/{id}", s.ShortLinkHandler).Methods("GET")
c := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:5173"},
AllowCredentials: true,
})
handler := c.Handler(r)
//log.Fatal(http.ListenAndServe(":3000", handler)
http.Handle("/", handler)
err = http.ListenAndServe(":8080", nil)
if err != nil {
logger.Printf("server failed: %s", err)
log.Fatalln(err)
return
}
}

View File

@ -0,0 +1,37 @@
-- all ids will be TEXT
-- make a table for storing webpages and related opengraph information in json format
CREATE TABLE webpages (
id TEXT PRIMARY KEY,
url TEXT NOT NULL UNIQUE,
opengraph JSONB NOT NULL
);
-- add an index to the url column
CREATE INDEX url_index ON webpages (url);
-- make a table for storing comments on webpages and replies to comments
CREATE TABLE comments (
id TEXT PRIMARY KEY,
webpage_id TEXT NOT NULL,
parent_id TEXT, -- NULL if top-level comment
commenter TEXT NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- make a table for storing votes on comments and webpages
CREATE TABLE votes (
id TEXT PRIMARY KEY,
webpage_id TEXT NOT NULL,
comment_id TEXT, -- NULL if vote is on webpage
voter TEXT NOT NULL,
vote_type TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- make a table for storing users
CREATE TABLE users (
id TEXT PRIMARY KEY,
username TEXT NOT NULL,
avatar TEXT NOT NULL
);

150
backend/server/server.go Normal file
View File

@ -0,0 +1,150 @@
package server
import (
"encoding/json"
"fmt"
"git.sa.vin/any-remark/backend/datastore"
"git.sa.vin/any-remark/backend/types"
"github.com/dyatlov/go-opengraph/opengraph"
"net/http"
)
type Server interface {
OpenGraphHandler(w http.ResponseWriter, r *http.Request)
ShortLinkHandler(w http.ResponseWriter, r *http.Request)
ShortenLinkHandler(w http.ResponseWriter, r *http.Request)
AddComment(w http.ResponseWriter, r *http.Request)
ListComments(w http.ResponseWriter, r *http.Request)
}
type server struct {
mapper datastore.Mapper
}
func NewServer(mapper datastore.Mapper) Server {
// Empty function for NewServer
return &server{
mapper: mapper,
}
}
func (s *server) OpenGraphHandler(w http.ResponseWriter, r *http.Request) {
// get the url from the query string
queryParams := r.URL.Query()
url := queryParams.Get("url")
og := opengraph.NewOpenGraph()
if url == "" {
http.Error(w, "url parameter is required", http.StatusBadRequest)
return
}
// check if our db has the url already
// get the opengraph data from the db
ogString, err := s.mapper.GetOpenGraphData(url)
if err != nil || ogString == "" {
// if it doesn't, fetch the opengraph data from the url
// TODO: fetch the url require that it responds with html
resp, err := http.Get(url) // TODO: use an external proxy to fetch the url
if err != nil {
http.Error(w, fmt.Sprintf("error fetching url: %s", err), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
err = og.ProcessHTML(resp.Body)
if err != nil {
// TODO: decide what to do if the given site does not respond with html or doesn't exist etc.
http.Error(w, fmt.Sprintf("error processing html: %s", err), http.StatusInternalServerError)
return
}
// save the opengraph data to the db
err = s.mapper.SaveOpenGraphData(url, og.String())
if err != nil {
http.Error(w, fmt.Sprintf("error saving opengraph data: %s", err), http.StatusInternalServerError)
return
}
// return the opengraph data
w.Header().Set("Content-Type", "application/json")
_, err = w.Write([]byte(og.String()))
if err != nil {
http.Error(w, fmt.Sprintf("error writing opengraph data: %s", err), http.StatusInternalServerError)
return
}
return
}
// return the opengraph data
w.Header().Set("Content-Type", "application/json")
_, err = w.Write([]byte(ogString))
if err != nil {
http.Error(w, fmt.Sprintf("error writing opengraph data: %s", err), http.StatusInternalServerError)
return
}
}
func (s *server) ShortLinkHandler(w http.ResponseWriter, r *http.Request) {
// Empty handler for /x/{id}
}
func (s *server) ShortenLinkHandler(w http.ResponseWriter, r *http.Request) {
// Empty handler for /api/v1/shorten
}
func (s *server) AddComment(w http.ResponseWriter, r *http.Request) {
var req types.AddCommentRequest
// unmarshal the request body into the req struct
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, fmt.Sprintf("error decoding request body: %s", err), http.StatusBadRequest)
return
}
err = s.mapper.AddComment(req)
if err != nil {
http.Error(w, fmt.Sprintf("error adding comment: %s", err), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
}
func (s *server) ListComments(w http.ResponseWriter, r *http.Request) {
comments, err := s.mapper.GetComments(r.URL.Query().Get("url"))
if err != nil {
http.Error(w, fmt.Sprintf("error getting comments: %s", err), http.StatusInternalServerError)
return
}
// TODO: add pagination
// return the comments
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(comments)
if err != nil {
http.Error(w, fmt.Sprintf("error encoding comments: %s", err), http.StatusInternalServerError)
return
}
}
// CorsMiddleware is a middleware that allows CORS
func CorsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// allow all origins
w.Header().Set("Access-Control-Allow-Origin", "*")
// allow all headers
w.Header().Set("Access-Control-Allow-Headers", "*")
// allow all methods
w.Header().Set("Access-Control-Allow-Methods", "*")
// allow credentials
w.Header().Set("Access-Control-Allow-Credentials", "true")
// handle pre-flight requests
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}
// call the next handler
next.ServeHTTP(w, r)
})
}

16
backend/types/types.go Normal file
View File

@ -0,0 +1,16 @@
package types
type AddCommentRequest struct {
Comment string `json:"comment"`
URL string `json:"url"`
UserID string `json:"user_id"`
}
type Comment struct {
ID string `json:"id"`
Content string `json:"content"`
Commenter string `json:"commenter"`
ParentID string `json:"parent_id"`
WebpageID string `json:"webpage_id"`
CreatedAt string `json:"created_at"`
}