Add templates

This commit is contained in:
Peter Stuifzand 2018-07-09 21:59:26 +02:00
parent 8ef9e31446
commit fe88320675
4 changed files with 62 additions and 21 deletions

View File

@ -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

@ -21,6 +21,7 @@ import (
"encoding/json"
"flag"
"fmt"
"html/template"
"io"
"log"
"net/http"
@ -68,17 +69,21 @@ type authResponse struct {
Me string `json:"me"`
}
type indexPage 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 +114,12 @@ 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>`)
t, err := template.ParseFiles("templates/index.html", "templates/settings.html")
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>
`)
}
var page indexPage
page.Session = sess
err = t.ExecuteTemplate(w, "index", page)
return
} else if r.URL.Path == "/auth/callback" {
c, err := r.Cookie("session")

23
templates/index.html Normal file
View File

@ -0,0 +1,23 @@
<html>
<head>
<title>Ekster</title>
</head>
<body>
<h1>Ekster - Microsub server</h1>
<p><a href="/settings">Settings</a></p>
{{ if .Session.LoggedIn }}
SUCCESS Me = {{ .Session.Me }}
<h2>Logout</h2>
<form action="/auth/logout" method="post">
<button type="submit">Logout</button>
</form>
{{ else }}
<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>
</body>
</html>

27
templates/settings.html Normal file
View File

@ -0,0 +1,27 @@
<html>
<head>
<title>Settings - Ekster</title>
<style>
* { box-sizing: border-box; margin:0; padding:0; }
body {
background: #efefef;
font-family: sans-serif;
}
.container {
padding: 12px;
background: white;
max-width: 800px;
margin: 30px auto;
}
</style>
</head>
<body>
<div class="container">
<h1>Ekster</h1>
<h2>Settings</h2>
<p>Not yet implemented</p>
</div>
</body>
</html>