Skip to content
Open
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
31 changes: 31 additions & 0 deletions go/api-GorillaMux/.idx/dev.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# To learn more about how to use Nix to configure your environment
# see: https://developers.google.com/idx/guides/customize-idx-env
{ pkgs, ... }: {
# Which nixpkgs channel to use.
channel = "stable-25.05"; # or "unstable"
# Use https://search.nixos.org/packages to find packages
packages = [
pkgs.go
pkgs.air
];
# Sets environment variables in the workspace
env = {};
idx = {
# Search for the extensions you want on https://open-vsx.org/ and use "publisher.id"
extensions = [
"golang.go"
];
workspace = {
onCreate = {
# Open editors for the following files by default, if they exist:
go-mod-tidy = "go mod tidy";
default.openFiles = ["main.go"];
};
# Runs when a workspace is (re)started
onStart= {
run-server = "air";
};
# To run something each time the workspace is first created, use the `onStart` hook
};
};
}
3 changes: 3 additions & 0 deletions go/api-GorillaMux/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This is a minimal Go backend API service based on: https://cloud.google.com/run/docs/quickstarts/build-and-deploy/deploy-go-service

Server should be run automatically when starting a workspace. Use `go run main.go` to run manually.
3 changes: 3 additions & 0 deletions go/api-GorillaMux/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/GoogleCloudPlatform/golang-samples/run/helloworld

go 1.20
37 changes: 37 additions & 0 deletions go/api-GorillaMux/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"fmt"
"log"
"net/http"
"os"

"github.com/gorilla/mux"
)

func main() {
log.Print("starting server...")
r := mux.NewRouter()
r.HandleFunc("/", handler)

// Determine port for HTTP service.
port := os.Getenv("PORT")
if port == "" {
port = "3000"
log.Printf("defaulting to port %s", port)
}

// Start HTTP server.
log.Printf("listening on http://localhost:%s", port)
if err := http.ListenAndServe(":"+port, r); err != nil {
log.Fatal(err)
}
}

func handler(w http.ResponseWriter, r *http.Request) {
name := os.Getenv("NAME")
if name == "" {
name = "World"
}
fmt.Fprintf(w, "Hello %s!\n", name)
}
3 changes: 2 additions & 1 deletion go/idx-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"options": {
"web": "Web server",
"web-template": "Web server with HTML templates",
"api": "API Server"
"api": "API Server",
"api-GorillaMux": "API-GorillaMux"
}
}
]
Expand Down