kls/handler.go

99 lines
2.2 KiB
Go

package main
import (
"context"
"crypto/subtle"
"embed"
"fmt"
"html/template"
"log"
"net/http"
"strings"
"git.sr.ht/~kisom/goutils/config"
"git.wntrmute.dev/kyle/kls/links"
"github.com/jackc/pgx/v4/pgxpool"
)
type server struct {
db *pgxpool.Pool
}
//go:embed templates/*.tpl
var templateFiles embed.FS
var templates = template.Must(template.ParseFS(templateFiles, "templates/*.tpl"))
type page struct {
Short string
}
func (srv *server) servePage(w http.ResponseWriter, p page) {
err := templates.ExecuteTemplate(w, "index.tpl", p)
if err != nil {
log.Printf("error executing template: %s", err)
http.Error(w, fmt.Sprintf("template execution failed: %s", err.Error()),
http.StatusInternalServerError)
return
}
}
func (srv *server) postURL(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
url := r.FormValue("value")
if len(url) == 0 {
http.Error(w, "invalid URL", http.StatusBadRequest)
return
}
ctx := context.Background()
short, err := links.StoreURL(ctx, srv.db, url)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
srv.servePage(w, page{Short: short})
}
func (srv *server) redirect(w http.ResponseWriter, r *http.Request) {
short := strings.TrimPrefix(r.URL.Path, "/")
u, err := links.RetrieveURL(context.Background(), srv.db, short)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
http.Redirect(w, r, u, http.StatusFound)
}
func (srv *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && links.ValidShortCode.MatchString(r.URL.Path) {
srv.redirect(w, r)
return
}
user, pass, ok := r.BasicAuth()
username := config.Get("HTTP_USER")
password := config.Get("HTTP_PASS")
if !ok || subtle.ConstantTimeCompare([]byte(user), []byte(username)) != 1 || subtle.ConstantTimeCompare([]byte(pass), []byte(password)) != 1 {
w.Header().Set("WWW-Authenticate", `Basic realm="quicknote"`)
w.WriteHeader(401)
w.Write([]byte("Unauthorised.\n"))
return
}
if r.Method == http.MethodPost {
srv.postURL(w, r)
return
}
srv.servePage(w, page{})
}