Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build]
cmd = "go build -o ./tmp/main ./cmd/camp"
bin = "./tmp/main"
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=8080
60 changes: 58 additions & 2 deletions cmd/camp/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
package main

import (
"encoding/json"
"log"
"net/http"
"os"
"time"

"github.com/codersgyan/camp/internal/contact"
"github.com/codersgyan/camp/internal/database"
"github.com/joho/godotenv"
httpSwagger "github.com/swaggo/http-swagger/v2"

_ "github.com/codersgyan/camp/docs"
)

func main() {
if err := godotenv.Load(); err != nil {
log.Printf("warning: could not load .env file: %v", err)
}

port := os.Getenv("PORT")
if port == "" {
port = "8080"
}

addr := ":" + port

db, err := database.Connect("./camp_data/camp.db")
if err != nil {
log.Fatal(err)
Expand All @@ -23,7 +41,45 @@ func main() {
contactRepository := contact.NewRepository(db)
contactHandler := contact.NewHandler(contactRepository)

http.HandleFunc("POST /api/contacts", contactHandler.Create)
mux := http.NewServeMux()

// health check endpoint
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"msg": "Hi Camp"})
})

// swagger UI endpoint
mux.Handle("GET /swagger/", httpSwagger.Handler(
httpSwagger.URL("http://localhost:8080/swagger/doc.json"),
))

mux.HandleFunc("POST /api/contacts", contactHandler.Create)

log.Printf("server is running on http://localhost:%s", port)
log.Printf("checkout API Documentation is running on http://localhost:%s/swagger", port)
log.Fatal(http.ListenAndServe(addr, requestLogger(mux)))
}

type loggingResponseWriter struct {
http.ResponseWriter
statusCode int
}

func newLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter {
return &loggingResponseWriter{ResponseWriter: w, statusCode: http.StatusOK}
}

func (lrw *loggingResponseWriter) WriteHeader(code int) {
lrw.statusCode = code
lrw.ResponseWriter.WriteHeader(code)
}

log.Fatal(http.ListenAndServe(":8080", nil))
func requestLogger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
lrw := newLoggingResponseWriter(w)
next.ServeHTTP(lrw, r)
log.Printf("%s %s %d %s", r.Method, r.URL.Path, lrw.statusCode, time.Since(start))
})
}
154 changes: 154 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Package docs Code generated by swaggo/swag. DO NOT EDIT
package docs

import "github.com/swaggo/swag"

const docTemplate = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{escape .Description}}",
"title": "{{.Title}}",
"contact": {},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/api/contacts": {
"post": {
"description": "Upsert a contact record. If the email exists, tags are updated; otherwise, a new contact is created.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Contacts"
],
"summary": "Create or update a contact with tags",
"parameters": [
{
"description": "Contact payload",
"name": "contact",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/contact.Contact"
}
}
],
"responses": {
"201": {
"description": "Created",
"schema": {
"type": "object",
"additionalProperties": {
"type": "integer",
"format": "int64"
}
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
}
},
"definitions": {
"contact.Contact": {
"type": "object",
"properties": {
"created_at": {
"type": "string",
"example": "2025-11-15T00:00:00Z"
},
"email": {
"type": "string",
"example": "ava@example.com"
},
"first_name": {
"type": "string",
"example": "Krishna"
},
"id": {
"type": "integer",
"example": 0
},
"last_name": {
"type": "string",
"example": "Web"
},
"phone": {
"type": "string",
"example": "+1-555-0102"
},
"tags": {
"type": "array",
"items": {
"$ref": "#/definitions/contact.Tag"
}
},
"updated_at": {
"type": "string",
"example": "2025-11-15T00:00:00Z"
}
}
},
"contact.Tag": {
"type": "object",
"properties": {
"created_at": {
"type": "string"
},
"id": {
"type": "integer",
"example": 0
},
"text": {
"type": "string",
"example": "customers"
},
"updated_at": {
"type": "string",
"example": "2025-11-15T00:00:00Z"
}
}
}
}
}`

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "",
Host: "",
BasePath: "",
Schemes: []string{},
Title: "",
Description: "",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",
RightDelim: "}}",
}

func init() {
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
}
125 changes: 125 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
{
"swagger": "2.0",
"info": {
"contact": {}
},
"paths": {
"/api/contacts": {
"post": {
"description": "Upsert a contact record. If the email exists, tags are updated; otherwise, a new contact is created.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Contacts"
],
"summary": "Create or update a contact with tags",
"parameters": [
{
"description": "Contact payload",
"name": "contact",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/contact.Contact"
}
}
],
"responses": {
"201": {
"description": "Created",
"schema": {
"type": "object",
"additionalProperties": {
"type": "integer",
"format": "int64"
}
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
}
},
"definitions": {
"contact.Contact": {
"type": "object",
"properties": {
"created_at": {
"type": "string",
"example": "2025-11-15T00:00:00Z"
},
"email": {
"type": "string",
"example": "ava@example.com"
},
"first_name": {
"type": "string",
"example": "Krishna"
},
"id": {
"type": "integer",
"example": 0
},
"last_name": {
"type": "string",
"example": "Web"
},
"phone": {
"type": "string",
"example": "+1-555-0102"
},
"tags": {
"type": "array",
"items": {
"$ref": "#/definitions/contact.Tag"
}
},
"updated_at": {
"type": "string",
"example": "2025-11-15T00:00:00Z"
}
}
},
"contact.Tag": {
"type": "object",
"properties": {
"created_at": {
"type": "string"
},
"id": {
"type": "integer",
"example": 0
},
"text": {
"type": "string",
"example": "customers"
},
"updated_at": {
"type": "string",
"example": "2025-11-15T00:00:00Z"
}
}
}
}
}
Loading