2018-11-24 12:34:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-08-25 10:30:00 +00:00
|
|
|
"encoding/json"
|
2019-02-18 19:47:55 +00:00
|
|
|
"flag"
|
2018-11-24 12:34:51 +00:00
|
|
|
"fmt"
|
2019-08-25 10:56:11 +00:00
|
|
|
"html"
|
2018-11-24 12:34:51 +00:00
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2019-02-18 19:47:31 +00:00
|
|
|
"time"
|
2019-08-25 10:30:00 +00:00
|
|
|
|
|
|
|
"gitlab.com/golang-commonmark/markdown"
|
|
|
|
"p83.nl/go/ekster/pkg/util"
|
|
|
|
"p83.nl/go/indieauth"
|
2018-11-24 12:34:51 +00:00
|
|
|
)
|
|
|
|
|
2020-05-12 14:12:33 +00:00
|
|
|
func init() {
|
|
|
|
log.SetFlags(log.Lshortfile)
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:34:51 +00:00
|
|
|
var (
|
|
|
|
mp PagesRepository
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ClientID = "https://wiki.p83.nl/"
|
|
|
|
RedirectURI = "https://wiki.p83.nl/auth/callback"
|
|
|
|
)
|
|
|
|
|
2020-05-08 15:21:39 +00:00
|
|
|
type Backref struct {
|
|
|
|
Name string
|
|
|
|
Title string
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:34:51 +00:00
|
|
|
// Page
|
|
|
|
type Page struct {
|
2020-05-07 13:20:36 +00:00
|
|
|
// Name is the filename of the page
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Title is the human-readable title of the page
|
|
|
|
Title string
|
|
|
|
|
2018-11-24 12:34:51 +00:00
|
|
|
Content string
|
2020-05-07 13:20:36 +00:00
|
|
|
|
2020-05-08 15:21:39 +00:00
|
|
|
Refs []Backref
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DiffPage struct {
|
|
|
|
Content string
|
|
|
|
Diff template.HTML
|
|
|
|
}
|
|
|
|
|
|
|
|
type Revision struct {
|
|
|
|
Version string
|
|
|
|
Page DiffPage
|
|
|
|
Summary string
|
|
|
|
}
|
|
|
|
|
2019-02-18 19:47:31 +00:00
|
|
|
type Change struct {
|
2019-03-02 13:38:35 +00:00
|
|
|
Page string
|
|
|
|
Date time.Time
|
|
|
|
EndDate time.Time
|
|
|
|
Body string
|
|
|
|
Count int
|
|
|
|
|
|
|
|
DateSummary string
|
|
|
|
TimeSummary string
|
2019-02-18 19:47:31 +00:00
|
|
|
}
|
|
|
|
|
2018-11-24 12:34:51 +00:00
|
|
|
type PagesRepository interface {
|
|
|
|
Get(p string) Page
|
2019-02-19 06:34:52 +00:00
|
|
|
Save(p string, page Page, summary, author string) error
|
2018-11-24 12:34:51 +00:00
|
|
|
Exist(p string) bool
|
|
|
|
PageHistory(p string) ([]Revision, error)
|
2019-02-18 19:47:31 +00:00
|
|
|
RecentChanges() ([]Change, error)
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type indexPage struct {
|
2020-05-07 13:20:36 +00:00
|
|
|
Session *Session
|
|
|
|
Title string
|
|
|
|
Name string
|
|
|
|
Content template.HTML
|
2020-05-08 15:21:39 +00:00
|
|
|
Backrefs []Backref
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type editPage struct {
|
2020-05-08 15:21:39 +00:00
|
|
|
Session *Session
|
|
|
|
Title string
|
|
|
|
Content string
|
|
|
|
Name string
|
|
|
|
Editor template.HTML
|
|
|
|
Backrefs []Backref
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type historyPage struct {
|
|
|
|
Session *Session
|
|
|
|
Title string
|
|
|
|
Name string
|
|
|
|
History []Revision
|
|
|
|
}
|
|
|
|
|
2019-02-18 19:47:31 +00:00
|
|
|
type recentPage struct {
|
|
|
|
Session *Session
|
|
|
|
Title string
|
|
|
|
Name string
|
2019-02-19 06:34:52 +00:00
|
|
|
Recent []Change
|
2019-02-18 19:47:31 +00:00
|
|
|
}
|
|
|
|
|
2018-11-24 12:34:51 +00:00
|
|
|
type indexHandler struct{}
|
|
|
|
type saveHandler struct{}
|
|
|
|
type editHandler struct{}
|
|
|
|
type historyHandler struct{}
|
2019-02-18 19:47:31 +00:00
|
|
|
type recentHandler struct{}
|
2018-11-24 12:34:51 +00:00
|
|
|
type authHandler struct{}
|
|
|
|
|
|
|
|
func (*authHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
sess, err := NewSession(w, r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2019-02-25 18:03:04 +00:00
|
|
|
defer func() {
|
|
|
|
if err := sess.Flush(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
2018-11-24 12:34:51 +00:00
|
|
|
|
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
if r.URL.Path == "/auth/login" {
|
|
|
|
t, err := template.ParseFiles("templates/layout.html", "templates/login.html")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = t.Execute(w, nil)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if r.URL.Path == "/auth/callback" {
|
2019-02-25 19:09:43 +00:00
|
|
|
log.Printf("%+v\n", sess)
|
|
|
|
code := r.URL.Query().Get("code")
|
|
|
|
state := r.URL.Query().Get("state")
|
2018-11-24 12:34:51 +00:00
|
|
|
|
|
|
|
if state != sess.State {
|
2019-02-25 19:09:43 +00:00
|
|
|
log.Printf("mismatched state: %s != %s", state, sess.State)
|
2018-11-24 12:34:51 +00:00
|
|
|
http.Error(w, "mismatched state", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
authURL := sess.AuthorizationEndpoint
|
|
|
|
|
|
|
|
verified, response, err := indieauth.VerifyAuthCode(ClientID, code, RedirectURI, authURL)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !verified {
|
|
|
|
http.Redirect(w, r, "/auth/login", 302)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sess.Me = response.Me
|
|
|
|
sess.LoggedIn = true
|
|
|
|
sess.State = ""
|
|
|
|
|
|
|
|
http.Redirect(w, r, sess.NextURI, 302)
|
|
|
|
return
|
|
|
|
} else if r.URL.Path == "/auth/logout" {
|
|
|
|
t, err := template.ParseFiles("templates/layout.html", "templates/logout.html")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = t.Execute(w, nil)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if r.Method == http.MethodPost {
|
|
|
|
if r.URL.Path == "/auth/login" {
|
|
|
|
r.ParseForm()
|
|
|
|
urlString := r.PostFormValue("url")
|
|
|
|
|
|
|
|
u, err := url.Parse(urlString)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoints, err := indieauth.GetEndpoints(u)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
authURL, err := url.Parse(endpoints.AuthorizationEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
state := util.RandStringBytes(32)
|
|
|
|
|
|
|
|
sess.AuthorizationEndpoint = authURL.String()
|
|
|
|
sess.Me = urlString
|
|
|
|
sess.LoggedIn = false
|
|
|
|
sess.RedirectURI = RedirectURI
|
|
|
|
sess.NextURI = "/"
|
|
|
|
sess.State = state
|
|
|
|
|
|
|
|
newURL := indieauth.CreateAuthenticationURL(*authURL, urlString, ClientID, RedirectURI, state)
|
|
|
|
|
|
|
|
http.Redirect(w, r, newURL, 302)
|
|
|
|
return
|
|
|
|
} else if r.URL.Path == "/auth/logout" {
|
|
|
|
sess.LoggedIn = false
|
|
|
|
sess.Me = ""
|
|
|
|
sess.AuthorizationEndpoint = ""
|
|
|
|
http.Redirect(w, r, "/", 302)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *historyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
sess, err := NewSession(w, r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2019-02-25 18:03:04 +00:00
|
|
|
defer func() {
|
|
|
|
if err := sess.Flush(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
2018-11-24 12:34:51 +00:00
|
|
|
|
2019-02-05 20:22:16 +00:00
|
|
|
if !sess.LoggedIn {
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:34:51 +00:00
|
|
|
r.ParseForm()
|
|
|
|
page := r.URL.Path[9:]
|
|
|
|
if page == "" {
|
|
|
|
page = "Home"
|
|
|
|
}
|
|
|
|
|
|
|
|
history, err := mp.PageHistory(page)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
funcs := template.FuncMap{
|
2019-02-05 20:22:16 +00:00
|
|
|
"historyIndex": func(x int, history []Revision) int { return len(history) - x },
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t, err := template.New("layout.html").Funcs(funcs).ParseFiles("templates/layout.html", "templates/history.html")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = t.Execute(w, historyPage{
|
|
|
|
Session: sess,
|
2020-05-13 20:36:03 +00:00
|
|
|
Title: "History of " + cleanTitle(page),
|
2018-11-24 12:34:51 +00:00
|
|
|
Name: page,
|
|
|
|
History: history,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *saveHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
sess, err := NewSession(w, r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2019-02-25 18:03:04 +00:00
|
|
|
defer func() {
|
|
|
|
if err := sess.Flush(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
2018-11-24 12:34:51 +00:00
|
|
|
|
2019-02-05 20:22:16 +00:00
|
|
|
if !sess.LoggedIn {
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-25 10:30:00 +00:00
|
|
|
err = r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
isJson := r.PostForm.Get("json") == "1"
|
2018-11-24 12:34:51 +00:00
|
|
|
page := r.PostForm.Get("p")
|
|
|
|
summary := r.PostForm.Get("summary")
|
|
|
|
|
2019-02-19 06:34:52 +00:00
|
|
|
pageData := Page{Content: r.PostForm.Get("content")}
|
|
|
|
err = mp.Save(page, pageData, summary, sess.Me)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2019-08-25 10:30:00 +00:00
|
|
|
|
|
|
|
if isJson {
|
|
|
|
fmt.Print(w, "{}")
|
|
|
|
} else {
|
|
|
|
http.Redirect(w, r, "/"+page, http.StatusFound)
|
|
|
|
}
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *editHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
sess, err := NewSession(w, r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2019-02-25 18:03:04 +00:00
|
|
|
defer func() {
|
|
|
|
if err := sess.Flush(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
2018-11-24 12:34:51 +00:00
|
|
|
|
2019-02-05 20:22:16 +00:00
|
|
|
if !sess.LoggedIn {
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:34:51 +00:00
|
|
|
page := r.URL.Path[6:]
|
|
|
|
if page == "" {
|
|
|
|
page = "Home"
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:21:39 +00:00
|
|
|
mpPage := mp.Get(page)
|
|
|
|
pageText := mpPage.Content
|
2018-11-24 12:34:51 +00:00
|
|
|
|
2020-05-12 14:12:33 +00:00
|
|
|
if pageText == "" {
|
|
|
|
pageText = "[]"
|
|
|
|
}
|
|
|
|
|
|
|
|
var rawMsg json.RawMessage
|
|
|
|
err = json.NewDecoder(strings.NewReader(pageText)).Decode(&rawMsg)
|
|
|
|
jsonEditor := pageText != "" && err == nil
|
2019-08-25 10:30:00 +00:00
|
|
|
|
|
|
|
var editor template.HTML
|
|
|
|
if jsonEditor {
|
|
|
|
editor, err = renderEditor(page, pageText, "json")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2019-08-25 10:56:11 +00:00
|
|
|
editor = template.HTML(fmt.Sprintf(`
|
|
|
|
<textarea name="content" rows="24" style="width:100%%">%s</textarea>
|
2019-08-25 10:30:00 +00:00
|
|
|
<br>
|
|
|
|
|
|
|
|
<div class="field">
|
|
|
|
<label class="label">Summary</label>
|
|
|
|
<div class="control">
|
|
|
|
<input type="text" name="summary" class="input" placeholder="Summary"/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<button class="button is-primary" type="submit">Save</button>
|
2019-08-25 10:56:11 +00:00
|
|
|
`, html.EscapeString(pageText)))
|
2019-08-25 10:30:00 +00:00
|
|
|
}
|
|
|
|
|
2018-11-24 12:34:51 +00:00
|
|
|
data := editPage{
|
2020-05-08 15:21:39 +00:00
|
|
|
Session: sess,
|
2020-05-13 20:36:03 +00:00
|
|
|
Title: cleanTitle(page),
|
2020-05-08 15:21:39 +00:00
|
|
|
Content: pageText,
|
|
|
|
Editor: editor,
|
|
|
|
Name: page,
|
|
|
|
Backrefs: mpPage.Refs,
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t, err := template.ParseFiles("templates/layout.html", "templates/edit.html")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = t.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
sess, err := NewSession(w, r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2019-02-25 18:03:04 +00:00
|
|
|
defer func() {
|
|
|
|
if err := sess.Flush(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
2018-11-24 12:34:51 +00:00
|
|
|
|
|
|
|
page := r.URL.Path[1:]
|
|
|
|
if page == "" {
|
|
|
|
page = "Home"
|
|
|
|
}
|
|
|
|
|
2020-05-07 13:20:36 +00:00
|
|
|
mpPage := mp.Get(page)
|
|
|
|
pageText := mpPage.Content
|
2018-11-24 12:34:51 +00:00
|
|
|
if pageText == "" {
|
|
|
|
http.Redirect(w, r, "/edit/"+page, 302)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-12 14:12:33 +00:00
|
|
|
if pageText == "" {
|
|
|
|
pageText = "[]"
|
|
|
|
}
|
2018-11-24 12:34:51 +00:00
|
|
|
|
2020-05-12 14:12:33 +00:00
|
|
|
var rawMsg json.RawMessage
|
|
|
|
err = json.NewDecoder(strings.NewReader(pageText)).Decode(&rawMsg)
|
|
|
|
|
|
|
|
jsonPage := pageText != "" && err == nil
|
|
|
|
if jsonPage {
|
2020-05-11 21:34:10 +00:00
|
|
|
var listItems []struct {
|
|
|
|
Id int
|
|
|
|
Indented int
|
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewDecoder(strings.NewReader(pageText)).Decode(&listItems)
|
2019-08-25 10:30:00 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|
2020-05-11 21:34:10 +00:00
|
|
|
|
|
|
|
pageText = ""
|
|
|
|
for _, item := range listItems {
|
|
|
|
pageText += strings.Repeat(" ", item.Indented) + "* " + item.Text + "\n"
|
|
|
|
}
|
2019-08-25 10:30:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 20:36:03 +00:00
|
|
|
hrefRE, err := regexp.Compile(`#?\[\[\s*([^\]]+)\s*\]\]`)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-11-24 12:34:51 +00:00
|
|
|
|
2020-05-11 21:34:10 +00:00
|
|
|
pageText = hrefRE.ReplaceAllStringFunc(pageText, func(s string) string {
|
|
|
|
tag := false
|
|
|
|
if s[0] == '#' {
|
|
|
|
s = strings.TrimPrefix(s, "#[[")
|
|
|
|
tag = true
|
|
|
|
} else {
|
|
|
|
s = strings.TrimPrefix(s, "[[")
|
|
|
|
}
|
|
|
|
s = strings.TrimSuffix(s, "]]")
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
if tag {
|
2020-05-13 20:36:03 +00:00
|
|
|
return fmt.Sprintf(`<a href=%q class="tag">%s</a>`, cleanNameURL(s), s)
|
2020-05-11 21:34:10 +00:00
|
|
|
}
|
2020-05-13 20:36:03 +00:00
|
|
|
return fmt.Sprintf("[%s](/%s)", s, cleanNameURL(s))
|
2020-05-11 21:34:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
md := markdown.New(
|
|
|
|
markdown.HTML(true),
|
|
|
|
markdown.XHTMLOutput(true),
|
|
|
|
)
|
|
|
|
pageText = md.RenderToString([]byte(pageText))
|
2018-11-24 12:34:51 +00:00
|
|
|
|
|
|
|
data := indexPage{
|
2020-05-07 13:20:36 +00:00
|
|
|
Session: sess,
|
2020-05-13 20:36:03 +00:00
|
|
|
Title: cleanTitle(page),
|
2020-05-07 13:20:36 +00:00
|
|
|
Content: template.HTML(pageText),
|
|
|
|
Name: page,
|
|
|
|
Backrefs: mpPage.Refs,
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t, err := template.ParseFiles("templates/layout.html", "templates/view.html")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = t.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2019-03-03 07:30:04 +00:00
|
|
|
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 19:47:31 +00:00
|
|
|
func (h *recentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
sess, err := NewSession(w, r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2019-02-25 18:03:04 +00:00
|
|
|
defer func() {
|
|
|
|
if err := sess.Flush(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
2019-02-18 19:47:31 +00:00
|
|
|
|
|
|
|
if !sess.LoggedIn {
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r.ParseForm()
|
|
|
|
|
|
|
|
changes, err := mp.RecentChanges()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-02 13:24:52 +00:00
|
|
|
// group recentchanges on Page
|
|
|
|
if len(changes) > 0 {
|
2019-03-03 07:30:04 +00:00
|
|
|
f := groupRecentChanges(changes)
|
2019-03-02 13:24:52 +00:00
|
|
|
changes = changes[f:]
|
|
|
|
}
|
|
|
|
|
2019-03-02 13:38:35 +00:00
|
|
|
for i, c := range changes {
|
2019-03-03 07:36:22 +00:00
|
|
|
if c.Count <= 1 {
|
2019-03-02 13:38:35 +00:00
|
|
|
changes[i].DateSummary = c.Date.Format("Mon, 2 Jan 2006")
|
|
|
|
changes[i].TimeSummary = c.Date.Format("15:04")
|
|
|
|
} else {
|
|
|
|
changes[i].DateSummary = c.Date.Format("Mon, 2 Jan 2006")
|
|
|
|
changes[i].TimeSummary = fmt.Sprintf("%s - %s", c.Date.Format("15:04"), c.EndDate.Format("15:04"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 19:47:31 +00:00
|
|
|
t, err := template.New("layout.html").ParseFiles("templates/layout.html", "templates/recent.html")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = t.Execute(w, recentPage{
|
|
|
|
Session: sess,
|
|
|
|
Title: "Recent changes",
|
|
|
|
Name: "Recent changes",
|
2019-02-19 06:34:52 +00:00
|
|
|
Recent: changes,
|
2019-02-18 19:47:31 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 10:30:00 +00:00
|
|
|
type LinkResponseMetaImage struct {
|
|
|
|
Url string `json:"url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type LinkResponseMeta struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Image LinkResponseMetaImage `json:"image"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type LinkResponse struct {
|
|
|
|
Success int `json:"success"`
|
|
|
|
Link string `json:"link"`
|
|
|
|
Meta LinkResponseMeta `json:"meta"`
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:34:51 +00:00
|
|
|
func main() {
|
2019-02-18 19:47:55 +00:00
|
|
|
var port int
|
|
|
|
flag.IntVar(&port, "port", 8080, "http port")
|
|
|
|
flag.Parse()
|
|
|
|
|
2018-11-24 12:34:51 +00:00
|
|
|
mp = NewFilePages("data")
|
|
|
|
|
|
|
|
http.Handle("/auth/", &authHandler{})
|
2019-08-25 10:30:00 +00:00
|
|
|
http.HandleFunc("/fetchLink", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
link := r.URL.Query().Get("url")
|
|
|
|
u, err := url.Parse(link)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var response LinkResponse
|
|
|
|
response.Success = 1
|
|
|
|
response.Link = u.String()
|
|
|
|
response.Meta.Title = "Test"
|
|
|
|
json.NewEncoder(w).Encode(response)
|
|
|
|
})
|
2019-08-25 10:46:48 +00:00
|
|
|
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./dist"))))
|
2018-11-24 12:34:51 +00:00
|
|
|
http.Handle("/save/", &saveHandler{})
|
|
|
|
http.Handle("/edit/", &editHandler{})
|
|
|
|
http.Handle("/history/", &historyHandler{})
|
2019-02-18 19:47:31 +00:00
|
|
|
http.Handle("/recent/", &recentHandler{})
|
2018-11-24 12:34:51 +00:00
|
|
|
http.Handle("/", &indexHandler{})
|
|
|
|
|
2019-02-18 19:47:55 +00:00
|
|
|
fmt.Printf("Running on port %d\n", port)
|
|
|
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
|
2018-11-24 12:34:51 +00:00
|
|
|
}
|