feat: added logging.

This commit is contained in:
Andrey Parhomenko 2024-06-09 18:26:31 +05:00
parent 5db9429bd8
commit 70dfaf8593
16 changed files with 72 additions and 193 deletions

View file

@ -1,10 +1,10 @@
package main
import (
"surdeus.su/util/tht/server"
"surdeus.su/util/tht"
"os"
)
func main() {
server.Tool.Run(os.Args[1:])
tht.Tool.Run(os.Args[1:])
}

2
go.mod
View file

@ -7,5 +7,5 @@ require (
github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2
surdeus.su/core/cli v0.1.2
surdeus.su/core/xgo v0.5.0
surdeus.su/util/tpp v0.2.0
surdeus.su/util/tpp v0.3.2
)

8
go.sum
View file

@ -6,5 +6,9 @@ surdeus.su/core/cli v0.1.2 h1:qPzjawqPyZsO4Z5SaA1u141recVE65yioA83Qs7Jecs=
surdeus.su/core/cli v0.1.2/go.mod h1:r9JtQz3aEJzpYzMaNUNQHJoYkoWKNPi047qhd5uGlmA=
surdeus.su/core/xgo v0.5.0 h1:/Rk3scfFkoSb0qjHRlkUNOp9sr/fd7wAvCiT4fBRo+U=
surdeus.su/core/xgo v0.5.0/go.mod h1:6C/AHbjfvAMvt3TOzLB4eIZ40eU3ahJXtdY+kr4yXoc=
surdeus.su/util/tpp v0.2.0 h1:9N78JPEUovDWdKnrdfrf05Jkz5xMA5hcBEWCIh4RWW0=
surdeus.su/util/tpp v0.2.0/go.mod h1:3bcSjGNd3vAevr6gPMI1/qJofxL7x53v0yB+Dr18CRM=
surdeus.su/util/tpp v0.3.0 h1:aIVFcpz+NtE09cuHZktNvYIyrQugoAZws4jctKR250o=
surdeus.su/util/tpp v0.3.0/go.mod h1:3bcSjGNd3vAevr6gPMI1/qJofxL7x53v0yB+Dr18CRM=
surdeus.su/util/tpp v0.3.1 h1:DwTqfin13SJmg7r/F9o1pgbPwCcE86oxttjqaK2+80s=
surdeus.su/util/tpp v0.3.1/go.mod h1:rXOVXwvdc7FxRGK/Smy03AXLQiet4N+2imFesic9Vzw=
surdeus.su/util/tpp v0.3.2 h1:ebcnEcY+4tgB4a6trs4GBd2CJjrZJaPKh3i5RKQf8/U=
surdeus.su/util/tpp v0.3.2/go.mod h1:rXOVXwvdc7FxRGK/Smy03AXLQiet4N+2imFesic9Vzw=

View file

@ -1,8 +1,8 @@
package server
package tht
import (
//"github.com/d5/tengo/v2"
"surdeus.su/util/tpp/mdx"
"surdeus.su/util/tht/mdx"
//"surdeus.su/core/xgo/xmodules/htmlx"
"surdeus.su/core/xgo/xmodules/httpx"
"surdeus.su/core/xgo/xmodules"

22
log.go Normal file
View file

@ -0,0 +1,22 @@
package tht
import "log"
import "net/http"
type LoggerMiddle struct {
Logger *log.Logger
After http.Handler
}
func (l *LoggerMiddle) ServeHTTP(
w http.ResponseWriter, r *http.Request,
) {
if l.Logger != nil {
l.Logger.Printf(
"%s %s %q\n",
r.RemoteAddr, r.Method, r.URL,
)
}
l.After.ServeHTTP(w, r)
}

4
run-tht.sh Executable file
View file

@ -0,0 +1,4 @@
#!/bin/sh
#
go build -o ./exe/ ./cmd/tht/ && \
./exe/tht

View file

@ -1,5 +0,0 @@
<main>shit</main>
{{
pp.print("cock")
}}
<main>later</main>

View file

@ -1,20 +0,0 @@
{{#
context.is_compo = true
}}
<form hx-put="/contact/1" hx-target="this" hx-swap="outerHTML">
<div>
<label>First Name</label>
<input type="text" name="firstName" value="Joe">
</div>
<div class="form-group">
<label>Last Name</label>
<input type="text" name="lastName" value="Blow">
</div>
<div class="form-group">
<label>Email Address</label>
<input type="email" name="email" value="joe@blow.com">
</div>
<button class="btn">Submit</button>
<button class="btn" hx-get="/contact/view.htm">Cancel</button>
</form>

View file

@ -1,13 +0,0 @@
{{#
//context.is_compo = true
}}
<div hx-target="this" hx-swap="outerHTML">
<div><label>First Name</label>: Joe</div>
<div><label>Last Name</label>: Blow</div>
<div><label>Email</label>: joe@blow.com</div>
<button hx-get="/contact/edit.htm" class="btn btn-primary">
Click To Edit
</button>
</div>

View file

@ -1,11 +0,0 @@
html := import("html").new_render()
pp.print(html.html().body(
html.head().body(
html.title().body(
"some title"
)
),
html.body().body(
)
))

View file

@ -5,24 +5,21 @@
html := import("html").new_render()
}}
<h1>This is the example page for the THT</h1>
{{
/*if !context.global.int {
context.global.int = 0
} else {
context.global.int++
}*/
pp.print(
html.h1().body("This is the example page for THT")
)
pp.print("some more shit")
if !context.global.val {
context.global.val = 1
} else {
context.global.val += 1
}
fmt.println("global: ", context.global)
req := context.http.request
q := req.url.query
// List checking.
list := []
rng := int(q.range[0])
fmt.println("range:", q.range)
if rng {
@ -36,43 +33,4 @@
pp.print("<div id=\"name\">", q.name[0], "</div>")
}
}}
<div name="counter">
{{ pp.print(context.global.val) }}
</div>
<div>
Hello, Cock!
</div>
<div check>
{{
pp.print(req.url.path)
}}
</div>
<ul>{{
for v in list {
pp.print("<li>", v, "</li>")
}
}}</ul>
{{
vals := ["die", "with", "them", "as", "you", 1, "could", "do"]
pp.print(html.div({
id: "the-uniq-shit"
}).body(
html.ul(
).body(
func(){
ret := []
for i:=0 ; i<len(vals) ; i++{
ret += [html.li({
class: "someclass"
}).body(i, ": ", vals[i])]
}
return ret
}()...
)
))
}}
<script src="/script.js" ></script>
<div></div>

