feat: add order-ecommerce api initial files

This commit is contained in:
2026-08-10 19:09:43 +02:00
parent d00ed5c591
commit 029b2a7689
6 changed files with 97 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
package main
import (
"log"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
type application struct {
config config
// logger
// db driver
}
type dbConfig struct {
dsn string // database connection string
}
type config struct {
addr string // 12 factor document
db dbConfig
}
// mount
func (app *application) mount() http.Handler {
r := chi.NewRouter()
// A good base middleware stack
r.Use(middleware.RequestID) // important for rate-limiting
r.Use(middleware.ClientIPFromRemoteAddr) // also important for rate-limiting, analytics and tracing
r.Use(middleware.Logger)
r.Use(middleware.Recoverer) // nice to recover from panics and crashes
// Set a timeout value on the request context (ctx), that will signal
// through ctx.Done() that the request has timed out and further
// processing should be stopped.
r.Use(middleware.Timeout(60 * time.Second))
r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hey"))
})
return r
}
// run
func (app *application) run(h http.Handler) error {
srv := &http.Server{
Addr: app.config.addr,
Handler: h,
WriteTimeout: 30 * time.Second,
ReadTimeout: 10 * time.Second,
IdleTimeout: 60 * time.Second,
}
log.Printf("Starting server on %s\n", app.config.addr)
return srv.ListenAndServe()
}
+26
View File
@@ -0,0 +1,26 @@
package main
import (
"log/slog"
"os"
)
func main() {
cfg := config{
addr: ":8080",
db: dbConfig{},
}
api := application{
config: cfg,
}
// structed logging using slog
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
slog.SetDefault(logger)
if err := api.run(api.mount()); err != nil {
slog.Error("Server failed to start", "error", err)
os.Exit(1)
}
}
+5
View File
@@ -0,0 +1,5 @@
module github.com/rmcampos/ecom
go 1.26.5
require github.com/go-chi/chi/v5 v5.3.1
+2
View File
@@ -0,0 +1,2 @@
github.com/go-chi/chi/v5 v5.3.1 h1:3j4HZLGZQ3JpMCrPJF/Jl3mYJfWLKBfNJ6quurUGCf8=
github.com/go-chi/chi/v5 v5.3.1/go.mod h1:R+tYY2hNuVUUjxoPtqUdgBqevM9s9njzkTLutVsOCto=
@@ -0,0 +1 @@
package products
@@ -0,0 +1 @@
package products