Compare commits

..

4 Commits

Author SHA1 Message Date
299e586d69 feat: combine everything together in main executable 2025-05-18 12:30:04 +02:00
2e15406237 feat: http api server 2025-05-18 12:29:55 +02:00
0958e96310 feat: user routes 2025-05-18 12:29:49 +02:00
338ecf660c feat: install chi router 2025-05-18 12:29:44 +02:00
5 changed files with 84 additions and 27 deletions

50
cmd/hspguard/api/api.go Normal file
View File

@ -0,0 +1,50 @@
package api
import (
"fmt"
"log"
"net/http"
"gitea.local/admin/hspguard/internal/repository"
"gitea.local/admin/hspguard/internal/user"
"github.com/go-chi/chi/v5"
)
type APIServer struct {
addr string
repo *repository.Queries
}
func NewAPIServer(addr string, db *repository.Queries) *APIServer {
return &APIServer{
addr: addr,
repo: db,
}
}
func (s *APIServer) Run() error {
router := chi.NewRouter()
router.Route("/api/v1", func(r chi.Router) {
userHandler := user.NewUserHandler()
userHandler.RegisterRoutes(r)
})
// Handle unknown routes
router.NotFound(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
_, _ = fmt.Fprint(w, `{"error": "404 - route not found"}`)
})
router.MethodNotAllowed(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprint(w, `{"error": "405 - method not allowed"}`)
})
log.Println("Listening on", s.addr)
return http.ListenAndServe(s.addr, router)
}

View File

@ -2,11 +2,10 @@ package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"gitea.local/admin/hspguard/cmd/hspguard/api"
"gitea.local/admin/hspguard/internal/repository"
"github.com/jackc/pgx/v5"
)
@ -14,34 +13,16 @@ import (
func main() {
ctx := context.Background()
fmt.Println("Hello, World!")
conn, err := pgx.Connect(ctx, os.Getenv("DATABASE_URL"))
if err != nil {
os.Exit(1)
}
defer conn.Close(ctx)
repo := repository.New(conn)
// id, err := repo.InsertUser(ctx, repository.InsertUserParams{
// Email: "test@test.com",
// FullName: "Test John",
// PasswordHash: "fjeijh3uhit5hg45bjkf4ghy8ft548",
// IsAdmin: true,
// })
// if err != nil {
// log.Fatalln("ERR: Failed to insert user:", err)
// return
// }
users, _ := repo.FindAllUsers(ctx)
dump, err := json.Marshal(users)
if err != nil {
log.Fatalln("ERR: Failed to marshal response:", err)
log.Fatalln("ERR: Failed to connect to db:", err)
return
}
fmt.Println(string(dump))
repo := repository.New(conn)
server := api.NewAPIServer(":3000", repo)
if err := server.Run(); err != nil {
log.Fatalln("ERR: Failed to start server:", err)
}
}

1
go.mod
View File

@ -3,6 +3,7 @@ module gitea.local/admin/hspguard
go 1.24.3
require (
github.com/go-chi/chi/v5 v5.2.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect

2
go.sum
View File

@ -1,4 +1,6 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8=
github.com/go-chi/chi/v5 v5.2.1/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=

23
internal/user/routes.go Normal file
View File

@ -0,0 +1,23 @@
package user
import (
"fmt"
"net/http"
"github.com/go-chi/chi/v5"
)
type UserHandler struct {}
func NewUserHandler() *UserHandler {
return &UserHandler{}
}
func (h *UserHandler) RegisterRoutes(router chi.Router) {
router.Get("/login", h.handleLogin)
}
func (h *UserHandler) handleLogin(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintf(w, "/GET Hello, user from %s", r.RemoteAddr)
}