Initialized proper project structure
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/robindittmar/dttmr-api/internal/api/response"
|
||||
)
|
||||
|
||||
type apiResponse struct {
|
||||
Method string `json:"method"`
|
||||
Url string `json:"url"`
|
||||
Proto string `json:"proto"`
|
||||
Header map[string]string `json:"header"`
|
||||
Host string `json:"host"`
|
||||
RemoteAddr string `json:"remoteAddr"`
|
||||
Form map[string]string `json:"form"`
|
||||
}
|
||||
|
||||
func DefaultHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var resp apiResponse
|
||||
|
||||
resp.Method = r.Method
|
||||
resp.Url = r.URL.String()
|
||||
resp.Proto = r.Proto
|
||||
resp.Header = make(map[string]string)
|
||||
resp.Host = r.Host
|
||||
resp.RemoteAddr = r.RemoteAddr
|
||||
resp.Form = make(map[string]string)
|
||||
|
||||
for k, v := range r.Header {
|
||||
resp.Header[k] = v[0]
|
||||
}
|
||||
|
||||
if err := r.ParseForm(); err == nil {
|
||||
for k, v := range r.Form {
|
||||
resp.Form[k] = v[0]
|
||||
}
|
||||
} else {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
response.JSON(w, http.StatusOK, resp)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/robindittmar/dttmr-api/internal/api/response"
|
||||
)
|
||||
|
||||
type healthResponse struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func HealthHandler(w http.ResponseWriter, r *http.Request) {
|
||||
resp := healthResponse{
|
||||
Status: "OK",
|
||||
}
|
||||
response.JSON(w, http.StatusOK, resp)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func WithTelemetry(next http.Handler) http.Handler {
|
||||
//return otelhttp.NewHandler(next, "dttmr-api")
|
||||
return next
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func JSON(w http.ResponseWriter, status int, data any) {
|
||||
payload, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
_, err := w.Write([]byte(`{"error": "internal server error: failed to marshal response"}`))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
_, err = w.Write(payload)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func Error(w http.ResponseWriter, status int, message string) {
|
||||
JSON(w, status, map[string]string{"error": message})
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/robindittmar/dttmr-api/internal/api/handler"
|
||||
"github.com/robindittmar/dttmr-api/internal/api/middleware"
|
||||
)
|
||||
|
||||
type Config struct{}
|
||||
|
||||
func NewMux(cfg Config) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("/", handler.DefaultHandler)
|
||||
mux.HandleFunc("GET /health", handler.HealthHandler)
|
||||
|
||||
var httpHandler http.Handler = mux
|
||||
httpHandler = middleware.WithTelemetry(httpHandler)
|
||||
|
||||
return httpHandler
|
||||
}
|
||||
Reference in New Issue
Block a user