Skip to content

Add swagger documentation #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 27, 2025
Merged
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
21 changes: 21 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
"github.com/acmcsufoss/api.acmcsuf.com/internal/db/models"
"github.com/gin-gonic/gin"
_ "modernc.org/sqlite"

"github.com/acmcsufoss/api.acmcsuf.com/docs"
"github.com/swaggo/files"
"github.com/swaggo/gin-swagger"
)

func main() {
Expand All @@ -40,6 +44,7 @@ func main() {
eventsService := services.NewEventsService(queries)
announcementService := services.NewAnnouncementService(queries)
router := gin.Default()

router.SetTrustedProxies([]string{
"127.0.0.1/32",
})
Expand All @@ -55,6 +60,22 @@ func main() {
log.Fatalf("Failed to start server: %v", err)
}
}()
//Setup swagger
// TODO: Implement swagger documentation
// Info:
docs.SwaggerInfo.Title = "ACM CSUF API"
docs.SwaggerInfo.Description = "This is a documentation of current API avaliable."
docs.SwaggerInfo.Host = "localhost:8080"
docs.SwaggerInfo.BasePath = "/"
docs.SwaggerInfo.Schemes = []string{"http", "https"}
documentation := router.Group("/")
{
eg := documentation.Group("/")
eg.GET("/events_handler")
eg.GET("/announcement_handler")
}
// Gin swagger serves api docs, or something like that
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

<-ctx.Done()
log.Println("Server shut down.")
Expand Down
179 changes: 179 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// 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": {
"/announcements": {
"post": {
"description": "Creates a new announcement and generates unique ID",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Announcements"
],
"summary": "Create new Announcement",
"responses": {}
}
},
"/announcements/:id": {
"get": {
"description": "Retrieves a single announcement from the database.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Announcements"
],
"summary": "Get an announcement by ID",
"parameters": [
{
"type": "string",
"description": "Announcement ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {}
}
},
"/events": {
"get": {
"description": "Gets all the events from the event database",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Events"
],
"summary": "Gets all the events",
"responses": {}
},
"post": {
"description": "Retrieves a single event from the database.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Events"
],
"summary": "Creates a new event and generates new ID",
"responses": {}
}
},
"/events/:id": {
"get": {
"description": "Retrieves a single event from the database.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Events"
],
"summary": "Get an Event by ID",
"parameters": [
{
"type": "string",
"description": "Event ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {}
},
"put": {
"description": "Updates the event of choice in the database",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Events"
],
"summary": "Update the Event of Choice",
"parameters": [
{
"type": "string",
"description": "Event ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {}
},
"delete": {
"description": "Delete the event of choice from the database",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Events"
],
"summary": "Deletes the Event of Choice",
"parameters": [
{
"type": "string",
"description": "Event ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {}
}
}
}
}`

// 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)
}
150 changes: 150 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
{
"swagger": "2.0",
"info": {
"contact": {}
},
"paths": {
"/announcements": {
"post": {
"description": "Creates a new announcement and generates unique ID",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Announcements"
],
"summary": "Create new Announcement",
"responses": {}
}
},
"/announcements/:id": {
"get": {
"description": "Retrieves a single announcement from the database.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Announcements"
],
"summary": "Get an announcement by ID",
"parameters": [
{
"type": "string",
"description": "Announcement ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {}
}
},
"/events": {
"get": {
"description": "Gets all the events from the event database",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Events"
],
"summary": "Gets all the events",
"responses": {}
},
"post": {
"description": "Retrieves a single event from the database.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Events"
],
"summary": "Creates a new event and generates new ID",
"responses": {}
}
},
"/events/:id": {
"get": {
"description": "Retrieves a single event from the database.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Events"
],
"summary": "Get an Event by ID",
"parameters": [
{
"type": "string",
"description": "Event ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {}
},
"put": {
"description": "Updates the event of choice in the database",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Events"
],
"summary": "Update the Event of Choice",
"parameters": [
{
"type": "string",
"description": "Event ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {}
},
"delete": {
"description": "Delete the event of choice from the database",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Events"
],
"summary": "Deletes the Event of Choice",
"parameters": [
{
"type": "string",
"description": "Event ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {}
}
}
}
}
Loading
Loading