aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-03-11 18:59:56 +0000
committerMatthew Lemon <y@yulqen.org>2024-03-11 18:59:56 +0000
commit86c012d7a7155ba3f935897e00f594a6bac9786a (patch)
tree3f6bed861d6ef319d36dbeb02b35ff2c53e6657d
Initial commit
-rw-r--r--Makefile0
-rw-r--r--cmd/api/datamaps.go23
-rw-r--r--cmd/api/healthcheck.go12
-rw-r--r--cmd/api/main.go63
-rw-r--r--cmd/api/routes.go13
-rw-r--r--go.mod3
6 files changed, 114 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Makefile
diff --git a/cmd/api/datamaps.go b/cmd/api/datamaps.go
new file mode 100644
index 0000000..53f08bf
--- /dev/null
+++ b/cmd/api/datamaps.go
@@ -0,0 +1,23 @@
+package main
+
+import (
+ "fmt"
+ "net/http"
+ "strconv"
+)
+
+func (app *application) createDatamapHandler(w http.ResponseWriter, r *http.Request) {
+ fmt.Fprintln(w, "create new datamaps page")
+}
+
+func (app *application) showDatamapHandler(w http.ResponseWriter, r *http.Request) {
+ id := r.PathValue("id")
+ app.logger.Info("the id requested", "id", id)
+ id_int, err := strconv.ParseInt(id, 10, 64)
+ // TODO: Handle negative integers passed in the URL here
+ if err != nil {
+ http.NotFound(w, r)
+ return
+ }
+ fmt.Fprintf(w, "show the details for datamap %d\n", id_int)
+}
diff --git a/cmd/api/healthcheck.go b/cmd/api/healthcheck.go
new file mode 100644
index 0000000..68bed0d
--- /dev/null
+++ b/cmd/api/healthcheck.go
@@ -0,0 +1,12 @@
+package main
+
+import (
+ "fmt"
+ "net/http"
+)
+
+func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Request) {
+ fmt.Fprintln(w, "status: available")
+ fmt.Fprintf(w, "environment: %s\n", app.config.env)
+ fmt.Fprintf(w, "version: %s\n", version)
+}
diff --git a/cmd/api/main.go b/cmd/api/main.go
new file mode 100644
index 0000000..9d3e9bd
--- /dev/null
+++ b/cmd/api/main.go
@@ -0,0 +1,63 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "log/slog"
+ "net/http"
+ "os"
+ "time"
+)
+
+const version = "1.0.0"
+
+// holds the config for our application
+// We will read this from the command line flags when we run the application
+type config struct {
+ port int
+ env string
+}
+
+// This application struct holds the dependencies for our HTTP handlers, helpers and
+// middleware.
+type application struct {
+ config config
+ logger *slog.Logger
+}
+
+func main() {
+ // Instance of config
+ var cfg config
+
+ // Read the flags into the config struct. Defaults are provided if none given.
+ flag.IntVar(&cfg.port, "port", 4000, "API server port")
+ flag.StringVar(&cfg.env, "env", "development", "Environment (development|staging|production)")
+ flag.Parse()
+
+ // Initialize a new structured logger which writes to stdout
+ logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
+
+ // An instance of application struct, containing the config struct and the logger
+ app := &application{
+ config: cfg,
+ logger: logger,
+ }
+
+ // Declare an http server which listens provided in the config struct and has
+ // sensible timeout settings and writes log messages to the structured logger at
+ // Error level.
+ srv := &http.Server{
+ Addr: fmt.Sprintf(":%d", cfg.port),
+ Handler: app.routes(),
+ IdleTimeout: time.Minute,
+ ReadTimeout: 5 * time.Second,
+ WriteTimeout: 10 * time.Second,
+ ErrorLog: slog.NewLogLogger(logger.Handler(), slog.LevelError),
+ }
+
+ logger.Info("startings server", "addr", srv.Addr, "env", cfg.env)
+
+ err := srv.ListenAndServe()
+ logger.Error(err.Error())
+ os.Exit(1)
+}
diff --git a/cmd/api/routes.go b/cmd/api/routes.go
new file mode 100644
index 0000000..acf13d0
--- /dev/null
+++ b/cmd/api/routes.go
@@ -0,0 +1,13 @@
+package main
+
+import "net/http"
+
+func (app *application) routes() *http.ServeMux {
+
+ mux := http.NewServeMux()
+
+ mux.HandleFunc("GET /v1/healthcheck", app.healthcheckHandler)
+ mux.HandleFunc("POST /v1/datamaps", app.createDatamapHandler)
+ mux.HandleFunc("GET /v1/datamaps/{id}", app.showDatamapHandler)
+ return mux
+}
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..6c7a716
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,3 @@
+module git.yulqen.org/go/dbasik-go
+
+go 1.22.1