Skip to content

Instantly share code, notes, and snippets.

@recoilme
Created March 24, 2021 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save recoilme/14056fddb357bd49436525b3739ba55b to your computer and use it in GitHub Desktop.
Save recoilme/14056fddb357bd49436525b3739ba55b to your computer and use it in GitHub Desktop.
server for html parsing with readability
package main
import (
"fmt"
"net/http"
"github.com/recoilme/clean"
. "github.com/stevelacy/daz"
)
func main() {
http.HandleFunc("/", mainHandler)
fmt.Println("listening on :8081")
http.ListenAndServe(":8081", nil)
}
func mainHandler(w http.ResponseWriter, r *http.Request) {
title := "Example Server"
meta := []func() string{
H("meta", Attr{"charset": "UTF-8"}),
H("meta", Attr{
"name": "viewport",
"content": "width=device-width, initial-scale=1.0",
}),
}
css := `body{padding:1rem;margin:auto;max-width:46rem;font-family:pt serif,Georgia,Cambria,times new roman,Times,serif;line-height:1.5;font-size:20px;color:rgba(0,0,0,.87);-webkit-font-smoothing:antialiased;font-weight:300}nav{display:flex;justify-content:space-between;align-items:center;margin:1em 0 3em}nav ul,nav li{display:inline-block;list-style:none;margin:0 0 0 .5rem;padding:0}nav ul{margin-right:1rem}.logo{background-color:rgba(0,0,0,.87);color:#fff;min-width:48px;min-height:48px;font-size:28px;border-radius:2px;display:flex;justify-content:center;align-items:center}.logo:hover{color:#fff}h1,h2{line-height:1.2;font-family:roboto,system-ui,segoe ui,Helvetica,Arial,sans-serif;font-weight:400;text-transform:uppercase;margin:1.34em 0 0}h1{font-size:1.95em}h2{font-size:1.25em;border-bottom:2px solid rgba(0,0,0,.87)}h1 a{color:rgba(0,0,0,.87)}p{margin:.67em 0 1em}a{color:#0076df;text-decoration:none;word-break:break-word}a:hover{color:rgba(0,0,0,.87)}ul,ol{list-style-position:outside;margin-left:1em}img{display:block;margin-left:auto;margin-right:auto;max-width:100%}pre,code{font-family:roboto mono,SFMono-Regular,Consolas,liberation mono,Menlo,monospace;font-weight:400;font-size:18px;color:rgba(0,0,0,.6);background:#eee}code{padding:.2rem .4rem;font-size:14px;border-radius:2px}pre{overflow-y:auto;line-height:20px;border-radius:2px;padding:1em}pre code{font-size:14px;padding:0;color:rgba(0,0,0,.87)}footer{font-size:12px}@media(prefers-color-scheme:dark){.logo{color:#444;background-color:#e4e4e4}.logo:hover{color:#444}body,h1 a,h2 a{background-color:#444;color:#e4e4e4}pre,pre code{background:#333;color:#e4e4e4}h2{border-bottom:1px solid #e4e4e4}code{color:#aaa;background:#333}a{color:#e39777}a:hover{color:#e4e4e4}img{filter:grayscale(30%)}}.hl{display:block;width:100%;background-color:#ffc}.ow,.gh,.gp,.gs,.gu,.nt,.nn,.ne,.ni,.nc,.kr,.kn,.kd,.kc,.k{font-weight:700}.c,.ch,.cm,.c1,.cs,.ge{color:#777}`
style := H("style", Attr{
"type": "text/css",
}, css)
head := H("head", H("title", title), meta, "\n", style)
inputs := []func() string{
H("input", Attr{
"type": "text",
"size": "40",
"name": "url",
"value": "",
}),
H("input", Attr{
"type": "submit",
"value": "Go!",
}),
}
form := H("form", Attr{"method": "GET"}, "URL: ", inputs)
u := r.URL
link := u.Query().Get("url")
body := H(
"body",
form,
)
if link != "" {
content, err := clean.URI(link, true)
if err != nil {
body = H(
"body",
form,
err.Error(),
)
} else {
body = H(
"body",
form,
func() string { return content },
)
}
}
html := H("html", head, "\n", body)
w.Write([]byte(html()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment