Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server:ws): adding htmp landing page for relay. #84

Merged
merged 3 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ linters:
- errname
- errorlint
- exhaustive
- exportloopref
- copyloopvar
- forbidigo
- gci
- ginkgolinter
Expand Down
4 changes: 1 addition & 3 deletions config/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ func (c *Config) LoadParameters(db *database.Database) error {
return err
}

_, updateErr := coll.UpdateOne(ctx, filter, &Parameters{
Version: immortal.StringVersion(),
})
_, updateErr := coll.UpdateOne(ctx, filter, bson.D{{"$set", bson.D{{"version", immortal.StringVersion()}}}}) //nolint

if updateErr != nil {
return updateErr
Expand Down
267 changes: 267 additions & 0 deletions server/websocket/landing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
<!DOCTYPE html>
<html>

<head>

<title>{{.Name}}</title>
<link rel="icon" href="{{.Icon}}" type="image/png">

<style>
/* CSS Reset */
html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
a,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
b,
u,
i,
center,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
canvas,
details,
embed,
figure,
figcaption,
footer,
header,
hgroup,
menu,
nav,
output,
ruby,
section,
summary,
time,
mark,
audio,
video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}

/* HTML5 display-role reset for older browsers */
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
display: block;
}

body {
line-height: 1;
padding-left: 0; /* No padding on the left */
}

ol,
ul {
list-style: none;
}

blockquote,
q {
quotes: none;
}

blockquote:before,
blockquote:after,
q:before,
q:after {
content: '';
content: none;
}

table {
border-collapse: collapse;
border-spacing: 0;
width: 100%; /* Full width tables */
}

/* Our stylesheet */
body {
font-family: sans-serif;
font-size: 135%;
}

.header {
font-size: 150%;
text-align: center;
margin-top: 10px; /* Reduce margin top */
margin-bottom: 10px; /* Reduce margin bottom */
font-weight: bold;

display: flex;
flex-direction: row;
align-items: center;
justify-content: space-around;
}

.header img {
width: 150px;
height: 150px; /* Maintain height for the image */
}

.header span {
margin-left: 10px; /* Optional space between image and text */
}

table th {
color: white;
background-color: black;
font-size: 110%;
}

table tr {
height: 30px; /* Add height to rows for spacing */
}

table tr td {
padding: 0; /* Remove padding from all table cells */
}

table tr td:nth-child(1) {
width: 20%;
text-align: left; /* Align field names to the left */
padding-right: 0; /* No right padding */
}

table tr td:nth-child(2) {
font-weight: bold;
padding-left: 0; /* Remove left padding */
}

table tr:nth-child(even) {
background-color: #f2f2f2;
}

table td,
th {
padding: 8px 5px; /* Consistent padding for cell content */
}

/* Customize spacing between rows */
table tr + tr {
margin-top: 10px; /* Space between rows */
}
</style>
</head>

<body>
<div class="header">
<a href="https://github.com/dezh-tech/immortal"><img src="{{.Icon}}" /></a>
<span>Please use a <a href="https://github.com/aljazceru/awesome-nostr#clients">Nostr client</a> to connect.</span>
</div>

<table>
<tr>
<th colspan="2">Relay</th>
</tr>

<tr>
<td>Name</td>
<td>{{.Name}}</td>
</tr>

<tr>
<td>Description</td>
<td>{{.Description}}</td>
</tr>

<tr>
<td>Pubkey</td>
<td><a href="https://njump.me/{{.PubKey}}">{{.PubKey}}</a></td>
</tr>

<tr>
<td>Contact</td>
<td>{{.Contact}}</td>
</tr>
</table>

<table>
<tr>
<th colspan="2">Server Software</th>
</tr>

<tr>
<td>Software</td>
<td><a href="https://github.com/dezh-tech/immortal">immortal</a></td>
</tr>

<tr>
<td>Version</td>
<td>{{.Version}}</td>
</tr>

<tr>
<td>Supported NIPs</td>
<td>{{.SupportedNIPs}}</td>
</tr>
</table>
</body>

</html>
32 changes: 29 additions & 3 deletions server/websocket/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package websocket

import (
"context"
_ "embed"
"encoding/json"
"fmt"
"html/template"
"log"
"net"
"net/http"
Expand All @@ -19,9 +21,14 @@ import (
"github.com/gorilla/websocket"
)

var upgrader = websocket.Upgrader{
CheckOrigin: func(_ *http.Request) bool { return true },
}
var (
upgrader = websocket.Upgrader{
CheckOrigin: func(_ *http.Request) bool { return true },
}

//go:embed landing.html
landingTempl []byte
)

// Server represents a websocket serer which keeps track of client connections and handle them.
type Server struct {
Expand Down Expand Up @@ -71,6 +78,25 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

if r.Header.Get("Upgrade") == "" {
t := template.New("webpage")
t, err := t.Parse(string(landingTempl))
if err != nil {
http.Error(w, "Error parsing template", http.StatusInternalServerError)

return
}

err = t.Execute(w, s.nip11Doc)
if err != nil {
http.Error(w, "Error executing template", http.StatusInternalServerError)

return
}

return
}

conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
Expand Down