forked from hswaw/hscloud
cebulacamp/landing: build and push backend
Change-Id: I1336fb2fe52de7c42e5de0c4f1e05f42c32a9777master
parent
69c7f99810
commit
b9ed123ff2
|
@ -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}",
|
||||
)
|
|
@ -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"],
|
||||
)
|
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue