From b9ed123ff205195eab3849ae0ebf9e106ebf9d5f Mon Sep 17 00:00:00 2001 From: Serge Bazanski Date: Wed, 12 May 2021 21:05:05 +0000 Subject: [PATCH] cebulacamp/landing: build and push backend Change-Id: I1336fb2fe52de7c42e5de0c4f1e05f42c32a9777 --- hswaw/cebulacamp/landing/BUILD.bazel | 34 ++++++++++++++ hswaw/cebulacamp/landing/backend/BUILD.bazel | 24 ++++++++++ hswaw/cebulacamp/landing/backend/main.go | 49 ++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 hswaw/cebulacamp/landing/BUILD.bazel create mode 100644 hswaw/cebulacamp/landing/backend/BUILD.bazel create mode 100644 hswaw/cebulacamp/landing/backend/main.go diff --git a/hswaw/cebulacamp/landing/BUILD.bazel b/hswaw/cebulacamp/landing/BUILD.bazel new file mode 100644 index 00000000..e2f09e04 --- /dev/null +++ b/hswaw/cebulacamp/landing/BUILD.bazel @@ -0,0 +1,34 @@ +load("@io_bazel_rules_docker//container:container.bzl", "container_image", "container_push") +load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//extras:embed_data.bzl", "go_embed_data") + +go_embed_data( + name = "static", + srcs = ["index.html", "style/main.css", "cebula2020.jpeg", "hotel-orle.jpg"], + package = "static", +) + +# keep +go_library( + name = "static_go", + srcs = [":static"], + importpath = "code.hackerspace.pl/hscloud/hswaw/cebulacamp/landing/static", + visibility = ["//visibility:public"], +) + +container_image( + name="latest", + base="@prodimage-bionic//image", + files = ["//hswaw/cebulacamp/landing/backend:backend"], + directory = "/hscloud/hswaw/cebulacamp/landing", + entrypoint = ["/hscloud/hswaw/cebulacamp/landing/backend"], +) + +container_push( + name = "push", + image = ":latest", + format = "Docker", + registry = "registry.k0.hswaw.net", + repository = "q3k/cebulacamp-landing", + tag = "{BUILD_TIMESTAMP}-{STABLE_GIT_COMMIT}", +) diff --git a/hswaw/cebulacamp/landing/backend/BUILD.bazel b/hswaw/cebulacamp/landing/backend/BUILD.bazel new file mode 100644 index 00000000..88409517 --- /dev/null +++ b/hswaw/cebulacamp/landing/backend/BUILD.bazel @@ -0,0 +1,24 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = ["main.go"], + importpath = "code.hackerspace.pl/hscloud/hswaw/cebulacamp/landing/backend", + visibility = ["//visibility:private"], + deps = [ + "@com_github_golang_glog//:go_default_library", + "//hswaw/cebulacamp/landing:static_go", # keep + ], +) + +go_binary( + name = "backend", + embed = [":go_default_library"], + visibility = ["//visibility:public"], +) + +go_test( + name = "go_default_test", + srcs = ["main_test.go"], + embed = [":go_default_library"], +) diff --git a/hswaw/cebulacamp/landing/backend/main.go b/hswaw/cebulacamp/landing/backend/main.go new file mode 100644 index 00000000..1416d078 --- /dev/null +++ b/hswaw/cebulacamp/landing/backend/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "flag" + "fmt" + "mime" + "net/http" + "strings" + + "github.com/golang/glog" + + "code.hackerspace.pl/hscloud/hswaw/cebulacamp/landing/static" +) + +var ( + flagBind string +) + +func init() { + flag.Set("logtostderr", "true") +} + +func main() { + flag.StringVar(&flagBind, "bind", "0.0.0.0:8080", "Address at which to serve HTTP requests") + flag.Parse() + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + if path == "/" { + path = "/index.html" + } + path = strings.TrimPrefix(path, "/") + staticPath := fmt.Sprintf("hswaw/cebulacamp/landing/%s", path) + if data, ok := static.Data[staticPath]; ok { + parts := strings.Split(path, ".") + ext := fmt.Sprintf(".%s", parts[len(parts)-1]) + t := mime.TypeByExtension(ext) + w.Header().Set("Content-Type", t) + w.Write(data) + } else { + http.NotFound(w, r) + } + }) + + glog.Infof("Starting up at %v", flagBind) + err := http.ListenAndServe(flagBind, nil) + if err != nil { + glog.Exit(err) + } +}