Skip to content

Commit

Permalink
feat(webui): login page is now styled
Browse files Browse the repository at this point in the history
login page is now styled from a file, binpacked by pkger
  • Loading branch information
gnur committed Sep 10, 2020
1 parent ecb3ef2 commit bca465c
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ dist/*
request.http
bin/deploy
ubby.toml
pkged.go
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ go 1.15
require (
github.com/BurntSushi/toml v0.3.1
github.com/caddyserver/certmagic v0.11.2
github.com/davecgh/go-spew v1.1.1
github.com/gorilla/handlers v1.5.0
github.com/gorilla/mux v1.8.0
github.com/kr/pretty v0.2.0 // indirect
github.com/markbates/goth v1.64.2
github.com/markbates/pkger v0.17.1
github.com/o1egl/paseto v1.0.0
github.com/ryanuber/go-glob v1.0.0
github.com/sirupsen/logrus v1.6.0
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI=
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down Expand Up @@ -198,6 +200,8 @@ github.com/liquidweb/liquidweb-go v1.6.0/go.mod h1:UDcVnAMDkZxpw4Y7NOHkqoeiGacVL
github.com/markbates/going v1.0.0/go.mod h1:I6mnB4BPnEeqo85ynXIx1ZFLLbtiLHNXVgWeFO9OGOA=
github.com/markbates/goth v1.64.2 h1:HDFwyuB6/ATU1USTvd/Rb3C9XE0VAxeuciSz+aUZHHA=
github.com/markbates/goth v1.64.2/go.mod h1:hSFJFfH56BfFCX4+hBIxyd3o5VzuH5rNwKVRsFr/JPk=
github.com/markbates/pkger v0.17.1 h1:/MKEtWqtc0mZvu9OinB9UzVN9iYCwLWuyUv4Bw+PCno=
github.com/markbates/pkger v0.17.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
Expand Down Expand Up @@ -516,6 +520,7 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
29 changes: 4 additions & 25 deletions goth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package main

import (
"net/http"
"text/template"
"time"

"github.com/davecgh/go-spew/spew"
"github.com/gorilla/mux"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
Expand Down Expand Up @@ -53,8 +51,6 @@ func (app *Tobab) setupgoth(r *mux.Router) {

http.SetCookie(w, &c)

spew.Dump(user)

cr, err := r.Cookie("X-Tobab-Source")
if err != nil {
http.Redirect(w, r, "/", 302)
Expand All @@ -65,15 +61,17 @@ func (app *Tobab) setupgoth(r *mux.Router) {
})

r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
t, _ := template.New("foo").Parse(indexTemplate)
user, err := app.extractUser(r)
providerIndex := &ProviderIndex{Providers: []string{"google"}, ProvidersMap: map[string]string{"google": "Google"}}
if err == nil {
providerIndex.User = user
} else {
app.logger.WithError(err).Error("unable to get user from request")
}
t.Execute(w, providerIndex)
err = app.templates.ExecuteTemplate(w, "index.html", providerIndex)
if err != nil {
app.logger.WithError(err).Error("failed executing template")
}
})
}

Expand All @@ -82,22 +80,3 @@ type ProviderIndex struct {
ProvidersMap map[string]string
User string
}

var indexTemplate = `<h1>Hi {{.User}}</h1><br/>
{{range $key,$value:=.Providers}}
<p><a href="/auth/{{$value}}">Log in with {{index $.ProvidersMap $value}}</a></p>
{{end}}`

var userTemplate = `
<p><a href="/logout/{{.Provider}}">logout</a></p>
<p>Name: {{.Name}} [{{.LastName}}, {{.FirstName}}]</p>
<p>Email: {{.Email}}</p>
<p>NickName: {{.NickName}}</p>
<p>Location: {{.Location}}</p>
<p>AvatarURL: {{.AvatarURL}} <img src="{{.AvatarURL}}"></p>
<p>Description: {{.Description}}</p>
<p>UserID: {{.UserID}}</p>
<p>AccessToken: {{.AccessToken}}</p>
<p>ExpiresAt: {{.ExpiresAt}}</p>
<p>RefreshToken: {{.RefreshToken}}</p>
`
17 changes: 12 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"crypto/rand"
"fmt"
"html/template"
"net"
"net/http"
"net/http/httputil"
Expand All @@ -22,11 +23,12 @@ import (
var salt = []byte("kcqbBue2Sr7U5yrpEaZFpGVdR6z4jfUeSECy6zuYDXktgxhFCxMtEkV9")

type Tobab struct {
fqdn string
key []byte
config Config
logger *logrus.Entry
maxAge time.Duration
fqdn string
key []byte
config Config
logger *logrus.Entry
maxAge time.Duration
templates *template.Template
}

func main() {
Expand Down Expand Up @@ -89,6 +91,11 @@ func main() {
fqdn: "https://" + cfg.Hostname,
}

app.templates, err = loadTemplates()
if err != nil {
logger.WithError(err).Fatal("unable to load templates")
}

r := mux.NewRouter()
hosts := []string{app.config.Hostname}

Expand Down
117 changes: 117 additions & 0 deletions template_functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package main

import (
"encoding/json"
"fmt"
"html/template"
"reflect"
"time"
)

var templateFunctions = template.FuncMap{
"percent": func(a, b int) float64 {
return float64(a) / float64(b) * 100
},
"safeHTML": func(s interface{}) template.HTML {
return template.HTML(fmt.Sprint(s))
},
"crop": func(s string, i int) string {
if len(s) > i {
return s[0:(i-2)] + "..."
}
return s
},
"add": func(b, a interface{}) (interface{}, error) {
av := reflect.ValueOf(a)
bv := reflect.ValueOf(b)

switch av.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
switch bv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return av.Int() + bv.Int(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return av.Int() + int64(bv.Uint()), nil
case reflect.Float32, reflect.Float64:
return float64(av.Int()) + bv.Float(), nil
default:
return nil, fmt.Errorf("add: unknown type for %q (%T)", bv, b)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
switch bv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return int64(av.Uint()) + bv.Int(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return av.Uint() + bv.Uint(), nil
case reflect.Float32, reflect.Float64:
return float64(av.Uint()) + bv.Float(), nil
default:
return nil, fmt.Errorf("add: unknown type for %q (%T)", bv, b)
}
case reflect.Float32, reflect.Float64:
switch bv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return av.Float() + float64(bv.Int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return av.Float() + float64(bv.Uint()), nil
case reflect.Float32, reflect.Float64:
return av.Float() + bv.Float(), nil
default:
return nil, fmt.Errorf("add: unknown type for %q (%T)", bv, b)
}
default:
return nil, fmt.Errorf("add: unknown type for %q (%T)", av, a)
}
},
"prettyTime": func(s interface{}) template.HTML {
t, ok := s.(time.Time)
if !ok {
return ""
}
if t.IsZero() {
return template.HTML("never")
}
return template.HTML(t.Format("2006-01-02 15:04:05"))
},
"json": func(s interface{}) template.HTML {
json, _ := json.MarshalIndent(s, "", " ")
return template.HTML(string(json))
},
"relativeTime": func(s interface{}) template.HTML {
t, ok := s.(time.Time)
if !ok {
return ""
}
if t.IsZero() {
return template.HTML("never")
}
tense := "ago"
diff := time.Since(t)
seconds := int64(diff.Seconds())
if seconds < 0 {
tense = "from now"
}
var quantifier string

if seconds < 60 {
quantifier = "s"
} else if seconds < 3600 {
quantifier = "m"
seconds /= 60
} else if seconds < 86400 {
quantifier = "h"
seconds /= 3600
} else if seconds < 604800 {
quantifier = "d"
seconds /= 86400
} else if seconds < 31556736 {
quantifier = "w"
seconds /= 604800
} else {
quantifier = "y"
seconds /= 31556736
}

return template.HTML(fmt.Sprintf("%v%s %s", seconds, quantifier, tense))
},
}
35 changes: 35 additions & 0 deletions templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"html/template"
"io/ioutil"
"os"
"strings"

"github.com/markbates/pkger"
)

func loadTemplates() (*template.Template, error) {
tpl := template.New("")
tpl.Funcs(templateFunctions)

err := pkger.Walk("/templates", func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".html") {
f, err := pkger.Open(path)
if err != nil {
return err
}
sl, err := ioutil.ReadAll(f)
if err != nil {
return err
}
_, err = tpl.Parse(string(sl))
if err != nil {
return err
}
}
return nil
})

return tpl, err
}
64 changes: 64 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{{define "index.html"}}
<!DOCTYPE html>
<html>

<head>
<title>tobab</title>
</head>

<style>
html,
body {
height: 100%;
}

body {
margin: 0;
background-color: rgb(15, 0, 36);
}

.flex-container {
height: 100%;
margin: 0;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
align-items: center;
justify-content: center;
}

.row {
font-family: Helvetica, Arial, sans-serif;
color: #FFFFFF;
width: auto;
background-color: rgb(58, 40, 61);
padding: 50px;
border-radius: 20px;
}

a,
a:hover,
a:visited,
a:valid {
color: rgb(247, 56, 253);
text-decoration: none;
font-weight: bold;
font-size: 2em;
}
</style>

<body>
<div class="flex-container">
<div class="row">
<h1>Hi {{.User}}</h1><br />
{{range $key,$value:=.Providers}}
<p><a href="/auth/{{$value}}">Log in with {{index $.ProvidersMap $value}}</a></p>
{{end}}
</div>
</div>
</body>

</html>
{{end}}

0 comments on commit bca465c

Please sign in to comment.