Skip to content

Commit

Permalink
feat(About) Load about from .json in services directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Djangss committed Dec 4, 2024
1 parent c2d2010 commit 2bae543
Show file tree
Hide file tree
Showing 9 changed files with 574 additions and 54 deletions.
2 changes: 2 additions & 0 deletions server/internal/consts/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ const EnvFile = ".env"
const EnvFileDirectory = "."

const MessageQueue = "message_queue"

const ServiceFileDirectory = "./services"
54 changes: 7 additions & 47 deletions server/internal/controllers/about.go
Original file line number Diff line number Diff line change
@@ -1,68 +1,28 @@
package controllers

import (
"AREA/internal/models"
"AREA/internal/pkg"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
"time"
)

type action struct {
Name string `json:"name"`
Description string `json:"description"`
}

type reaction struct {
Name string `json:"name"`
Description string `json:"description"`
}

type service struct {
Name string `json:"name"`
Actions []action `json:"actions"`
Reaction []reaction `json:"reaction"`
}

func getServiceList() []service {
return []service{
{
Name: "mail",
Actions: []action{
{
Name: "send",
Description: "send mail",
},
},
Reaction: []reaction{
{
Name: "receive",
Description: "receive mail",
},
},
},
}
}

// About godoc
// @Summary About
// @Description about
// @Tags about
// @Accept json
// @Produce json
// @Success 200 {msg} string
// @Router /about.json [get]
// About handler with cached service data
func About(c *gin.Context) {
var msg struct {
Client struct {
Host string `json:"host"`
} `json:"client"`
Server struct {
CurrentTime string `json:"current_time"`
Services []service
CurrentTime string `json:"current_time"`
Services []models.ServiceList `json:"services"`
} `json:"server"`
}

msg.Client.Host = c.ClientIP()
msg.Server.CurrentTime = strconv.FormatInt(time.Now().Unix(), 10)
msg.Server.Services = getServiceList()
msg.Server.Services = pkg.CachedServices
c.JSON(http.StatusOK, msg)
}
25 changes: 25 additions & 0 deletions server/internal/models/about.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package models

