This commit is contained in:
DeveloperDurp 2024-07-28 09:40:32 -05:00
parent 035a813154
commit 67b4818274
10 changed files with 181 additions and 33 deletions

51
.air.toml Normal file
View file

@ -0,0 +1,51 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ."
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false
[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"
[log]
main_only = false
time = false
[misc]
clean_on_exit = false
[proxy]
app_port = 0
enabled = false
proxy_port = 0
[screen]
clear_on_rebuild = false
keep_scroll = true

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
tmp

7
go.mod
View file

@ -1,3 +1,10 @@
module gitlab.com/developerdurp/durpweb
go 1.22.0
require (
github.com/MicahParks/keyfunc v1.9.0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
gitlab.com/developerdurp/logger v1.0.0 // indirect
gitlab.com/developerdurp/middleware v0.0.0-20240504132822-e2b82ba68384 // indirect
)

9
go.sum Normal file
View file

@ -0,0 +1,9 @@
github.com/MicahParks/keyfunc v1.9.0 h1:lhKd5xrFHLNOWrDc4Tyb/Q1AJ4LCzQ48GVJyVIID3+o=
github.com/MicahParks/keyfunc v1.9.0/go.mod h1:IdnCilugA0O/99dW+/MkvlyrsX8+L8+x95xuVNtM5jw=
github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
gitlab.com/developerdurp/logger v1.0.0 h1:wozbKR26RVoFVaUgJV2x8WsZHLWOJBqaSCSTpK7crfk=
gitlab.com/developerdurp/logger v1.0.0/go.mod h1:x6gZvBeEq8oQUXeQoLY2m78mYJjvb5KE+7Vb5AcS8oo=
gitlab.com/developerdurp/middleware v0.0.0-20240504132822-e2b82ba68384 h1:kzDu3qiYiFAIt+QDiEUe40fWCKd9cPWOXKMmVPWBeuU=
gitlab.com/developerdurp/middleware v0.0.0-20240504132822-e2b82ba68384/go.mod h1:5Bk78ogPuDsKCldVTMPK8jiFq2HUJj2ZQ8m5xVdtw04=

View file

@ -1,14 +0,0 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<script src="https://unpkg.com/htmx.org@1.9.10"></script>
</head>
<body>
<h1>Go and HTMX</h1>
<h2>{{.Message}}</h2>
<button hx-get="/time" hx-target="#time">Get Current Time</button>
<h2 id="time">Shows Current Time</h2>
</body>
</html>

40
main.go
View file

@ -2,31 +2,29 @@ package main
import (
"fmt"
"html/template"
"net/http"
"time"
"gitlab.com/developerdurp/durpweb/pkg/catfact"
"gitlab.com/developerdurp/durpweb/pkg/index"
"gitlab.com/developerdurp/middleware"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFiles("index.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
router := http.NewServeMux()
stack := middleware.CreateStack(
middleware.Logging,
)
router.HandleFunc("/", index.GetIndex)
router.HandleFunc("/catfact", catfact.GetCatFact)
server := http.Server{
Addr: ":8080",
Handler: stack(router),
}
data := struct {
Message string
}{
Message: "Hello World",
}
tmpl.Execute(w, data)
})
http.HandleFunc("/time", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Current Time: " + time.Now().Format(time.RFC1123)))
})
fmt.Println("Started http server on port :8080")
http.ListenAndServe(":8080", nil)
fmt.Println("Server listening on port :8080")
server.ListenAndServe()
}

42
pkg/catfact/handler.go Normal file
View file

@ -0,0 +1,42 @@
package catfact
import (
"encoding/json"
"html/template"
"io"
"net/http"
)
type CatFact struct {
Fact string `json:"fact"`
Length int `json:"length"`
}
func GetCatFact(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseGlob("view/*.html")
// Send a POST request to the specified URL with the request body
response, err := http.Get(
"http://catfact.ninja/fact",
)
if err != nil {
}
defer response.Body.Close()
// Read the response body
responseBody, err := io.ReadAll(response.Body)
if err != nil {
}
// Unmarshal the JSON response
var resp CatFact
if err := json.Unmarshal(responseBody, &resp); err != nil {
}
data := struct {
Fact string
}{
Fact: resp.Fact,
}
tmpl.ExecuteTemplate(w, "CatFact", data)
}

25
pkg/index/handler.go Normal file
View file

@ -0,0 +1,25 @@
package index
import (
"html/template"
"net/http"
)
func GetIndex(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseGlob("view/*.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
data := struct {
Message string
MainTitle string
}{
Message: "Hello World!!!",
MainTitle: "SimpleWebsite!",
}
tmpl.ExecuteTemplate(w, "index.html", data)
}

7
view/blocks.html Normal file
View file

@ -0,0 +1,7 @@
{{ define "header" }}
<h1 class="text-2xl font-bold my-5">Go and HTMX</h1>
<h2>{{.Message}}</h2>
{{ end }}
{{ define "CatFact" }}
<h2 id="catfact">Fact: {{.Fact}}</h2>
{{ end }}

22
view/index.html Normal file
View file

@ -0,0 +1,22 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<script src="https://unpkg.com/htmx.org@1.9.10"></script>
<script src="https://cdn.tailwindcss.com"></script>
<title>{{.MainTitle}}</title>
</head>
<body>
<div class="text-center">
{{ template "header" . }}
<button
class = "bg-blue-500 text-white py-2 px-3 my-5 rounded-lg"
hx-get="/catfact"
hx-target="#catfact"
hx-swap="outerHTML"
>Get a Cat Fact</button>
{{ template "CatFact" . }}
</div>
</body>
</html>