diff --git a/.air.toml b/.air.toml new file mode 100644 index 0000000..58fff2a --- /dev/null +++ b/.air.toml @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a9a5aec --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tmp diff --git a/go.mod b/go.mod index a2a0ced..da31753 100644 --- a/go.mod +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..261f1fc --- /dev/null +++ b/go.sum @@ -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= diff --git a/index.html b/index.html deleted file mode 100644 index 4be1715..0000000 --- a/index.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - -

Go and HTMX

-

{{.Message}}

- -

Shows Current Time

- - \ No newline at end of file diff --git a/main.go b/main.go index d10cc4a..62178ae 100644 --- a/main.go +++ b/main.go @@ -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) - } - data := struct { - Message string - }{ - Message: "Hello World", - } + router := http.NewServeMux() - tmpl.Execute(w, data) - }) + stack := middleware.CreateStack( + middleware.Logging, + ) - http.HandleFunc("/time", func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("Current Time: " + time.Now().Format(time.RFC1123))) - }) + router.HandleFunc("/", index.GetIndex) + router.HandleFunc("/catfact", catfact.GetCatFact) - fmt.Println("Started http server on port :8080") - http.ListenAndServe(":8080", nil) + server := http.Server{ + Addr: ":8080", + Handler: stack(router), + } + + fmt.Println("Server listening on port :8080") + server.ListenAndServe() } diff --git a/pkg/catfact/handler.go b/pkg/catfact/handler.go new file mode 100644 index 0000000..875bf88 --- /dev/null +++ b/pkg/catfact/handler.go @@ -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) +} diff --git a/pkg/index/handler.go b/pkg/index/handler.go new file mode 100644 index 0000000..19e95de --- /dev/null +++ b/pkg/index/handler.go @@ -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) + +} diff --git a/view/blocks.html b/view/blocks.html new file mode 100644 index 0000000..372b1b6 --- /dev/null +++ b/view/blocks.html @@ -0,0 +1,7 @@ +{{ define "header" }} +

Go and HTMX

+

{{.Message}}

+{{ end }} +{{ define "CatFact" }} +

Fact: {{.Fact}}

+{{ end }} diff --git a/view/index.html b/view/index.html new file mode 100644 index 0000000..07e43a1 --- /dev/null +++ b/view/index.html @@ -0,0 +1,22 @@ + + + + + + + + {{.MainTitle}} + + +
+ {{ template "header" . }} + + {{ template "CatFact" . }} +
+ +