type parameter struct {
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"type"`
}

type action struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters []parameter `json:"parameters"`
}

type reaction struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters []parameter `json:"parameters"`
}

type ServiceList struct {
Name string `json:"name"`
Actions []action `json:"actions"`
Reaction []reaction `json:"reactions"`
}
41 changes: 41 additions & 0 deletions server/internal/pkg/about.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package pkg

import (
"AREA/internal/consts"
"AREA/internal/models"
"github.com/goccy/go-json"
"io/ioutil"
"log"
"path/filepath"
)

var CachedServices []models.ServiceList

func InitServiceList() {
files, err := filepath.Glob(filepath.Join(consts.ServiceFileDirectory, "*.json"))
if err != nil {
log.Println("Error loading service files:", err)
return
}

var services []models.ServiceList
for _, file := range files {
data, err := ioutil.ReadFile(file)
if err != nil {
log.Printf("Error reading file %s: %v", file, err)
continue
}

var srv models.ServiceList
err = json.Unmarshal(data, &srv)
if err != nil {
log.Printf("Error unmarshalling file %s: %v", file, err)
continue
}

services = append(services, srv)
}

CachedServices = services
log.Printf("Loaded %d services at startup.", len(CachedServices))
}
10 changes: 3 additions & 7 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package main

import (
"AREA/internal/config"
"AREA/internal/models"
db "AREA/internal/pkg"
"AREA/internal/pkg"
"AREA/internal/routers"
"fmt"
"github.com/gin-gonic/gin"
Expand All @@ -27,11 +26,8 @@ import (
// @BasePath /
func main() {
config.LoadConfig()
db.InitDB()
err := db.DB.AutoMigrate(&models.User{})
if err != nil {
return
}
pkg.InitDB()
pkg.InitServiceList()
gin.SetMode(config.AppConfig.GinMode)
router := routers.SetupRouter()
port := strconv.Itoa(config.AppConfig.Port)
Expand Down
118 changes: 118 additions & 0 deletions server/services/discord.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{
"name": "discord",
"actions": [
{
"name": "A new message is sent",
"description": "Triggered when a new message is sent in a Discord channel.",
"parameters": [
{
"name": "channel_id",
"description": "The ID of the Discord channel where the message was sent.",
"type": "string"
},
{
"name": "message_content",
"description": "The content of the message.",
"type": "string"
},
{
"name": "author_id",
"description": "The ID of the user who sent the message.",
"type": "string"
}
]
},
{
"name": "A message is deleted",
"description": "Triggered when a message is deleted in a Discord channel.",
"parameters": [
{
"name": "channel_id",
"description": "The ID of the Discord channel where the message was deleted.",
"type": "string"
},
{
"name": "message_id",
"description": "The ID of the deleted message.",
"type": "string"
}
]
},
{
"name": "A message is edited",
"description": "Triggered when a message is edited in a Discord channel.",
"parameters": [
{
"name": "channel_id",
"description": "The ID of the Discord channel where the message was edited.",
"type": "string"
},
{
"name": "message_id",
"description": "The ID of the edited message.",
"type": "string"
},
{
"name": "new_content",
"description": "The new content of the edited message.",
"type": "string"
}
]
}
],
"reactions": [
{
"name": "Send a message in a channel",
"description": "Send a message to a specific Discord channel.",
"parameters": [
{
"name": "channel_id",
"description": "The ID of the Discord channel where the message should be sent.",
"type": "string"
},
{
"name": "message_content",
"description": "The content of the message to send.",
"type": "string"
}
]
},
{
"name": "Delete a message",
"description": "Delete a specific message in a Discord channel.",
"parameters": [
{
"name": "channel_id",
"description": "The ID of the Discord channel where the message should be deleted.",
"type": "string"
},
{
"name": "message_id",
"description": "The ID of the message to delete.",
"type": "string"
}
]
},
{
"name": "Pin/Unpin a message",
"description": "Pin or unpin a specific message in a Discord channel.",
"parameters": [
{
"name": "channel_id",
"description": "The ID of the Discord channel where the message should be pinned or unpinned.",
"type": "string"
},
{
"name": "message_id",
"description": "The ID of the message to pin or unpin.",
"type": "string"
},
{
"name": "is_pinned",
"description": "Set to true to pin the message, or false to unpin it.",
"type": "bool"
}
]
}
]
}
102 changes: 102 additions & 0 deletions server/services/google.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
"name": "google",
"actions": [
{
"name": "Email Received",
"description": "Triggered when a new email is received in your Gmail inbox.",
"parameters": [
{
"name": "sender_email",
"description": "The email address of the sender.",
"type": "string"
},
{
"name": "subject",
"description": "The subject of the email.",
"type": "string"
},
{
"name": "received_date",
"description": "The date and time when the email was received.",
"type": "string"
}
]
},
{
"name": "Email Sent",
"description": "Triggered when an email is sent from your Gmail account.",
"parameters": [
{
"name": "recipient_email",
"description": "The email address of the recipient.",
"type": "string"
},
{
"name": "subject",
"description": "The subject of the sent email.",
"type": "string"
},
{
"name": "sent_date",
"description": "The date and time when the email was sent.",
"type": "string"
}
]
}
],
"reactions": [
{
"name": "Send an Email",
"description": "Send an email using your Gmail account.",
"parameters": [
{
"name": "recipient_email",
"description": "The email address of the recipient.",
"type": "string"
},
{
"name": "subject",
"description": "The subject of the email.",
"type": "string"
},
{
"name": "body",
"description": "The content of the email.",
"type": "string"
}
]
},
{
"name": "Reply to an Email",
"description": "Reply to a specific email in your Gmail inbox.",
"parameters": [
{
"name": "email_id",
"description": "The unique ID of the email to reply to.",
"type": "string"
},
{
"name": "reply_body",
"description": "The content of the reply.",
"type": "string"
}
]
},
{
"name": "Marked as read/unread",
"description": "Mark an email as read or unread in your Gmail inbox.",
"parameters": [
{
"name": "email_id",
"description": "The unique ID of the email to mark.",
"type": "string"
},
{
"name": "is_read",
"description": "Set to true to mark as read, or false to mark as unread.",
"type": "bool"
}
]
}
]
}
Loading

0 comments on commit 2bae543

Please sign in to comment.