Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 38ef6be

Browse files
Adding 'greet', a replacement for socat
1 parent 4bf3e93 commit 38ef6be

File tree

21 files changed

+11508
-0
lines changed

21 files changed

+11508
-0
lines changed

greet/cmd/greet.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
"os"
9+
10+
"gopkg.in/yaml.v3"
11+
)
12+
13+
// config represents to contents of our YAML configuration file. It's fields are
14+
// exported for this purpose.
15+
type config struct {
16+
Name string
17+
Port int
18+
}
19+
20+
func main() {
21+
// Declare and parse our CLI flags.
22+
configFile := flag.String("c", "", "Path to the YAML config file")
23+
flag.Parse()
24+
25+
// Ensure that the '-c' flag was passed.
26+
if *configFile == "" {
27+
log.Fatal("Flag '-c' is required, use '-help' for more info")
28+
}
29+
30+
// Load the contents of the config file into memory.
31+
configData, err := os.ReadFile(*configFile)
32+
if err != nil {
33+
log.Fatalf("While loading the config file at %q: %s", *configFile, err)
34+
}
35+
36+
// Unmarshal our config file.
37+
var conf config
38+
err = yaml.Unmarshal(configData, &conf)
39+
if err != nil {
40+
log.Fatalf("While unmarshaling the config file at %q: %s", *configFile, err)
41+
}
42+
43+
// Declare our request handler.
44+
http.HandleFunc(
45+
"/",
46+
func(w http.ResponseWriter, r *http.Request) {
47+
fmt.Fprintf(w, "Hello, %s", conf.Name)
48+
},
49+
)
50+
51+
// Start our web server. If an error is encountered it will log and exit
52+
// immediately.
53+
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", conf.Port), nil))
54+
}

greet/example/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
name: Samantha
3+
port: 1234

greet/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/letsencrypt/hashicorp-lessons/greet
2+
3+
go 1.17
4+
5+
require gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b

greet/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
4+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

greet/vendor/gopkg.in/yaml.v3/LICENSE

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

greet/vendor/gopkg.in/yaml.v3/NOTICE

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

greet/vendor/gopkg.in/yaml.v3/README.md

Lines changed: 150 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)