Ver Fonte

reorder to try to use go modules

teizz há 3 anos atrás
pai
commit
979ef108f4
9 ficheiros alterados com 86 adições e 33 exclusões
  1. 1 1
      src/broadcast.go
  2. 9 0
      build.sh
  3. 7 0
      cmd/go.mod
  4. 48 0
      cmd/main.go
  5. 0 0
      docker/Dockerfile
  6. 1 1
      src/get.go
  7. 3 0
      go.mod
  8. 16 30
      src/main.go
  9. 1 1
      src/post.go

+ 1 - 1
src/broadcast.go

@@ -1,4 +1,4 @@
-package main
+package pathway
 
 import (
 	"io"

+ 9 - 0
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

+ 7 - 0
cmd/go.mod

@@ -0,0 +1,7 @@
+module main
+
+go 1.16
+
+require (
+    git.nxdomain.nl/mattijs/pathway@latest
+)

+ 48 - 0
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...)
+	}
+}

Dockerfile → docker/Dockerfile


+ 1 - 1
src/get.go

@@ -1,4 +1,4 @@
-package main
+package pathway
 
 import (
 	"io"

+ 3 - 0
go.mod

@@ -0,0 +1,3 @@
+module git.nxdomain.nl/mattijs/pathway
+
+go 1.16

+ 16 - 30
src/main.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...)
 }

+ 1 - 1
src/post.go

@@ -1,4 +1,4 @@
-package main
+package pathway
 
 import (
 	"io"