summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarin Ivanov <[email protected]>2025-06-12 21:17:44 +0300
committerMarin Ivanov <[email protected]>2025-06-12 21:17:44 +0300
commit109c9f0e7644acf26b3c08ac7ddbfd236f62ba6b (patch)
treed3b681ad1bee2879bc22963e45a9b8457088ec2f
Initial commit
-rw-r--r--.gitignore2
-rw-r--r--data/.gitkeep0
-rw-r--r--go.mod8
-rw-r--r--go.sum14
-rw-r--r--main.go87
5 files changed, 111 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..634b0a2
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/bin
+/data
diff --git a/data/.gitkeep b/data/.gitkeep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/data/.gitkeep
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..8786350
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,8 @@
+module go.metala.org/gloginki
+
+go 1.24.1
+
+require (
+ github.com/robert-nix/ansihtml v1.0.1
+ github.com/spf13/pflag v1.0.6
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..655ea97
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,14 @@
+github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/robert-nix/ansihtml v1.0.1 h1:VTiyQ6/+AxSJoSSLsMecnkh8i0ZqOEdiRl/odOc64fc=
+github.com/robert-nix/ansihtml v1.0.1/go.mod h1:CJwclxYaTPc2RfcxtanEACsYuTksh4yDXcNeHHKZINE=
+github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
+github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
+github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..a542ddb
--- /dev/null
+++ b/main.go
@@ -0,0 +1,87 @@
+package main
+
+import (
+ "crypto/rand"
+ "encoding/base64"
+ "fmt"
+ "io"
+ "log"
+ "net/http"
+ "os"
+ "path/filepath"
+
+ "github.com/robert-nix/ansihtml"
+ flag "github.com/spf13/pflag"
+)
+
+type server struct {
+ data string
+}
+
+func (s *server) Post(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path != "/" {
+ return
+ }
+ var random [16]byte
+ _, err := io.ReadFull(rand.Reader, random[:])
+ if err != nil {
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+ filename := base64.RawURLEncoding.EncodeToString(random[:])
+ path := filepath.Join(s.data, filename)
+ f, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o644)
+ if err != nil {
+ log.Print("OpenFile(): ", err)
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+ defer f.Close()
+ if _, err = io.Copy(f, r.Body); err != nil {
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+ w.WriteHeader(http.StatusCreated)
+ fmt.Fprintf(w, "/%s", filename)
+}
+
+func (s *server) Get(w http.ResponseWriter, r *http.Request) {
+ path := r.URL.Path
+ filename := filepath.Join(s.data, path)
+ http.ServeFile(w, r, filename)
+}
+
+func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ switch r.Method {
+ case "POST":
+ s.Post(w, r)
+ case "GET":
+ s.Get(w, r)
+ }
+}
+
+func main() {
+ var (
+ addr string
+ data string
+ )
+ flag.StringVarP(&addr, "addr", "a", "0.0.0.0:9000", "The address to bind to")
+ flag.StringVarP(&data, "data-path", "d", "./data", "The path to the data")
+ flag.Parse()
+
+ html := ansihtml.ConvertToHTML([]byte("\x1b[33mThis text is yellow.\x1b[m"))
+ // html: `<span style="color:olive;">This text is yellow.</span>`
+ println(string(html))
+
+ datadir, err := filepath.Abs(data)
+ if err != nil {
+ log.Fatal(err)
+ }
+ s := &server{
+ data: datadir,
+ }
+ log.Printf("Listening at '%s'...", addr)
+ if err := http.ListenAndServe(addr, s); err != nil {
+ log.Fatal(err)
+ }
+}