From 0cd379f653380c2a2001f4fcc65f31356113916a Mon Sep 17 00:00:00 2001 From: Nathan Wagner Date: Sat, 5 Aug 2023 18:29:36 +0000 Subject: [PATCH] initial commit --- .devcontainer/devcontainer.json | 25 +++++++++++++++++++ snippetbox/go.mod | 3 +++ snippetbox/main.go | 43 +++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 .devcontainer/devcontainer.json create mode 100644 snippetbox/go.mod create mode 100644 snippetbox/main.go diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..a69be52 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,25 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the +// README at: https://github.com/devcontainers/templates/tree/main/src/go +{ + "name": "Go", + // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile + "image": "mcr.microsoft.com/devcontainers/go:1-1.20-bullseye", + "features": { + "ghcr.io/devcontainers/features/git-lfs:1": {} + } + + // Features to add to the dev container. More info: https://containers.dev/features. + // "features": {}, + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + + // Use 'postCreateCommand' to run commands after the container is created. + // "postCreateCommand": "go version", + + // Configure tool-specific properties. + // "customizations": {}, + + // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. + // "remoteUser": "root" +} diff --git a/snippetbox/go.mod b/snippetbox/go.mod new file mode 100644 index 0000000..56729d7 --- /dev/null +++ b/snippetbox/go.mod @@ -0,0 +1,3 @@ +module gitea.wagshome.duckdns.org/nathan/golearn + +go 1.20 diff --git a/snippetbox/main.go b/snippetbox/main.go new file mode 100644 index 0000000..73c2e8c --- /dev/null +++ b/snippetbox/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "strconv" +) + +func home(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/" { + http.NotFound(w, r) + return + } + w.Write([]byte("hello from Snippetbox")) +} + +func snippetView(w http.ResponseWriter, r *http.Request) { + id, err := strconv.Atoi(r.URL.Query().Get("id")) + if err != nil || id < 1 { + http.NotFound(w, r) + return + } + fmt.Fprintf(w, "Display a specific snippet with ID %d...", id) +} +func snippetCreate(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + w.Header().Set("Allow", http.MethodPost) + http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed) + return + } + w.Write([]byte("Create a new snippet")) +} + +func main() { + mux := http.NewServeMux() + mux.HandleFunc("/", home) + mux.HandleFunc("/snippet/view", snippetView) + mux.HandleFunc("/snippet/create", snippetCreate) + log.Print("Starting server on :4000") + err := http.ListenAndServe(":4000", mux) + log.Fatal(err) +}