From 979ef108f4c0a6b343e4ec6e4dbab874013b169e Mon Sep 17 00:00:00 2001 From: teizz Date: Thu, 30 Sep 2021 12:26:15 +0200 Subject: [PATCH] reorder to try to use go modules --- src/broadcast.go => broadcast.go | 2 +- build.sh | 9 ++++++ cmd/go.mod | 7 +++++ cmd/main.go | 48 ++++++++++++++++++++++++++++++++ Dockerfile => docker/Dockerfile | 0 src/get.go => get.go | 2 +- go.mod | 3 ++ src/main.go => pathway.go | 46 +++++++++++------------------- src/post.go => post.go | 2 +- 9 files changed, 86 insertions(+), 33 deletions(-) rename src/broadcast.go => broadcast.go (99%) create mode 100755 build.sh create mode 100644 cmd/go.mod create mode 100644 cmd/main.go rename Dockerfile => docker/Dockerfile (100%) rename src/get.go => get.go (98%) create mode 100644 go.mod rename src/main.go => pathway.go (72%) rename src/post.go => post.go (99%) diff --git a/src/broadcast.go b/broadcast.go similarity index 99% rename from src/broadcast.go rename to broadcast.go index 5cc6d9a..1dcecc4 100644 --- a/src/broadcast.go +++ b/broadcast.go @@ -1,4 +1,4 @@ -package main +package pathway import ( "io" diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..edc5c31 --- /dev/null +++ b/build.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +export CGO_ENABLED=0 +export GO111MODULE=off + +echo go build -trimpath \ + -ldflags="all=-s -w -buildid= -X main.version=$(cat VERSION) -X main.buildtime=$(date +%FT%T%z)" \ + -o pathway \ + src/*.go diff --git a/cmd/go.mod b/cmd/go.mod new file mode 100644 index 0000000..f5a5b5a --- /dev/null +++ b/cmd/go.mod @@ -0,0 +1,7 @@ +module main + +go 1.16 + +require ( + git.nxdomain.nl/mattijs/pathway@latest +) diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..548fd19 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "log" + "net/http" + + "git.nxdomain.nl/mattijs/pathway" +) + +var ( + // variables to set during build-time + debugging = "" + version = "0.0-undefined" + buildtime = "0000-00-00T00:00:00+0000" +) + +func okHandler(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte("ok")) +} + +func emptyHandler(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) +} + +func main() { + info("pathway version:%s buildtime:%s", version, buildtime) + + http.HandleFunc("/health", okHandler) + http.HandleFunc("/favicon.ico", emptyHandler) + http.HandleFunc("/robots.txt", emptyHandler) + http.HandleFunc("/", pathway.pathHandler) + + err := http.ListenAndServe(":8080", nil) + if err != nil { + info("%s", err.Error()) + } +} + +func info(msg string, args ...interface{}) { + log.Printf("INFO | "+msg, args...) +} + +func debug(msg string, args ...interface{}) { + if len(debugging) > 0 { + log.Printf("DEBUG | "+msg, args...) + } +} diff --git a/Dockerfile b/docker/Dockerfile similarity index 100% rename from Dockerfile rename to docker/Dockerfile diff --git a/src/get.go b/get.go similarity index 98% rename from src/get.go rename to get.go index 48bfc53..2f2dea4 100644 --- a/src/get.go +++ b/get.go @@ -1,4 +1,4 @@ -package main +package pathway import ( "io" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8656eea --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.nxdomain.nl/mattijs/pathway + +go 1.16 diff --git a/src/main.go b/pathway.go similarity index 72% rename from src/main.go rename to pathway.go index 0d1b1e9..a9ed2d0 100644 --- a/src/main.go +++ b/pathway.go @@ -1,4 +1,4 @@ -package main +package pathway import ( "io" @@ -6,18 +6,19 @@ import ( "net/http" "strings" "sync" + // "nhooyr.io/websocket" ) var ( - // variables to set during build-time - debugging = "" - version = "0.0-undefined" - buildtime = "0000-00-00T00:00:00+0000" - // actual business end of the device - paths = &sync.Map{} + paths *sync.Map + debugging string ) +func init() { + paths = &sync.Map{} +} + // Transfer holds a single tranferable connection to be read type Transfer struct { reader io.ReadCloser @@ -57,7 +58,15 @@ func pathHandler(w http.ResponseWriter, r *http.Request) { if h := r.Header.Get("Connection"); len(h) > 0 && strings.ToLower(h) == "upgrade" { if h = r.Header.Get("Upgrade"); len(h) > 0 && strings.ToLower(h) == "websocket" { + // c, err := websocket.Accept(w, r, nil) + // if err != nil { + // info("Websocket not accepted: %+s", err.Error()) + // return + // } + // + // defer c.Close(websocket.StatusInternalError, "the sky is falling") info("Websocket connections not supported yet. Headers: %+v", r.Header) + // return } } @@ -77,29 +86,6 @@ func pathHandler(w http.ResponseWriter, r *http.Request) { } } -func okHandler(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - w.Write([]byte("ok")) -} - -func emptyHandler(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) -} - -func main() { - info("pathway version:%s buildtime:%s", version, buildtime) - - http.HandleFunc("/health", okHandler) - http.HandleFunc("/favicon.ico", emptyHandler) - http.HandleFunc("/robots.txt", emptyHandler) - http.HandleFunc("/", pathHandler) - - err := http.ListenAndServe(":8080", nil) - if err != nil { - info("%s", err.Error()) - } -} - func info(msg string, args ...interface{}) { log.Printf("INFO | "+msg, args...) } diff --git a/src/post.go b/post.go similarity index 99% rename from src/post.go rename to post.go index caed6b2..9985fd5 100644 --- a/src/post.go +++ b/post.go @@ -1,4 +1,4 @@ -package main +package pathway import ( "io"