Skip to content

Commit

Permalink
Merge pull request #42 from stahnma/issue31
Browse files Browse the repository at this point in the history
This adds a /usage endpoint for documentation and improves the documentation sent via the application to a user requesting a new API key.
  • Loading branch information
Michael Stahnke authored Feb 8, 2024
2 parents 170a818 + 2996a9f commit 0458a10
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions cspp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/fsnotify/fsnotify v1.7.0
github.com/gin-gonic/gin v1.9.1
github.com/google/uuid v1.4.0
github.com/russross/blackfriday/v2 v2.1.0
github.com/sirupsen/logrus v1.9.3
github.com/slack-go/slack v0.12.3
github.com/spf13/pflag v1.0.5
Expand Down
2 changes: 2 additions & 0 deletions cspp/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdU
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
Expand Down
12 changes: 11 additions & 1 deletion cspp/slack_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,19 @@ func sendKeyInDM(slackId string, key string) error {
log.Debugln("(sendKeyInDM) slackId", slackId, "key", key)
slack_token := viper.GetString("slack_token")
api := slack.New(slack_token)
msg := "Your CSPP API key is: `" + key + "`" + ". Please keep it safe and do not share it with anyone. "
msg += "In most cases, you can use your key and the entire CSPP service via something like the following command:\n"
cmd := `curl -X POST \
-F "image=@/path/to/file" \
-F "caption=String you want to with the picture" \
-H "X-API-KEY: $API_KEY" \
<service URI>"`
msg += "```" + cmd + "```"
// TODO add the service URI to the message
msg += "\n See /usage for more details."

_, _, err := api.PostMessage(slackId,
slack.MsgOptionText(key, false),
slack.MsgOptionText(msg, false),
slack.MsgOptionAsUser(true),
)
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions cspp/web_receiver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"embed"
"encoding/json"
"fmt"
"io"
Expand All @@ -10,10 +11,19 @@ import (
"time"

"github.com/gin-gonic/gin"
"github.com/russross/blackfriday/v2"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

/*
This next line is significant. It tells the compiler to include the static
file in the binary. There also cannot be space between // and go:embed.
*/

//go:embed static/usage.md
var md embed.FS

// ImageInfo represents the data to be stored in the JSON file
type ImageInfo struct {
ImagePath string `json:"image_path"`
Expand Down Expand Up @@ -129,10 +139,24 @@ func receiver(done chan struct{}) {
router.POST("/upload", uploadHandler)
router.POST("/api", postApiKeyHandler)
router.DELETE("/api", deleteApiKeyHandler)
router.GET("/usage", staticFileServer)
// TODO implement the PUT method to replace the API key
router.Run(":" + viper.GetString("PORT"))
}

func staticFileServer(c *gin.Context) {
log.Debugln("(staticFileServer)")
markdownContent, err := md.ReadFile("static/usage.md")
if err != nil {
log.Errorln("Error reading usage.md:", err)
c.String(http.StatusInternalServerError, "Error reading static documentation")
return
}
html := blackfriday.Run(markdownContent)
c.Data(http.StatusOK, "text/html; charset=utf-8", html)
return
}

func deleteApiKeyHandler(c *gin.Context) {
apiKey := c.GetHeader("X-API-Key")
if c.Request.Method == "DELETE" {
Expand Down

0 comments on commit 0458a10

Please sign in to comment.