Compare commits

...

6 Commits

Author SHA1 Message Date
bel 46de2a126c can pass ip 2025-10-19 21:24:24 -06:00
bel 981c850edf add access logger option 2023-08-17 17:08:10 -06:00
bel 1a4be5d384 :wqastub 2023-08-17 16:40:55 -06:00
bel a1b6ca009f k 2023-05-07 19:41:09 -06:00
bel e3908bae2e k 2023-05-07 19:36:45 -06:00
bel 6a92a82363 up go mod gzip 2023-05-07 19:18:36 -06:00
3 changed files with 42 additions and 9 deletions

9
go.mod
View File

@ -1,9 +1,14 @@
module gogs.inhome.blapointe.com/local/simpleserve
go 1.16
go 1.18
require (
gogs.inhome.blapointe.com/local/args v0.0.0-20230410154220-44370f257b34
gogs.inhome.blapointe.com/local/gziphttp v0.0.0-20230428215632-f3fe5ebcc23f
gogs.inhome.blapointe.com/local/gziphttp v0.0.0-20230508014052-4ccd700640fc
gogs.inhome.blapointe.com/local/notes-server v0.0.0-20230410171406-a4d39d38dc55
)
require (
github.com/gomarkdown/markdown v0.0.0-20220607163217-45f7c050e2d1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

5
go.sum
View File

@ -146,10 +146,9 @@ github.com/yunify/qingstor-sdk-go v2.2.15+incompatible/go.mod h1:w6wqLDQ5bBTzxGJ
go.mongodb.org/mongo-driver v1.7.2/go.mod h1:Q4oFMbo1+MSNqICAdYMlC/zSTrwCogR4R8NzkI+yfU8=
gogs.inhome.blapointe.com/local/args v0.0.0-20230410154220-44370f257b34 h1:0tuX5dfOksiOQD1vbJjVNVTVxTTIng7UrUdSLF5T+Ao=
gogs.inhome.blapointe.com/local/args v0.0.0-20230410154220-44370f257b34/go.mod h1:YG9n3Clg7683ohkVnJK2hdX8bBS9EojIsd1qPZumX0Y=
gogs.inhome.blapointe.com/local/gziphttp v0.0.0-20230410171240-32da87640b26 h1:zo9E05aBiyvtuuQiT3XjzBnpbm8+bVbzHe7soBQS0Tk=
gogs.inhome.blapointe.com/local/gziphttp v0.0.0-20230410171240-32da87640b26/go.mod h1:Sdj/NB9h3xrzPDqViQAHoDhA5gmpHkrWRXUauvLSA74=
gogs.inhome.blapointe.com/local/gziphttp v0.0.0-20230428215632-f3fe5ebcc23f h1:b3JijyS9wOa4GJSkEfJtsAVoQBhVZMYnYT1ONtOYvWs=
gogs.inhome.blapointe.com/local/gziphttp v0.0.0-20230428215632-f3fe5ebcc23f/go.mod h1:Sdj/NB9h3xrzPDqViQAHoDhA5gmpHkrWRXUauvLSA74=
gogs.inhome.blapointe.com/local/gziphttp v0.0.0-20230508014052-4ccd700640fc h1:ayxolpOV9uIm8rGdmtz03d5JlnbSmFF8daKjmwrZZ1o=
gogs.inhome.blapointe.com/local/gziphttp v0.0.0-20230508014052-4ccd700640fc/go.mod h1:Sdj/NB9h3xrzPDqViQAHoDhA5gmpHkrWRXUauvLSA74=
gogs.inhome.blapointe.com/local/logb v0.0.0-20230410154319-880efa39d871/go.mod h1:E0pLNvMLzY0Kth1W078y+06z1AUyVMWnChMpRFf4w2Q=
gogs.inhome.blapointe.com/local/notes-server v0.0.0-20230410171406-a4d39d38dc55 h1:WRN6wfRNgKGJJ0MyEnY6DzAQQbzJDTeMdQiuoO/Ib3g=
gogs.inhome.blapointe.com/local/notes-server v0.0.0-20230410171406-a4d39d38dc55/go.mod h1:TcdL6JdQXp2624U28X/ZKtv+UZuHEWZBI9qRrVFNFpc=

37
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
@ -13,6 +14,7 @@ import (
"path"
"regexp"
"strings"
"time"
"gogs.inhome.blapointe.com/local/args"
"gogs.inhome.blapointe.com/local/gziphttp"
@ -31,8 +33,10 @@ var (
func main() {
fs = args.NewArgSet()
fs.Append(args.STRING, "p", "port to serve", "8100")
fs.Append(args.STRING, "ip", "ip to serve", "")
fs.Append(args.STRING, "u", "user:pass for basic auth", "")
fs.Append(args.BOOL, "md", "whether to render markdown as html", true)
fs.Append(args.BOOL, "log", "emit access logs", false)
fs.Append(args.BOOL, "ro", "read only mode", false)
fs.Append(args.BOOL, "https", "https only", false)
fs.Append(args.STRING, "md-css", "css to load for md", "/dev/null")
@ -49,6 +53,7 @@ func main() {
https := fs.Get("https").GetBool()
mdCss := fs.Get("md-css").GetString()
mdClass := fs.Get("md-class").GetString()
accessLogging := fs.GetBool("log")
if mdCss != "" {
b, err := ioutil.ReadFile(mdCss)
if err != nil {
@ -93,17 +98,41 @@ func main() {
b,
)
}
ip := fs.Get("ip").GetString()
p := strings.TrimPrefix(fs.Get("p").GetString(), ":")
http.Handle("/", http.HandlerFunc(handler(userPass, https, ro, d, md, mdCss, mdClass)))
http.Handle("/", http.HandlerFunc(handler(userPass, https, ro, d, md, mdCss, mdClass, accessLogging)))
log.Printf("Serving %s on HTTP port: %s\n", d, p)
log.Fatal(http.ListenAndServe(":"+p, nil))
log.Fatal(http.ListenAndServe(ip+":"+p, nil))
}
func handler(userPass string, https, ro bool, d string, md bool, mdCss, mdClass string) http.HandlerFunc {
return httpsOnly(https, gzip(basicAuth(userPass, endpoints(ro, withDel(ro, withMD(d, md, mdCss, mdClass, fserve(d)))))))
func handler(userPass string, https, ro bool, d string, md bool, mdCss, mdClass string, accessLogging bool) http.HandlerFunc {
return withAccessLogging(accessLogging, httpsOnly(https, gzip(basicAuth(userPass, endpoints(ro, withDel(ro, withMD(d, md, mdCss, mdClass, fserve(d))))))))
}
func withAccessLogging(enabled bool, h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Host == "" {
r.URL.Host = r.Host
}
b, _ := json.Marshal(map[string]any{
"_ts": time.Now(),
"client": map[string]any{
"ip": r.RemoteAddr,
"forwarded-for": r.Header.Get("X-Forwarded-For"),
"user-agent": r.UserAgent(),
},
"request": map[string]any{
"method": r.Method,
"url": r.URL.String(),
"headers": r.Header,
},
})
fmt.Printf("%s\n", b)
h(w, r)
}
}
func writeMeta(w http.ResponseWriter) {