networking and shit

This commit is contained in:
2026-09-18 11:42:15 -04:00
parent 1bae4e44b0
commit d7c01f2f0e
7 changed files with 128 additions and 1 deletions
+4 -1
View File
@@ -1,4 +1,4 @@
go 1.27.0 go 1.27.1
use ( use (
./Composite_Types ./Composite_Types
@@ -6,6 +6,9 @@ use (
./courses ./courses
./formatIO ./formatIO
./functions ./functions
./networkingHTTP/cmd/client
./networkingHTTP/cmd/server
./networkingHTTP/cmd/templ
./pointers ./pointers
./strings ./strings
./types ./types
+3
View File
@@ -0,0 +1,3 @@
module zacksolutions.dev/client
go 1.27.1
+46
View File
@@ -0,0 +1,46 @@
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
const url = "https://jsonplaceholder.typicode.com"
type todo struct {
ID int `json:id`
Title string `json:title`
Status bool `json: status`
}
func handler(w http.ResponseWriter, r *http.Request) {
const base = "https://jsonplaceholder."
}
func main() {
resp, err := http.Get(url + "/todos/1/")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
var item todo
err = json.Unmarshal(body, &item)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
fmt.Printf("%#v", item)
}
}
+3
View File
@@ -0,0 +1,3 @@
module zacksolutions.dev
go 1.27.1
+16
View File
@@ -0,0 +1,16 @@
package main
import (
"fmt"
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello! This is from %s\n", r.URL.Path[1:])
}
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":9090", nil))
}
+3
View File
@@ -0,0 +1,3 @@
module zacksolutions.dev/templ
go 1.27.1
+53
View File
@@ -0,0 +1,53 @@
package main
import (
"encoding/json"
"html/template"
"log"
"net/http"
)
type todo struct {
UserID int `json:"userId"`
ID int `json:"id"`
Title string `json:"title"`
Completed bool `json:"completed"`
}
var form = `
<h2>Todo #{{.ID}}</h2>
<div>{{printf "User %d" .UserID}}</div>
<div>{{printf "%s (status: %t)" .Title .Completed}}</div>
`
func handler(w http.ResponseWriter, r *http.Request) {
const base = "https://jsonplaceholder.typicode.com/"
resp, err := http.Get(base + r.URL.Path[1:])
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
defer resp.Body.Close()
var item todo
if err = json.NewDecoder(resp.Body).Decode(&item); err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
tmpl, err := template.New("Mine").Parse(form)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := tmpl.Execute(w, item); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":9090", nil))
}