Compare commits

...

11 Commits

7 changed files with 180 additions and 24 deletions

View File

@ -2,7 +2,7 @@ pipeline:
build:
image: golang
commands:
- go get github.com/pstuifzand/microsub-server/cmd/eksterd
- go get github.com/pstuifzand/microsub-server/...
- go build github.com/pstuifzand/microsub-server/cmd/eksterd
publish:

View File

@ -1,5 +1,8 @@
FROM ubuntu
RUN apt-get -y update && apt-get install -y ca-certificates
RUN ["mkdir", "/usr/share/eksterd"]
ADD ./eksterd /usr/local/bin
ADD ./templates /usr/share/eksterd
EXPOSE 80
ENV EKSTER_TEMPLATES "/usr/share/eksterd"
ENTRYPOINT ["/usr/local/bin/eksterd"]

View File

@ -1,7 +1,7 @@
# build stage
FROM golang:1.10.2-alpine3.7 AS build-env
RUN apk --no-cache add git
RUN go get github.com/pstuifzand/ekster/cmd/eksterd
RUN go get github.com/pstuifzand/ekster/...
# final stage
FROM alpine
@ -10,5 +10,6 @@ RUN ["mkdir", "-p", "/opt/micropub"]
WORKDIR /opt/micropub
EXPOSE 80
COPY --from=build-env /go/bin/eksterd /app/
COPY --from=build-env /go/bin/ek /app/
RUN ["mkdir", "/app/templates"]
COPY --from=build-env /go/src/github.com/pstuifzand/ekster/templates /app/templates
ENTRYPOINT ["/app/eksterd"]

View File

@ -131,7 +131,8 @@ func (h *hubIncomingBackend) GetFeeds() []Feed {
defer conn.Close()
feeds := []Feed{}
feedKeys, err := redis.Strings(conn.Do("KEYS feed:*"))
// FIXME(peter): replace with set of currently checked feeds
feedKeys, err := redis.Strings(conn.Do("KEYS", "feed:*"))
if err != nil {
log.Println(err)
return feeds

View File

@ -21,6 +21,7 @@ import (
"encoding/json"
"flag"
"fmt"
"html/template"
"io"
"log"
"net/http"
@ -68,17 +69,24 @@ type authResponse struct {
Me string `json:"me"`
}
type indexPage struct {
Session session
}
type settingsPage struct {
Session session
}
func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
conn := pool.Get()
defer conn.Close()
err := r.ParseForm()
if err != nil {
log.Println(err)
http.Error(w, fmt.Sprintf("Bad Request: %s", err.Error()), 400)
return
}
if r.Method == http.MethodGet {
if r.URL.Path == "/" {
c, err := r.Cookie("session")
@ -109,27 +117,22 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
fmt.Fprintln(w, "<h1>Ekster - Microsub server</h1>")
fmt.Fprintln(w, `<p><a href="/settings">Settings</a></p>`)
if sess.LoggedIn {
fmt.Fprintf(w, "SUCCESS Me = %s", sess.Me)
fmt.Fprintln(w, `
<h2>Logout</h2>
<form action="/auth/logout" method="post">
<button type="submit">Logout</button>
</form>
`)
} else {
fmt.Fprintln(w, `
<h2>Sign in to Ekster</h2>
<form action="/auth" method="post">
<input type="text" name="url" placeholder="https://example.com/">
<button type="submit">Login</button>
</form>
`)
t, err := template.ParseFiles(
os.Getenv("EKSTER_TEMPLATES") + "/index.html",
)
if err != nil {
fmt.Fprintf(w, "ERROR: %q\n", err)
return
}
var page indexPage
page.Session = sess
err = t.ExecuteTemplate(w, "index.html", page)
if err != nil {
fmt.Fprintf(w, "ERROR: %q\n", err)
return
}
return
} else if r.URL.Path == "/auth/callback" {
c, err := r.Cookie("session")
@ -203,6 +206,43 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "ERROR: HTTP response code from authorization_endpoint (%s) %d \n", sess.AuthorizationEndpoint, resp.StatusCode)
return
}
} else if r.URL.Path == "/settings" {
c, err := r.Cookie("session")
if err == http.ErrNoCookie {
http.Redirect(w, r, "/", 302)
return
}
sessionVar := c.Value
var sess session
sessionKey := "session:" + sessionVar
data, err := redis.Values(conn.Do("HGETALL", sessionKey))
if err != nil {
fmt.Fprintf(w, "ERROR: %q\n", err)
return
}
err = redis.ScanStruct(data, &sess)
if err != nil {
fmt.Fprintf(w, "ERROR: %q\n", err)
return
}
t, err := template.ParseFiles(
os.Getenv("EKSTER_TEMPLATES") + "/settings.html",
)
if err != nil {
fmt.Fprintf(w, "ERROR: %q\n", err)
return
}
var page settingsPage
page.Session = sess
err = t.ExecuteTemplate(w, "settings.html", page)
if err != nil {
fmt.Fprintf(w, "ERROR: %q\n", err)
return
}
return
}
} else if r.Method == http.MethodPost {
if r.URL.Path == "/auth" {

65
templates/index.html Normal file
View File

@ -0,0 +1,65 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ekster</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css">
</head>
<body>
<section class="section">
<div class="container">
<nav class="navbar" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<a class="navbar-item" href="/">
Ekster
</a>
<a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false" data-target="menu">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
</div>
{{ if .Session.LoggedIn }}
<div id="menu" class="navbar-menu">
<a class="navbar-item" href="/settings">
Settings
</a>
<a class="navbar-item" href="{{ .Session.Me }}">
Profile
</a>
</div>
{{ end }}
</nav>
<h1 class="title">Ekster - Microsub server</h1>
{{ if .Session.LoggedIn }}
<h2 class="title">Logout</h2>
<form action="/auth/logout" method="post">
<button type="submit" class="button is-info">Logout</button>
</form>
{{ else }}
<h2 class="title">Sign in to Ekster</h2>
<form action="/auth" method="post">
<div class="field">
<label class="label" for="url"></label>
<div class="control">
<input type="text" name="url" id="url" class="input" placeholder="https://example.com/">
</div>
</div>
<div class="field is-grouped">
<div class="control">
<button type="submit" class="button is-info">Login</button>
</div>
</div>
</form>
{{ end }}
</div>
</section>
</body>
</html>

46
templates/settings.html Normal file
View File

@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ekster</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css">
</head>
<body>
<section class="section">
<div class="container">
<nav class="navbar" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<a class="navbar-item" href="/">
Ekster
</a>
<a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false" data-target="menu">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
</div>
{{ if .Session.LoggedIn }}
<div id="menu" class="navbar-menu">
<a class="navbar-item" href="/settings">
Settings
</a>
<a class="navbar-item" href="{{ .Session.Me }}">
Profile
</a>
</div>
{{ end }}
</nav>
<h1 class="title">Ekster - Microsub server</h1>
{{ if .Session.LoggedIn }}
{{ end }}
</div>
</section>
</body>
</html>