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
|
flagDatabase string
|
||||||
flagListenPort int
|
flagListenPort int
|
||||||
flagSequenceFilename string
|
flagSequenceFilename string
|
||||||
flagDomain string
|
flagDomain string
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
@ -63,6 +63,12 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
if flagSequenceFilename == "" {
|
||||||
|
fmt.Println("Please specify -seq with a sequenc filename.")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
err := initDB(flagHost, flagPort, flagDatabase, flagUsername, flagPassword)
|
err := initDB(flagHost, flagPort, flagDatabase, flagUsername, flagPassword)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
|
@ -70,6 +76,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
listenOn := fmt.Sprintf("[::]:%d", flagListenPort)
|
listenOn := fmt.Sprintf("[::]:%d", flagListenPort)
|
||||||
|
http.HandleFunc("/", pageIndex)
|
||||||
http.HandleFunc("/invite", createInvite)
|
http.HandleFunc("/invite", createInvite)
|
||||||
fmt.Printf("Listen on %s\n", listenOn)
|
fmt.Printf("Listen on %s\n", listenOn)
|
||||||
err = http.ListenAndServe(listenOn, nil)
|
err = http.ListenAndServe(listenOn, nil)
|
||||||
|
|
@ -102,6 +109,50 @@ func sequenceNext() (seq int, err error) {
|
||||||
return
|
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) {
|
func createInvite(w http.ResponseWriter, r *http.Request) {
|
||||||
seq, err := sequenceNext()
|
seq, err := sequenceNext()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -113,57 +164,59 @@ func createInvite(w http.ResponseWriter, r *http.Request) {
|
||||||
email := fmt.Sprintf("%08d@example.com", seq)
|
email := fmt.Sprintf("%08d@example.com", seq)
|
||||||
expire := time.Now().Add(time.Hour * 24 * 7)
|
expire := time.Now().Add(time.Hour * 24 * 7)
|
||||||
|
|
||||||
// legacy_object
|
if false {
|
||||||
key := fmt.Sprintf("invitation:uid:2:invited:%s", email)
|
// legacy_object
|
||||||
_, err = db.Exec(` INSERT INTO public.legacy_object(_key, "type", "expireAt") VALUES($1, 'string', null)`, key)
|
key := fmt.Sprintf("invitation:uid:2:invited:%s", email)
|
||||||
if err != nil {
|
_, err = db.Exec(` INSERT INTO public.legacy_object(_key, "type", "expireAt") VALUES($1, 'string', null)`, key)
|
||||||
w.Write([]byte(err.Error()))
|
if err != nil {
|
||||||
return
|
w.Write([]byte(err.Error()))
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
key = fmt.Sprintf("invitation:invited:%s", email)
|
key = fmt.Sprintf("invitation:invited:%s", email)
|
||||||
_, err = db.Exec(` INSERT INTO public.legacy_object(_key, "type", "expireAt") VALUES($1, 'set', null)`, key)
|
_, err = db.Exec(` INSERT INTO public.legacy_object(_key, "type", "expireAt") VALUES($1, 'set', null)`, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
key = fmt.Sprintf("invitation:token:%s", newUUID)
|
key = fmt.Sprintf("invitation:token:%s", newUUID)
|
||||||
_, err = db.Exec(` INSERT INTO public.legacy_object(_key, "type", "expireAt") VALUES($1, 'hash', $2)`, key, expire)
|
_, err = db.Exec(` INSERT INTO public.legacy_object(_key, "type", "expireAt") VALUES($1, 'hash', $2)`, key, expire)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// legacy_hash
|
// legacy_hash
|
||||||
key = fmt.Sprintf("invitation:token:%s", newUUID)
|
key = fmt.Sprintf("invitation:token:%s", newUUID)
|
||||||
data := fmt.Sprintf(`{"email": "%s", "token": "%s", "inviter": 2, "groupsToJoin": "[]"}`, email, 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)
|
_, err = db.Exec(` INSERT INTO public.legacy_hash(_key, "data", "type") VALUES($1, $2, 'hash')`, key, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// legacy_set
|
// legacy_set
|
||||||
key = fmt.Sprintf("invitation:uid:2")
|
key = "invitation:uid:2"
|
||||||
_, err = db.Exec(` INSERT INTO public.legacy_set(_key, "member", "type") VALUES($1, $2, 'set')`, key, email)
|
_, err = db.Exec(` INSERT INTO public.legacy_set(_key, "member", "type") VALUES($1, $2, 'set')`, key, email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
key = fmt.Sprintf("invitation:invited:%s", email)
|
key = fmt.Sprintf("invitation:invited:%s", email)
|
||||||
_, err = db.Exec(` INSERT INTO public.legacy_set(_key, "member", "type") VALUES($1, $2, 'set')`, key, newUUID)
|
_, err = db.Exec(` INSERT INTO public.legacy_set(_key, "member", "type") VALUES($1, $2, 'set')`, key, newUUID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
key = fmt.Sprintf("invitation:uid:2:invited:%s", email)
|
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)
|
_, err = db.Exec(` INSERT INTO public.legacy_string(_key, "data", "type") VALUES($1, $2, 'string')`, key, newUUID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
return
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
j, _ := json.Marshal(struct {
|
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),
|
fmt.Sprintf("https://%s/register?token=%s", flagDomain, newUUID),
|
||||||
})
|
})
|
||||||
w.Write(j)
|
w.Write(j)
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue