GoBlog/postAliases.go

40 lines
986 B
Go
Raw Normal View History

2020-11-09 17:33:56 +00:00
package main
import (
"database/sql"
"net/http"
)
func (db *database) allPostAliases() ([]string, error) {
2020-11-09 17:33:56 +00:00
var aliases []string
rows, err := db.query("select distinct value from post_parameters where parameter = 'aliases' and value != path")
2020-11-09 17:33:56 +00:00
if err != nil {
return nil, err
}
for rows.Next() {
var path string
_ = rows.Scan(&path)
if path != "" {
aliases = append(aliases, path)
}
2020-11-09 17:33:56 +00:00
}
return aliases, nil
}
func (a *goBlog) servePostAlias(w http.ResponseWriter, r *http.Request) {
row, err := a.db.queryRow("select path from post_parameters where parameter = 'aliases' and value = @alias", sql.Named("alias", r.URL.Path))
2020-11-09 17:33:56 +00:00
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
2020-11-09 17:33:56 +00:00
return
}
var path string
2021-02-08 17:51:07 +00:00
if err := row.Scan(&path); err == sql.ErrNoRows {
a.serve404(w, r)
2020-11-09 17:33:56 +00:00
return
} else if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
2020-11-09 17:33:56 +00:00
return
}
http.Redirect(w, r, path, http.StatusFound)
}