From b9bedefc552c5546ec47c1a245c899751eac8e35 Mon Sep 17 00:00:00 2001 From: Mathias Gumz Date: Sun, 19 Nov 2023 12:14:39 +0100 Subject: [PATCH] Refactor: code layout Lets follow https://github.com/golang-standards/project-layout --- cmd/knut/main.go | 58 +++++++++++---------- {cmd => internal/pkg}/knut/compress_pool.go | 2 +- {cmd => internal/pkg}/knut/git.go | 11 ++-- {cmd => internal/pkg}/knut/gzip.go | 4 +- {cmd => internal/pkg}/knut/gzip_deflate.go | 4 +- {cmd => internal/pkg}/knut/handlers.go | 20 +++---- {cmd => internal/pkg}/knut/helper.go | 8 +-- {cmd => internal/pkg}/knut/knut_test.go | 6 +-- {cmd => internal/pkg}/knut/logging.go | 4 +- {cmd => internal/pkg}/knut/myip.go | 4 +- {cmd => internal/pkg}/knut/qr.go | 4 +- {cmd => internal/pkg}/knut/tar.go | 8 +-- {cmd => internal/pkg}/knut/tls.go | 30 ++++++----- {cmd => internal/pkg}/knut/upload.go | 4 +- {cmd => internal/pkg}/knut/zip.go | 8 +-- {cmd => internal/pkg}/knut/zipfs.go | 5 +- vendor/github.com/skip2/go-qrcode/go.mod | 3 -- vendor/modules.txt | 2 +- 18 files changed, 95 insertions(+), 90 deletions(-) rename {cmd => internal/pkg}/knut/compress_pool.go (99%) rename {cmd => internal/pkg}/knut/git.go (88%) rename {cmd => internal/pkg}/knut/gzip.go (92%) rename {cmd => internal/pkg}/knut/gzip_deflate.go (92%) rename {cmd => internal/pkg}/knut/handlers.go (83%) rename {cmd => internal/pkg}/knut/helper.go (89%) rename {cmd => internal/pkg}/knut/knut_test.go (94%) rename {cmd => internal/pkg}/knut/logging.go (93%) rename {cmd => internal/pkg}/knut/myip.go (93%) rename {cmd => internal/pkg}/knut/qr.go (86%) rename {cmd => internal/pkg}/knut/tar.go (91%) rename {cmd => internal/pkg}/knut/tls.go (82%) rename {cmd => internal/pkg}/knut/upload.go (97%) rename {cmd => internal/pkg}/knut/zip.go (89%) rename {cmd => internal/pkg}/knut/zipfs.go (97%) delete mode 100644 vendor/github.com/skip2/go-qrcode/go.mod diff --git a/cmd/knut/main.go b/cmd/knut/main.go index 00feb00..2224516 100644 --- a/cmd/knut/main.go +++ b/cmd/knut/main.go @@ -11,10 +11,12 @@ import ( "net/url" "os" "strings" + + "github.com/mgumz/knut/internal/pkg/knut" ) var ( - Version = "1.5.0" + Version = "dev-build" GitHash = "" BuildDate = "" ) @@ -114,11 +116,11 @@ func main() { var h = http.Handler(tree) if opts.addServerID != "" { - h = addServerIDHandler(h, opts.addServerID) + h = knut.AddServerIDHandler(h, opts.addServerID) } - h = noCacheHandler(h) + h = knut.NoCacheHandler(h) if opts.doCompress { - h = compressHandler(h) + h = knut.CompressHandler(h) } if opts.doAuth != "" { parts := strings.SplitN(opts.doAuth, ":", 2) @@ -126,10 +128,10 @@ func main() { fmt.Fprintf(os.Stderr, "error: missing separator for argument to -auth") os.Exit(1) } - h = basicAuthHandler(h, parts[0], parts[1]) + h = knut.BasicAuthHandler(h, parts[0], parts[1]) } if opts.doLog { - h = logRequestHandler(h, os.Stdout) + h = knut.LogRequestHandler(h, os.Stdout) } // @@ -138,12 +140,12 @@ func main() { var run func() error switch { case opts.tlsOnetime: - onetime := &onetimeTLS{} + onetime := &knut.OnetimeTLS{} if err := onetime.Create(opts.bindAddr); err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(1) } - run = func() error { return http.Serve(onetime.listener, h) } + run = func() error { return http.Serve(onetime.Listener, h) } case opts.tlsCert != "" && opts.tlsKey != "": run = func() error { return http.ListenAndServeTLS(opts.bindAddr, opts.tlsCert, opts.tlsKey, h) } default: @@ -175,7 +177,7 @@ func prepareTrees(muxer *http.ServeMux, mappings []string) (*http.ServeMux, int) for i := range mappings { - window, tree, err = getWindowAndTree(mappings[i]) + window, tree, err = knut.GetWindowAndTree(mappings[i]) if err != nil { fmt.Fprintf(os.Stderr, "warning: parsing %q (pos %d): %v\n", mappings[i], i+1, err) continue @@ -192,15 +194,15 @@ func prepareTrees(muxer *http.ServeMux, mappings []string) (*http.ServeMux, int) fmt.Fprintf(os.Stderr, "warning: existing %q is not a directory\n", tree) continue } - handler, verb = uploadHandler(tree), "catches" + handler, verb = knut.UploadHandler(tree), "catches" case strings.HasPrefix(window, "30x"): if window = window[3:]; window == "" { fmt.Fprintf(os.Stderr, "warning: post uri in pair %d is empty\n", i) continue } - handler, verb = redirectHandler(window, tree), "points at" + handler, verb = knut.RedirectHandler(window, tree), "points at" case tree[0] == STRING_HANDLER: - handler = serveStringHandler(tree[1:]) + handler = knut.ServeStringHandler(tree[1:]) default: if treeURL, err := url.Parse(tree); err == nil { query := treeURL.Query() @@ -208,9 +210,9 @@ func prepareTrees(muxer *http.ServeMux, mappings []string) (*http.ServeMux, int) case "http", "https": handler = httputil.NewSingleHostReverseProxy(treeURL) case "file": - handler = fileOrDirHandler(localFilename(treeURL), window) + handler = knut.FileOrDirHandler(knut.LocalFilename(treeURL), window) case "myip": - handler = myIPHandler() + handler = knut.MyIPHandler() case "qr": qrContent := treeURL.Path if len(qrContent) <= 1 { @@ -218,36 +220,36 @@ func prepareTrees(muxer *http.ServeMux, mappings []string) (*http.ServeMux, int) continue } qrContent = qrContent[1:] // cut away the leading / - handler = qrHandler(qrContent) - handler = setContentType(handler, "image/png") + handler = knut.QrHandler(qrContent) + handler = knut.SetContentType(handler, "image/png") case "git": - handler = gitHandler(localFilename(treeURL), window) + handler = knut.GitHandler(knut.LocalFilename(treeURL), window) case "cgit": - handler = cgitHandler(localFilename(treeURL), window) + handler = knut.CgitHandler(knut.LocalFilename(treeURL), window) case "tar": prefix := query.Get("prefix") - handler = tarHandler(localFilename(treeURL), prefix) - handler = setContentType(handler, "application/x-tar") + handler = knut.TarHandler(knut.LocalFilename(treeURL), prefix) + handler = knut.SetContentType(handler, "application/x-tar") case "tar+gz", "tar.gz", "tgz": prefix := query.Get("prefix") clevel := query.Get("level") - handler = tarHandler(localFilename(treeURL), prefix) - handler = gzHandler(handler, clevel) - handler = setContentType(handler, "application/x-gtar") + handler = knut.TarHandler(knut.LocalFilename(treeURL), prefix) + handler = knut.GzHandler(handler, clevel) + handler = knut.SetContentType(handler, "application/x-gtar") case "zip": prefix := query.Get("prefix") - store := hasQueryParam("store", query) - handler = zipHandler(localFilename(treeURL), prefix, store) - handler = setContentType(handler, "application/zip") + store := knut.HasQueryParam("store", query) + handler = knut.ZipHandler(knut.LocalFilename(treeURL), prefix, store) + handler = knut.SetContentType(handler, "application/zip") case "zipfs": prefix := query.Get("prefix") index := query.Get("index") - handler = zipFSHandler(localFilename(treeURL), prefix, index) + handler = knut.ZipFSHandler(knut.LocalFilename(treeURL), prefix, index) } } if handler == nil { - handler = fileOrDirHandler(tree, window) + handler = knut.FileOrDirHandler(tree, window) } } diff --git a/cmd/knut/compress_pool.go b/internal/pkg/knut/compress_pool.go similarity index 99% rename from cmd/knut/compress_pool.go rename to internal/pkg/knut/compress_pool.go index 425ee1d..3ba324b 100644 --- a/cmd/knut/compress_pool.go +++ b/internal/pkg/knut/compress_pool.go @@ -1,7 +1,7 @@ // Copyright 2016 Mathias Gumz. All rights reserved. Use of this source code // is governed by a BSD-style license that can be found in the LICENSE file. -package main +package knut import ( "compress/flate" diff --git a/cmd/knut/git.go b/internal/pkg/knut/git.go similarity index 88% rename from cmd/knut/git.go rename to internal/pkg/knut/git.go index 5fdeccf..2a489ef 100644 --- a/cmd/knut/git.go +++ b/internal/pkg/knut/git.go @@ -1,7 +1,7 @@ // Copyright 2015 Mathias Gumz. All rights reserved. Use of this source code // is governed by a BSD-style license that can be found in the LICENSE file. -package main +package knut import ( "net/http" @@ -15,7 +15,7 @@ import ( // offering a git repository (opposite to the dumb http-protocol also possible) // // see https://git-scm.com/docs/git-http-backend -func gitHandler(path, uri string) http.Handler { +func GitHandler(path, uri string) http.Handler { gitBinary, _ := exec.LookPath("git") gitHandler := new(cgi.Handler) @@ -37,11 +37,12 @@ func gitHandler(path, uri string) http.Handler { // scan-path directive to "." which makes cgit scan the directory given via // the uri. if the user places a "cgitrc" file into the .git folder of a // scanned git-repo, the "repo.*" options are applied there. eg, -// knut.git/.git/cgitrc -// desc=knut - throws trees out of windows +// +// knut.git/.git/cgitrc +// desc=knut - throws trees out of windows // // will make that directory be listed with that description. -func cgitHandler(path, uri string) http.Handler { +func CgitHandler(path, uri string) http.Handler { cgitBinary, _ := exec.LookPath("cgit") cgitHandler := new(cgi.Handler) cgitHandler.Dir = path diff --git a/cmd/knut/gzip.go b/internal/pkg/knut/gzip.go similarity index 92% rename from cmd/knut/gzip.go rename to internal/pkg/knut/gzip.go index 4a405b4..48160db 100644 --- a/cmd/knut/gzip.go +++ b/internal/pkg/knut/gzip.go @@ -1,7 +1,7 @@ // Copyright 2015 Mathias Gumz. All rights reserved. Use of this source code // is governed by a BSD-style license that can be found in the LICENSE file. -package main +package knut import ( "compress/gzip" @@ -11,7 +11,7 @@ import ( "strconv" ) -func gzHandler(next http.Handler, clevel string) http.Handler { +func GzHandler(next http.Handler, clevel string) http.Handler { level, err := atoiInRange(clevel, -1, gzip.BestCompression, gzip.DefaultCompression) if err != nil { diff --git a/cmd/knut/gzip_deflate.go b/internal/pkg/knut/gzip_deflate.go similarity index 92% rename from cmd/knut/gzip_deflate.go rename to internal/pkg/knut/gzip_deflate.go index aa58cc8..0738d02 100644 --- a/cmd/knut/gzip_deflate.go +++ b/internal/pkg/knut/gzip_deflate.go @@ -1,7 +1,7 @@ // Copyright 2015 Mathias Gumz. All rights reserved. Use of this source code // is governed by a BSD-style license that can be found in the LICENSE file. -package main +package knut import ( "compress/flate" @@ -12,7 +12,7 @@ import ( // compressHandler requests having Accept-Encoding 'deflate' or 'gzip' // taken from https://github.com/gorilla/handlers/blob/master/compress.go -func compressHandler(handler http.Handler) http.Handler { +func CompressHandler(handler http.Handler) http.Handler { gzPool := newGzPool(gzip.DefaultCompression) flatePool := newFlatePool(flate.DefaultCompression) diff --git a/cmd/knut/handlers.go b/internal/pkg/knut/handlers.go similarity index 83% rename from cmd/knut/handlers.go rename to internal/pkg/knut/handlers.go index 3c082d2..2fdf209 100644 --- a/cmd/knut/handlers.go +++ b/internal/pkg/knut/handlers.go @@ -1,7 +1,7 @@ // Copyright 2015 Mathias Gumz. All rights reserved. Use of this source code // is governed by a BSD-style license that can be found in the LICENSE file. -package main +package knut import ( "bytes" @@ -13,21 +13,21 @@ import ( ) // serveFileHandler serves the file given by 'name' -func serveFileHandler(name string) http.Handler { +func ServeFileHandler(name string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, name) }) } // serveStringHandler writes given 'str' to the response -func serveStringHandler(str string) http.Handler { +func ServeStringHandler(str string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write([]byte(str)) }) } -func redirectHandler(path, location string) http.Handler { +func RedirectHandler(path, location string) http.Handler { type uriHostPort struct { url.URL @@ -51,10 +51,10 @@ func redirectHandler(path, location string) http.Handler { }) } -func fileOrDirHandler(path, uri string) http.Handler { +func FileOrDirHandler(path, uri string) http.Handler { if fi, err := os.Stat(path); err == nil && !fi.IsDir() { - return serveFileHandler(path) + return ServeFileHandler(path) } handler := http.FileServer(http.Dir(path)) @@ -64,7 +64,7 @@ func fileOrDirHandler(path, uri string) http.Handler { // addServerIDHandler adds "Server: " to the response // header -func addServerIDHandler(next http.Handler, serverID string) http.Handler { +func AddServerIDHandler(next http.Handler, serverID string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Server", serverID) next.ServeHTTP(w, r) @@ -72,7 +72,7 @@ func addServerIDHandler(next http.Handler, serverID string) http.Handler { } // noCacheHandler adds a 'nocaching, please' hint to the response header -func noCacheHandler(next http.Handler) http.Handler { +func NoCacheHandler(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "private, max-age=0, no-cache") next.ServeHTTP(w, r) @@ -81,7 +81,7 @@ func noCacheHandler(next http.Handler) http.Handler { // basicAuthHandler checks the submited username and password against predefined // values. -func basicAuthHandler(next http.Handler, username, password string) http.Handler { +func BasicAuthHandler(next http.Handler, username, password string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("WWW-Authenticate", `Basic realm="knut"`) rUser, rPassword, ok := r.BasicAuth() @@ -95,7 +95,7 @@ func basicAuthHandler(next http.Handler, username, password string) http.Handler // setContentType sets the "Content-Type" to "contentType", if not already // set -func setContentType(next http.Handler, contentType string) http.Handler { +func SetContentType(next http.Handler, contentType string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if _, exists := w.Header()["Content-Type"]; !exists { w.Header().Set("Content-Type", contentType) diff --git a/cmd/knut/helper.go b/internal/pkg/knut/helper.go similarity index 89% rename from cmd/knut/helper.go rename to internal/pkg/knut/helper.go index 0f5a763..944f207 100644 --- a/cmd/knut/helper.go +++ b/internal/pkg/knut/helper.go @@ -1,7 +1,7 @@ // Copyright 2015 Mathias Gumz. All rights reserved. Use of this source code // is governed by a BSD-style license that can be found in the LICENSE file. -package main +package knut import ( "fmt" @@ -21,7 +21,7 @@ var ( // of *knut* the first part is called "window" (it is the url-endpoint, // essentially), the part after the first ':' is called "the tree", it's // the content that will be delivered. -func getWindowAndTree(arg string) (window, tree string, err error) { +func GetWindowAndTree(arg string) (window, tree string, err error) { // if the user just feeds in a list of (existing) filenames // we assume the user just wants to expose the given filenames @@ -44,7 +44,7 @@ func getWindowAndTree(arg string) (window, tree string, err error) { return window, tree, nil } -func localFilename(fileURL *url.URL) string { +func LocalFilename(fileURL *url.URL) string { return filepath.Join(fileURL.Host, fileURL.Path) } @@ -55,7 +55,7 @@ func writeStatus(w http.ResponseWriter, code int) { fmt.Fprintf(w, "%d: %s", code, http.StatusText(code)) } -func hasQueryParam(key string, vals url.Values) bool { +func HasQueryParam(key string, vals url.Values) bool { _, exists := vals[key] return exists } diff --git a/cmd/knut/knut_test.go b/internal/pkg/knut/knut_test.go similarity index 94% rename from cmd/knut/knut_test.go rename to internal/pkg/knut/knut_test.go index 26684c8..55b0801 100644 --- a/cmd/knut/knut_test.go +++ b/internal/pkg/knut/knut_test.go @@ -1,7 +1,7 @@ // Copyright 2015 Mathias Gumz. All rights reserved. Use of this source code // is governed by a BSD-style license that can be found in the LICENSE file. -package main +package knut import ( "net/url" @@ -48,7 +48,7 @@ func TestGetWindowAndTree(t *testing.T) { } for i, test := range tests { - win, tree, err := getWindowAndTree(test.in) + win, tree, err := GetWindowAndTree(test.in) t.Logf("case %d: %q => %q,%q,%v", i, test.in, win, tree, err) if win != test.window || tree != test.tree || err != test.err { t.Errorf("case %d: %q,%q,%v (%q) does not match %v", @@ -69,7 +69,7 @@ func TestLocalFilename(t *testing.T) { if err != nil { t.Errorf("case %d: parsing test.in %q: %v", i, test.in, err) } - out := filepath.ToSlash(localFilename(u)) + out := filepath.ToSlash(LocalFilename(u)) t.Logf("case %d: localFilename(%q): %q", i, u.String(), out) if out != test.out { t.Errorf("case %d: localFilename(%q): expected %q, got %q", diff --git a/cmd/knut/logging.go b/internal/pkg/knut/logging.go similarity index 93% rename from cmd/knut/logging.go rename to internal/pkg/knut/logging.go index cc83bc5..e7a341e 100644 --- a/cmd/knut/logging.go +++ b/internal/pkg/knut/logging.go @@ -1,7 +1,7 @@ // Copyright 2015 Mathias Gumz. All rights reserved. Use of this source code // is governed by a BSD-style license that can be found in the LICENSE file. -package main +package knut import ( "fmt" @@ -13,7 +13,7 @@ import ( // logRequestHandler returns a handler which logs all requests to 'writer'. it also captures // the status-code -func logRequestHandler(handler http.Handler, logWriter io.Writer) http.Handler { +func LogRequestHandler(handler http.Handler, logWriter io.Writer) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var sc = statusCodeCapture{w: w} diff --git a/cmd/knut/myip.go b/internal/pkg/knut/myip.go similarity index 93% rename from cmd/knut/myip.go rename to internal/pkg/knut/myip.go index bae1bc9..7b5dddf 100644 --- a/cmd/knut/myip.go +++ b/internal/pkg/knut/myip.go @@ -1,7 +1,7 @@ // Copyright 2017 Mathias Gumz. All rights reserved. Use of this source code // is governed by a BSD-style license that can be found in the LICENSE file. -package main +package knut import ( "html/template" @@ -10,7 +10,7 @@ import ( ) // myIPHandler yells back the remote IP to the browser -func myIPHandler() http.Handler { +func MyIPHandler() http.Handler { tmpl, _ := template.New("myip").Parse(` diff --git a/cmd/knut/qr.go b/internal/pkg/knut/qr.go similarity index 86% rename from cmd/knut/qr.go rename to internal/pkg/knut/qr.go index a43cdbe..f84898d 100644 --- a/cmd/knut/qr.go +++ b/internal/pkg/knut/qr.go @@ -1,7 +1,7 @@ // Copyright 2015 Mathias Gumz. All rights reserved. Use of this source code // is governed by a BSD-style license that can be found in the LICENSE file. -package main +package knut import ( "net/http" @@ -9,7 +9,7 @@ import ( qrcode "github.com/skip2/go-qrcode" ) -func qrHandler(content string) http.Handler { +func QrHandler(content string) http.Handler { qr, _ := qrcode.New(content, qrcode.Medium) qrbytes, _ := qr.PNG(256) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/cmd/knut/tar.go b/internal/pkg/knut/tar.go similarity index 91% rename from cmd/knut/tar.go rename to internal/pkg/knut/tar.go index 5869afb..9c4cd49 100644 --- a/cmd/knut/tar.go +++ b/internal/pkg/knut/tar.go @@ -1,7 +1,7 @@ // Copyright 2015 Mathias Gumz. All rights reserved. Use of this source code // is governed by a BSD-style license that can be found in the LICENSE file. -package main +package knut import ( "archive/tar" @@ -14,7 +14,7 @@ import ( // tarHandler creates a tar-archive from 'dir' on the fly and // writes it to 'w' -func tarHandler(dir, prefix string) http.Handler { +func TarHandler(dir, prefix string) http.Handler { if dir == "" { // "tar://." yields "" after url.Parse() dir = "." @@ -53,7 +53,9 @@ type tarEntry struct { err error } -func (entry *tarEntry) GetHeader(fi os.FileInfo) { entry.header, entry.err = tar.FileInfoHeader(fi, "") } +func (entry *tarEntry) GetHeader(fi os.FileInfo) { + entry.header, entry.err = tar.FileInfoHeader(fi, "") +} func (entry *tarEntry) SetName(name string) { if entry.err != nil { return diff --git a/cmd/knut/tls.go b/internal/pkg/knut/tls.go similarity index 82% rename from cmd/knut/tls.go rename to internal/pkg/knut/tls.go index 965cde7..d851895 100644 --- a/cmd/knut/tls.go +++ b/internal/pkg/knut/tls.go @@ -1,4 +1,7 @@ -package main +// Copyright 2015 Mathias Gumz. All rights reserved. Use of this source code +// is governed by a BSD-style license that can be found in the LICENSE file. + +package knut import ( "bytes" @@ -14,8 +17,9 @@ import ( "time" ) -type onetimeTLS struct { - listener net.Listener +type OnetimeTLS struct { + Listener net.Listener + privKey *ecdsa.PrivateKey sn *big.Int template *x509.Certificate @@ -29,7 +33,7 @@ type onetimeTLS struct { // (elliptic curve521), and create a tlsListener based upon that certificate. // it's only purpose is to have a tls-cert with a onetime, throw-away // certificate. fyi: http://safecurves.cr.yp.to/ -func (ot *onetimeTLS) Create(addr string) error { +func (ot *OnetimeTLS) Create(addr string) error { ot.createListener(addr) ot.createPrivateKey() @@ -40,17 +44,17 @@ func (ot *onetimeTLS) Create(addr string) error { ot.fillTLSConfig() if ot.err == nil { - ot.listener = tls.NewListener(ot.listener, &ot.tlsConfig) + ot.Listener = tls.NewListener(ot.Listener, &ot.tlsConfig) } return ot.err } -func (ot *onetimeTLS) createListener(addr string) { +func (ot *OnetimeTLS) createListener(addr string) { if ot.err == nil { - ot.listener, ot.err = net.Listen("tcp", addr) + ot.Listener, ot.err = net.Listen("tcp", addr) } } -func (ot *onetimeTLS) createPrivateKey() { +func (ot *OnetimeTLS) createPrivateKey() { if ot.err == nil { // * in general a good read: https://safecurves.cr.yp.to/ // * elliptic.P256/() returns a Curve which implements P-256 (see @@ -63,12 +67,12 @@ func (ot *onetimeTLS) createPrivateKey() { ot.privKey, ot.err = ecdsa.GenerateKey(curve, rand.Reader) } } -func (ot *onetimeTLS) createSerialNumber(n uint) { +func (ot *OnetimeTLS) createSerialNumber(n uint) { if ot.err == nil { ot.sn, ot.err = rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), n)) } } -func (ot *onetimeTLS) createTemplate() { +func (ot *OnetimeTLS) createTemplate() { if ot.err == nil { ot.template = &x509.Certificate{ SerialNumber: ot.sn, @@ -89,7 +93,7 @@ func (ot *onetimeTLS) createTemplate() { } } } -func (ot *onetimeTLS) createPKBytes() { +func (ot *OnetimeTLS) createPKBytes() { if ot.err == nil { ot.privKeyBytes, ot.err = x509.MarshalECPrivateKey(ot.privKey) } @@ -97,7 +101,7 @@ func (ot *onetimeTLS) createPKBytes() { ot.privKeyBytes = bytesToPem(ot.privKeyBytes, "EC PRIVATE KEY") } } -func (ot *onetimeTLS) createCertBytes() { +func (ot *OnetimeTLS) createCertBytes() { if ot.err == nil { ot.certBytes, ot.err = x509.CreateCertificate(rand.Reader, ot.template, ot.template, &ot.privKey.PublicKey, ot.privKey) } @@ -105,7 +109,7 @@ func (ot *onetimeTLS) createCertBytes() { ot.certBytes = bytesToPem(ot.certBytes, "CERTIFICATE") } } -func (ot *onetimeTLS) fillTLSConfig() { +func (ot *OnetimeTLS) fillTLSConfig() { if ot.err == nil { ot.tlsConfig.NextProtos = []string{"http/1.1"} ot.tlsConfig.MinVersion = tls.VersionTLS11 diff --git a/cmd/knut/upload.go b/internal/pkg/knut/upload.go similarity index 97% rename from cmd/knut/upload.go rename to internal/pkg/knut/upload.go index 89cebe2..89f4e07 100644 --- a/cmd/knut/upload.go +++ b/internal/pkg/knut/upload.go @@ -1,7 +1,7 @@ // Copyright 2015 Mathias Gumz. All rights reserved. Use of this source code // is governed by a BSD-style license that can be found in the LICENSE file. -package main +package knut import ( "fmt" @@ -19,7 +19,7 @@ import ( // uploadHandler handles uploads to a given 'dir'. for method "GET" an upload-form is // rendered, "POST" handles the actual upload -func uploadHandler(dir string) http.Handler { +func UploadHandler(dir string) http.Handler { const htmlDoc = ` diff --git a/cmd/knut/zip.go b/internal/pkg/knut/zip.go similarity index 89% rename from cmd/knut/zip.go rename to internal/pkg/knut/zip.go index b18df33..e68ce9e 100644 --- a/cmd/knut/zip.go +++ b/internal/pkg/knut/zip.go @@ -1,7 +1,7 @@ // Copyright 2016 Mathias Gumz. All rights reserved. Use of this source code // is governed by a BSD-style license that can be found in the LICENSE file. -package main +package knut import ( "archive/zip" @@ -13,14 +13,14 @@ import ( "path/filepath" ) -func zipHandler(dir, prefix string, store bool) http.Handler { +func ZipHandler(dir, prefix string, store bool) http.Handler { if dir == "" { // "zip://." yields "" after url.Parse() dir = "." } return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if err := zipDirectory(w, dir, prefix, store); err != nil { + if err := ZipDirectory(w, dir, prefix, store); err != nil { fmt.Fprintf(os.Stderr, "warning: creating zip of %q: %v\n", dir, err) } }) @@ -28,7 +28,7 @@ func zipHandler(dir, prefix string, store bool) http.Handler { // zipDirectory creates a .zip from "dir" and writes it to // "w". it also prepends "prefix" to each name. -func zipDirectory(w io.Writer, dir, prefix string, store bool) error { +func ZipDirectory(w io.Writer, dir, prefix string, store bool) error { zw := zip.NewWriter(w) defer zw.Close() diff --git a/cmd/knut/zipfs.go b/internal/pkg/knut/zipfs.go similarity index 97% rename from cmd/knut/zipfs.go rename to internal/pkg/knut/zipfs.go index 13701f1..52a8d7f 100644 --- a/cmd/knut/zipfs.go +++ b/internal/pkg/knut/zipfs.go @@ -1,7 +1,7 @@ // Copyright 2015 Mathias Gumz. All rights reserved. Use of this source code // is governed by a BSD-style license that can be found in the LICENSE file. -package main +package knut import ( "archive/zip" @@ -25,8 +25,7 @@ import ( // // if the requested path is a folder, use the "index" in that folder to // to render the folder entries. -// -func zipFSHandler(name, prefix, index string) http.Handler { +func ZipFSHandler(name, prefix, index string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // NOTE: yes, we open the zip for every request. this allows to diff --git a/vendor/github.com/skip2/go-qrcode/go.mod b/vendor/github.com/skip2/go-qrcode/go.mod deleted file mode 100644 index a915813..0000000 --- a/vendor/github.com/skip2/go-qrcode/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module github.com/skip2/go-qrcode - -go 1.13 diff --git a/vendor/modules.txt b/vendor/modules.txt index ebb64ef..1b35e04 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,5 +1,5 @@ # github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e -## explicit +## explicit; go 1.13 github.com/skip2/go-qrcode github.com/skip2/go-qrcode/bitset github.com/skip2/go-qrcode/reedsolomon