Fixed HTML page
This commit is contained in:
parent
94512dff5e
commit
66c1c2198b
1 changed files with 99 additions and 48 deletions
147
main.go
147
main.go
|
|
@ -26,7 +26,7 @@ var (
|
|||
flagDatabase string
|
||||
flagListenPort int
|
||||
flagSequenceFilename string
|
||||
flagDomain string
|
||||
flagDomain string
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
|
@ -63,6 +63,12 @@ func init() {
|
|||
}
|
||||
|
||||
func main() {
|
||||
if flagSequenceFilename == "" {
|
||||
fmt.Println("Please specify -seq with a sequenc filename.")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
|
||||
err := initDB(flagHost, flagPort, flagDatabase, flagUsername, flagPassword)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
|
@ -70,6 +76,7 @@ func main() {
|
|||
}
|
||||
|
||||
listenOn := fmt.Sprintf("[::]:%d", flagListenPort)
|
||||
http.HandleFunc("/", pageIndex)
|
||||
http.HandleFunc("/invite", createInvite)
|
||||
fmt.Printf("Listen on %s\n", listenOn)
|
||||
err = http.ListenAndServe(listenOn, nil)
|
||||
|
|
@ -102,6 +109,50 @@ func sequenceNext() (seq int, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func pageIndex(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(`<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
|
||||
</head>
|
||||
<body style="margin: 32px">
|
||||
<div style="margin-bottom: 32px"><img src="/images/logo.svg"></div>
|
||||
<div>Skapar länk...</div>
|
||||
|
||||
<div id="link"></div>
|
||||
|
||||
<script type="text/javascript">
|
||||
fetch('./invite')
|
||||
.then(data=>data.json())
|
||||
.then(json=>{
|
||||
const el = document.getElementById('link')
|
||||
|
||||
const desc = document.createElement('div')
|
||||
desc.style.marginTop = '32px'
|
||||
desc.innerText = 'Klicka på länken (den kopieras automatiskt) och skicka till den nya medlemmen: '
|
||||
|
||||
const l = document.createElement('div')
|
||||
l.innerText = json.Link
|
||||
l.style.cursor = 'pointer'
|
||||
l.style.marginTop = '8px'
|
||||
l.style.color = '#782144'
|
||||
l.addEventListener('click', event=>{
|
||||
navigator.clipboard.writeText(json.Link)
|
||||
event.target.style.color = '#6c31d7'
|
||||
setTimeout(()=>event.target.style.color = '#782144', 250)
|
||||
})
|
||||
|
||||
el.append(desc)
|
||||
el.append(l)
|
||||
})
|
||||
.catch(err=>alert(err))
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
`))
|
||||
}
|
||||
|
||||
func createInvite(w http.ResponseWriter, r *http.Request) {
|
||||
seq, err := sequenceNext()
|
||||
if err != nil {
|
||||
|
|
@ -113,57 +164,59 @@ func createInvite(w http.ResponseWriter, r *http.Request) {
|
|||
email := fmt.Sprintf("%08d@example.com", seq)
|
||||
expire := time.Now().Add(time.Hour * 24 * 7)
|
||||
|
||||
// legacy_object
|
||||
key := fmt.Sprintf("invitation:uid:2:invited:%s", email)
|
||||
_, err = db.Exec(` INSERT INTO public.legacy_object(_key, "type", "expireAt") VALUES($1, 'string', null)`, key)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
if false {
|
||||
// legacy_object
|
||||
key := fmt.Sprintf("invitation:uid:2:invited:%s", email)
|
||||
_, err = db.Exec(` INSERT INTO public.legacy_object(_key, "type", "expireAt") VALUES($1, 'string', null)`, key)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
key = fmt.Sprintf("invitation:invited:%s", email)
|
||||
_, err = db.Exec(` INSERT INTO public.legacy_object(_key, "type", "expireAt") VALUES($1, 'set', null)`, key)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
key = fmt.Sprintf("invitation:invited:%s", email)
|
||||
_, err = db.Exec(` INSERT INTO public.legacy_object(_key, "type", "expireAt") VALUES($1, 'set', null)`, key)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
key = fmt.Sprintf("invitation:token:%s", newUUID)
|
||||
_, err = db.Exec(` INSERT INTO public.legacy_object(_key, "type", "expireAt") VALUES($1, 'hash', $2)`, key, expire)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
key = fmt.Sprintf("invitation:token:%s", newUUID)
|
||||
_, err = db.Exec(` INSERT INTO public.legacy_object(_key, "type", "expireAt") VALUES($1, 'hash', $2)`, key, expire)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// legacy_hash
|
||||
key = fmt.Sprintf("invitation:token:%s", newUUID)
|
||||
data := fmt.Sprintf(`{"email": "%s", "token": "%s", "inviter": 2, "groupsToJoin": "[]"}`, email, newUUID)
|
||||
_, err = db.Exec(` INSERT INTO public.legacy_hash(_key, "data", "type") VALUES($1, $2, 'hash')`, key, data)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
// legacy_hash
|
||||
key = fmt.Sprintf("invitation:token:%s", newUUID)
|
||||
data := fmt.Sprintf(`{"email": "%s", "token": "%s", "inviter": 2, "groupsToJoin": "[]"}`, email, newUUID)
|
||||
_, err = db.Exec(` INSERT INTO public.legacy_hash(_key, "data", "type") VALUES($1, $2, 'hash')`, key, data)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// legacy_set
|
||||
key = fmt.Sprintf("invitation:uid:2")
|
||||
_, err = db.Exec(` INSERT INTO public.legacy_set(_key, "member", "type") VALUES($1, $2, 'set')`, key, email)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
// legacy_set
|
||||
key = "invitation:uid:2"
|
||||
_, err = db.Exec(` INSERT INTO public.legacy_set(_key, "member", "type") VALUES($1, $2, 'set')`, key, email)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
key = fmt.Sprintf("invitation:invited:%s", email)
|
||||
_, err = db.Exec(` INSERT INTO public.legacy_set(_key, "member", "type") VALUES($1, $2, 'set')`, key, newUUID)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
key = fmt.Sprintf("invitation:invited:%s", email)
|
||||
_, err = db.Exec(` INSERT INTO public.legacy_set(_key, "member", "type") VALUES($1, $2, 'set')`, key, newUUID)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
key = fmt.Sprintf("invitation:uid:2:invited:%s", email)
|
||||
_, err = db.Exec(` INSERT INTO public.legacy_string(_key, "data", "type") VALUES($1, $2, 'string')`, key, newUUID)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
key = fmt.Sprintf("invitation:uid:2:invited:%s", email)
|
||||
_, err = db.Exec(` INSERT INTO public.legacy_string(_key, "data", "type") VALUES($1, $2, 'string')`, key, newUUID)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
j, _ := json.Marshal(struct {
|
||||
|
|
@ -172,6 +225,4 @@ func createInvite(w http.ResponseWriter, r *http.Request) {
|
|||
fmt.Sprintf("https://%s/register?token=%s", flagDomain, newUUID),
|
||||
})
|
||||
w.Write(j)
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue