From f580a2651ce3e802c8f36b0159f8e3a188982d9f Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Tue, 20 Dec 2016 15:20:08 -0500 Subject: [PATCH] Setup docker and stuffs --- .gitignore | 2 ++ Dockerfile | 4 ++++ Makefile | 15 +++++++++++++-- cmd/pypihub/main.go | 7 +------ docker-compose.yml | 7 +++++++ server.go | 24 ++++++++++++++++++++---- 6 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b1a3c1e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +build diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..111edd8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM alpine:3.4 +RUN apk add --no-cache ca-certificates +COPY build/pypihub . +CMD ["./pypihub"] diff --git a/Makefile b/Makefile index e469143..0c07b7a 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,21 @@ -pypihub: ./*.go +pypihub: ./*.go ./cmd/pypihub/*.go go build -o pypihub ./cmd/pypihub/ +build/pypihub: ./*.go ./cmd/pypihub/*.go + mkdir -p build/ + CGO_ENABLED=0 GOOS=linux go build -a -tags netgo -ldflags '-w' -o build/pypihub ./cmd/pypihub/ + clean: rm -f ./pypihub + rm -rf ./build run: go run ./cmd/pypihub/main.go -.PHONY: clean +docker_build: build/pypihub + docker build -t pypihub . + +docker_up: docker_build + docker-compose up --build + +.PHONY: clean docker_build run docker)up diff --git a/cmd/pypihub/main.go b/cmd/pypihub/main.go index 32651bd..f0d4870 100644 --- a/cmd/pypihub/main.go +++ b/cmd/pypihub/main.go @@ -1,15 +1,10 @@ package main -import ( - "fmt" - - "github.com/brettlangdon/pypihub" -) +import "github.com/brettlangdon/pypihub" func main() { var config pypihub.Config config = pypihub.ParseConfig() - fmt.Printf("%#v\r\n", config) var server *pypihub.Server server = pypihub.NewServer(config) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..c2a4874 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,7 @@ +version: '2' +services: + pypihub: + build: . + env_file: '.env' + ports: + - "8287:8287" diff --git a/server.go b/server.go index 55276bd..3fd3786 100644 --- a/server.go +++ b/server.go @@ -46,7 +46,7 @@ func (s *Server) startTimer() { if s.timer != nil { s.timer.Stop() } - s.timer = time.AfterFunc(time.Duration(5*time.Minute), func() { + s.timer = time.AfterFunc(time.Duration(10*time.Minute), func() { go s.refetchAssets() }) } @@ -65,6 +65,22 @@ func (s *Server) listAssets(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "") } +func (s *Server) listRepoAssets(w http.ResponseWriter, r *http.Request) { + var parts = strings.SplitN(strings.Trim(r.URL.Path, "/"), "/", 2) + var owner = parts[0] + var repo = parts[1] + + w.Header().Add("Content-Type", "text/html") + fmt.Fprintf(w, "Links for %s", repo) + fmt.Fprintf(w, "

Links for %s

", repo) + for _, a := range s.assets { + if a.Owner == owner && a.Repo == repo { + fmt.Fprintf(w, "%s ", a.URL(), a.Name) + } + } + fmt.Fprintf(w, "") +} + func (s *Server) listAllAssets(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "text/html") fmt.Fprintf(w, "All asset links") @@ -140,13 +156,13 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { case 0: s.listAllAssets(w, r) case 1: - if parts[0] == "simple" { - s.listRepos(w, r) - } else if parts[0] == "favicon.ico" { + if parts[0] == "favicon.ico" { s.serveFavicon(w, r) } else { s.listAssets(w, r) } + case 2: + s.listRepoAssets(w, r) default: s.fetchAsset(w, r) }