Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go #5

Merged
merged 4 commits into from
Mar 11, 2022
Merged

Go #5

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ bazel-out
bazel-testlogs

__pycache__

.idea
Empty file added BUILD.bazel
Empty file.
38 changes: 38 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,41 @@ pip_install(
name = "python_deps",
requirements = "//third_party:requirements.txt",
)

### GO

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "io_bazel_rules_go",
sha256 = "2b1641428dff9018f9e85c0384f03ec6c10660d935b750e3fa1492a281a53b0f",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip",
],
)

http_archive(
name = "bazel_gazelle",
sha256 = "de69a09dc70417580aabf20a28619bb3ef60d038470c7cf8442fafcf627c21cb",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")

go_repository(
name = "github_com_gorilla_mux",
importpath = "github.com/gorilla/mux",
sum = "h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=",
version = "v1.8.0",
)

go_rules_dependencies()

go_register_toolchains(version = "1.17.2")

gazelle_dependencies(go_repository_default_config = "@//:WORKSPACE.bazel")
14 changes: 14 additions & 0 deletions projects/greeter/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "greeter",
importpath = "github.com/kriscfoster/multi-language-bazel-monorepo/projects/greeter",
srcs = ["greeter.go"],
visibility = ["//projects/my-go-app:__pkg__"],
)

go_test(
name = "greeter_test",
srcs = ["greeter_test.go"],
embed = [":greeter"]
)
5 changes: 5 additions & 0 deletions projects/greeter/greeter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package greeter

func Greet() string {
return "Hello World!"
}
11 changes: 11 additions & 0 deletions projects/greeter/greeter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package greeter

import "testing"

func TestGreeter(t *testing.T) {
expected := "Hello World!"
greeting := Greet()
if greeting != expected {
t.Errorf("expected %q but got %q", expected, greeting)
}
}
10 changes: 10 additions & 0 deletions projects/my-go-app/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_binary(
name = "my-go-app",
srcs = ["main.go"],
deps = [
"//projects/greeter:greeter",
"@github_com_gorilla_mux//:mux",
]
)
22 changes: 22 additions & 0 deletions projects/my-go-app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"net/http"
"log"
"github.com/gorilla/mux"
"github.com/kriscfoster/multi-language-bazel-monorepo/projects/greeter"
)

func YourHandler(w http.ResponseWriter, r *http.Request) {
log.Println("received request")
w.Write([]byte(greeter.Greet()))
}

func main() {
r := mux.NewRouter()
// Routes consist of a path and a handler function.
r.HandleFunc("/", YourHandler)

// Bind to a port and pass our router in
log.Fatal(http.ListenAndServe(":8000", r))
}