Basic flake with go http server and debugging
This commit is contained in:
10
server/default.nix
Normal file
10
server/default.nix
Normal file
@@ -0,0 +1,10 @@
|
||||
{ pkgs, ... }:
|
||||
|
||||
pkgs.buildGoModule rec {
|
||||
pname = "dynamic-frame-server";
|
||||
version = "0.0.1";
|
||||
|
||||
src = ./.;
|
||||
|
||||
vendorSha256 = "uTqGdqswbnmPsEm5NISD0Nh9ry4GLMAsuVF7+U8BRdA=";
|
||||
}
|
||||
5
server/go.mod
Normal file
5
server/go.mod
Normal file
@@ -0,0 +1,5 @@
|
||||
module git.neet.dev/zuckerberg/dynamic-frame/server
|
||||
|
||||
go 1.19
|
||||
|
||||
require github.com/go-chi/chi/v5 v5.0.8
|
||||
2
server/go.sum
Normal file
2
server/go.sum
Normal file
@@ -0,0 +1,2 @@
|
||||
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
|
||||
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
||||
38
server/main.go
Normal file
38
server/main.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
// Define a function to handle incoming requests
|
||||
func requestHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Get the 'name' variable from the request
|
||||
name := chi.URLParam(r, "name")
|
||||
|
||||
// Use a default value if 'name' is not present
|
||||
if name == "" {
|
||||
name = "World"
|
||||
}
|
||||
|
||||
// Respond with a greeting message
|
||||
fmt.Fprintf(w, "Hello, %s!\n", name)
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello, Nix!")
|
||||
|
||||
// Create a new Chi router
|
||||
router := chi.NewRouter()
|
||||
|
||||
// Register the requestHandler function to handle requests at the root path
|
||||
// and a path with the 'name' parameter
|
||||
router.Get("/", requestHandler)
|
||||
router.Get("/{name}", requestHandler)
|
||||
|
||||
// Start the HTTP server on port 8080 and log any errors
|
||||
log.Fatal(http.ListenAndServe(":8080", router))
|
||||
}
|
||||
Reference in New Issue
Block a user