Compare commits

..

No commits in common. "master" and "v0.15.0" have entirely different histories.

3 changed files with 9 additions and 42 deletions

9
go.mod
View File

@ -1,14 +1,9 @@
module gogs.inhome.blapointe.com/local/simpleserve
go 1.18
go 1.16
require (
gogs.inhome.blapointe.com/local/args v0.0.0-20230410154220-44370f257b34
gogs.inhome.blapointe.com/local/gziphttp v0.0.0-20230508014052-4ccd700640fc
gogs.inhome.blapointe.com/local/gziphttp v0.0.0-20230428215632-f3fe5ebcc23f
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,9 +146,10 @@ 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-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/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/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,7 +2,6 @@ package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
@ -14,7 +13,6 @@ import (
"path"
"regexp"
"strings"
"time"
"gogs.inhome.blapointe.com/local/args"
"gogs.inhome.blapointe.com/local/gziphttp"
@ -33,10 +31,8 @@ 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")
@ -53,7 +49,6 @@ 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 {
@ -98,41 +93,17 @@ 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, accessLogging)))
http.Handle("/", http.HandlerFunc(handler(userPass, https, ro, d, md, mdCss, mdClass)))
log.Printf("Serving %s on HTTP port: %s\n", d, p)
log.Fatal(http.ListenAndServe(ip+":"+p, nil))
log.Fatal(http.ListenAndServe(":"+p, nil))
}
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 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 writeMeta(w http.ResponseWriter) {