View file

@ -1,37 +0,0 @@
{{#
context.title = "The example.site main page"
fmt := import("fmt")
html := import("html").new_render()
}}
{{
pp.print(
html.h1().body("This is the example page for THT")
)
if !context.global.val {
context.global.val = 1
} else {
context.global.val += 1
}
}}
{{
req := context.http.request
q := req.url.query
// List checking.
rng := int(q.range[0])
fmt.println("range:", q.range)
if rng {
pp.print(`<ul name="range">`)
for i:=0 ; i < rng ; i++ {
pp.printf("<li>%d</li>", i)
}
pp.print("</ul>")
}
if q.name {
pp.print("<div id=\"name\">", q.name[0], "</div>")
}
}}

View file

@ -1,35 +0,0 @@
{{#
context.title = "Let's celebrate and suck, SOME DICK"
}}
{{markdown(`
# Hello, World!
This is the markdown example shit.
func main() {
print("Fuck you")
}
## Yet another level of header
And even more text
> Cheap is talk, show me the code.
(c) Linus Torvalds
`)}}
<div id="uniq">
Even <strong>more</strong> HTML
</div>
{{markdown(`
### Third level header
Some shit
#### `, 135, `
`)}}

View file

@ -1,8 +1,12 @@
{
"error": null,
"data": {
"name": "Andrey",
"surname": "Parhomenko",
"age": 22
{{
json := import("json")
ret := {
"error": undefined,
data: {
name: "Andrew",
surname: "Parhomenko",
age: 22
}
}
pp.print(string(json.encode(ret)))
}}

View file

@ -1,3 +0,0 @@
{
"name":"Andrew"
}

View file

@ -1,14 +1,12 @@
package server
package tht
import (
"github.com/d5/tengo/v2"
//"github.com/d5/tengo/v2/stdlib"
"surdeus.su/util/tpp/mdx"
"surdeus.su/util/tht/mdx"
"surdeus.su/core/cli/mtool"
"net/http"
"log"
//"context"
)
// Context key type for internal usage.
@ -25,6 +23,7 @@ const (
var Tool = mtool.T("tht").Func(func(flags *mtool.Flags) {
var (
addr, ext, src, mod, index string
noLog bool
)
flags.StringVar(&addr, "addr", ":3000", "address to serve at")
@ -32,18 +31,30 @@ var Tool = mtool.T("tht").Func(func(flags *mtool.Flags) {
flags.StringVar(&src, "src", "./src", "directory with source files")
flags.StringVar(&ext, "ext", ".tpp", "extension for TPP files")
flags.StringVar(&index, "index", "index.htm", "index file name")
flags.BoolVar(
&noLog,
"no-log",
false,
"disable logging",
)
flags.Parse()
srv := &http.Server{
Addr: addr,
Handler: NewHandler(
handler := &LoggerMiddle{
After: NewHandler(
DefaultPP(mod),
src, ext, index,
map[string] tengo.Object{},
).SetMD(mdx.MakeDefaultMarkdown()),
}
if !noLog {
handler.Logger = log.Default()
}
srv := &http.Server{
Addr: addr,
Handler: handler,
}
log.Printf("Listening on %q\n", addr)
err := srv.ListenAndServe()