From 70dfaf8593fb5977f1ab810c8ad717bdf3a57dc8 Mon Sep 17 00:00:00 2001 From: surdeus Date: Sun, 9 Jun 2024 18:26:31 +0500 Subject: [PATCH] feat: added logging. --- cmd/tht/main.go | 4 +-- go.mod | 2 +- go.sum | 8 +++-- server/handler.go => handler.go | 4 +-- log.go | 22 ++++++++++++++ run-tht.sh | 4 +++ src/check.htm.tpp | 5 ---- src/contact/edit.htm.tpp | 20 ------------- src/contact/view.htm.tpp | 13 --------- src/incorrect.htm.tpp | 11 ------- src/index.htm.tpp | 52 ++++----------------------------- src/main.htm.tpp | 37 ----------------------- src/page.htm.tpp | 35 ---------------------- src/req.json.tpp | 18 +++++++----- src/request.json | 3 -- server/tool.go => tool.go | 27 ++++++++++++----- 16 files changed, 72 insertions(+), 193 deletions(-) rename server/handler.go => handler.go (98%) create mode 100644 log.go create mode 100755 run-tht.sh delete mode 100644 src/check.htm.tpp delete mode 100644 src/contact/edit.htm.tpp delete mode 100644 src/contact/view.htm.tpp delete mode 100644 src/incorrect.htm.tpp delete mode 100644 src/main.htm.tpp delete mode 100644 src/page.htm.tpp delete mode 100644 src/request.json rename server/tool.go => tool.go (81%) diff --git a/cmd/tht/main.go b/cmd/tht/main.go index a8c70b7..5c2a29f 100644 --- a/cmd/tht/main.go +++ b/cmd/tht/main.go @@ -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:]) } diff --git a/go.mod b/go.mod index a99fda1..3daa747 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 2745b13..54beab5 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/server/handler.go b/handler.go similarity index 98% rename from server/handler.go rename to handler.go index daa2408..8005d2b 100644 --- a/server/handler.go +++ b/handler.go @@ -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" diff --git a/log.go b/log.go new file mode 100644 index 0000000..f347efa --- /dev/null +++ b/log.go @@ -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) +} + diff --git a/run-tht.sh b/run-tht.sh new file mode 100755 index 0000000..646c7c1 --- /dev/null +++ b/run-tht.sh @@ -0,0 +1,4 @@ +#!/bin/sh +# +go build -o ./exe/ ./cmd/tht/ && \ + ./exe/tht diff --git a/src/check.htm.tpp b/src/check.htm.tpp deleted file mode 100644 index 6674cd9..0000000 --- a/src/check.htm.tpp +++ /dev/null @@ -1,5 +0,0 @@ -
shit
-{{ - pp.print("cock") -}} -
later
diff --git a/src/contact/edit.htm.tpp b/src/contact/edit.htm.tpp deleted file mode 100644 index b762aac..0000000 --- a/src/contact/edit.htm.tpp +++ /dev/null @@ -1,20 +0,0 @@ -{{# - context.is_compo = true -}} - -
-
- - -
-
- - -
-
- - -
- - -
diff --git a/src/contact/view.htm.tpp b/src/contact/view.htm.tpp deleted file mode 100644 index 04fff93..0000000 --- a/src/contact/view.htm.tpp +++ /dev/null @@ -1,13 +0,0 @@ -{{# - //context.is_compo = true -}} - -
-
: Joe
-
: Blow
-
: joe@blow.com
- -
- diff --git a/src/incorrect.htm.tpp b/src/incorrect.htm.tpp deleted file mode 100644 index 0f67522..0000000 --- a/src/incorrect.htm.tpp +++ /dev/null @@ -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( - ) -)) diff --git a/src/index.htm.tpp b/src/index.htm.tpp index 2e4c875..666751c 100644 --- a/src/index.htm.tpp +++ b/src/index.htm.tpp @@ -5,24 +5,21 @@ html := import("html").new_render() }} -

This is the example page for the THT

{{ - /*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("
", q.name[0], "
") } }} -
- {{ pp.print(context.global.val) }} -
-
- Hello, Cock! -
-
- {{ - pp.print(req.url.path) - }} -
- -{{ - 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 - -
diff --git a/src/main.htm.tpp b/src/main.htm.tpp deleted file mode 100644 index 53a9907..0000000 --- a/src/main.htm.tpp +++ /dev/null @@ -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(`") - } - if q.name { - pp.print("
", q.name[0], "
") - } -}} diff --git a/src/page.htm.tpp b/src/page.htm.tpp deleted file mode 100644 index f9fb15f..0000000 --- a/src/page.htm.tpp +++ /dev/null @@ -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 - -`)}} - -
- Even more HTML -
- -{{markdown(` -### Third level header - -Some shit - -#### `, 135, ` -`)}} diff --git a/src/req.json.tpp b/src/req.json.tpp index 16bb6de..7d8470b 100644 --- a/src/req.json.tpp +++ b/src/req.json.tpp @@ -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))) +}} diff --git a/src/request.json b/src/request.json deleted file mode 100644 index 3b0cd87..0000000 --- a/src/request.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "name":"Andrew" -} diff --git a/server/tool.go b/tool.go similarity index 81% rename from server/tool.go rename to tool.go index 24cebfc..3203c03 100644 --- a/server/tool.go +++ b/tool.go @@ -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()