diff options
author | Matthew Lemon <y@yulqen.org> | 2024-02-08 15:17:57 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-02-08 15:17:57 +0000 |
commit | 145b0e74247a161c4c22e1aa0c5568604a3a83b6 (patch) | |
tree | 576d9efd92bd1a138459e001fa9649e7e3bc45d5 /cmd | |
parent | 3dad97d316007f345800062d518661846d0b6571 (diff) |
Moved main to cmd/web and added routes() method
Started to separate out funtionality: added an application struct and
several methods (routes(), home() and a notFound() error).
At the moment, the application struct does not carry any other
configuration objects or a database pool connection - it is just there
to get the routing going.
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/web/handlers.go | 18 | ||||
-rw-r--r-- | cmd/web/main.go | 17 | ||||
-rw-r--r-- | cmd/web/routes.go | 10 |
3 files changed, 45 insertions, 0 deletions
diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go new file mode 100644 index 0000000..aed4103 --- /dev/null +++ b/cmd/web/handlers.go @@ -0,0 +1,18 @@ +package main + +import ( + "net/http" +) + +func (app *application) notFound(w http.ResponseWriter) { + http.Error(w, http.StatusText(401), 401) +} + +func (app *application) home(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/" { + app.notFound(w) + return + } + + w.Write([]byte("Hello from DED")) +} diff --git a/cmd/web/main.go b/cmd/web/main.go new file mode 100644 index 0000000..bd7e500 --- /dev/null +++ b/cmd/web/main.go @@ -0,0 +1,17 @@ +package main + +import ( + "log" + "net/http" +) + +type application struct{} + +func main() { + app := &application{} + // mux := http.NewServeMux() + // mux.HandleFunc("/", home) + log.Print("starting server on :4000") + err := http.ListenAndServe(":4000", app.routes()) + log.Fatal(err) +} diff --git a/cmd/web/routes.go b/cmd/web/routes.go new file mode 100644 index 0000000..2761d99 --- /dev/null +++ b/cmd/web/routes.go @@ -0,0 +1,10 @@ +package main + +import "net/http" + +func (app *application) routes() *http.ServeMux { + mux := http.NewServeMux() + + mux.HandleFunc("/", app.home) + return mux +} |