From 79f3383fdc906641256dcc7e46c56080a6cdf38f Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Thu, 14 Jul 2022 09:59:55 -0700 Subject: [PATCH] Feature/dynamic swagger uri (#15) * made the swagger uri based on env * updated swagger to reflect the dynamic uri * updated extamples --- README.md | 1 + docker-compose.yaml | 3 +++ docs/docs.go | 2 +- docs/swagger.json | 1 - docs/swagger.yaml | 1 - main.go | 15 ++++++++++----- services/config/config.go | 4 ++-- 7 files changed, 17 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2b4ab82..403e6ba 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ This is the collection service of newsbot to pull articles from the web. 2. `curl -X 'GET' 'http://localhost:8081/api/config/sources' -H 'accept: application/json'` 6. Add any new sources 7. Add a Discord Web Hook + 1. `curl -X 'POST' 'http://localhost:8081/api/discord/webhooks/new?url=WEBHOOKURL&server=SERVERNAME&channel=CHANNELNAME' -H 'accept: application/json' -d ''` 8. Create your subscription links 1. This is a link between a source and a discord web hook. Without this, the app will not send a notification about new posts. diff --git a/docker-compose.yaml b/docker-compose.yaml index 97883bc..feee91c 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -7,6 +7,8 @@ services: api: image: ghcr.io/jtom38/newsbot.collector.api:master environment: + SERVER_ADDRESS: "localhost" + SQL_CONNECTION_STRING: "host=localhost user=postgres password=postgres dbname=postgres sslmode=disable" # Used for database migrations @@ -21,6 +23,7 @@ services: # Enable/Disable YouTube monitoring FEATURE_ENABLE_YOUTUBE_BACKEND: false + FEATURE_ENABLE_TWITCH_BACKEND: false # Set your Twitch Developer ID and Secrets here and they will be used to collect updates. TWITCH_CLIENT_ID: "" TWITCH_CLIENT_SECRET: "" diff --git a/docs/docs.go b/docs/docs.go index 63e2ec6..ddfecb0 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -518,7 +518,7 @@ const docTemplate = `{ // SwaggerInfo holds exported Swagger Info so clients can modify it var SwaggerInfo = &swag.Spec{ Version: "0.1", - Host: "localhost:8081", + Host: "", BasePath: "/api", Schemes: []string{}, Title: "NewsBot collector", diff --git a/docs/swagger.json b/docs/swagger.json index adcacd0..4362b33 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -5,7 +5,6 @@ "contact": {}, "version": "0.1" }, - "host": "localhost:8081", "basePath": "/api", "paths": { "/articles": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index a48fb98..6ab85ac 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,5 +1,4 @@ basePath: /api -host: localhost:8081 info: contact: {} title: NewsBot collector diff --git a/main.go b/main.go index 3e02f58..ea9ddb5 100644 --- a/main.go +++ b/main.go @@ -4,17 +4,21 @@ import ( "context" "fmt" "net/http" - - _ "github.com/jtom38/newsbot/collector/docs" + + "github.com/jtom38/newsbot/collector/docs" "github.com/jtom38/newsbot/collector/routes" + "github.com/jtom38/newsbot/collector/services/config" "github.com/jtom38/newsbot/collector/services/cron" ) // @title NewsBot collector // @version 0.1 // @BasePath /api - func main() { + cfg := config.New() + address := cfg.GetConfig(config.ServerAddress) + docs.SwaggerInfo.Host = fmt.Sprintf("%v:8081", address) + ctx := context.Background() c := cron.New(ctx) c.Start() @@ -22,8 +26,9 @@ func main() { server := routes.NewServer(ctx) fmt.Println("API is online and waiting for requests.") - fmt.Println("API: http://localhost:8081/api") - fmt.Println("Swagger: http://localhost:8081/swagger/index.html") + fmt.Printf("API: http://%v:8081/api\r\n", address) + fmt.Printf("Swagger: http://%v:8081/swagger/index.html\r\n", address) + err := http.ListenAndServe(":8081", server.Router) if err != nil { panic(err) diff --git a/services/config/config.go b/services/config/config.go index 016e38a..7a57411 100644 --- a/services/config/config.go +++ b/services/config/config.go @@ -11,9 +11,9 @@ import ( ) const ( - DB_URI string = "DB_URI" + ServerAddress = "SERVER_ADDRESS" - Sql_Connection_String string = "SQL_CONNECTION_STRING" + Sql_Connection_String = "SQL_CONNECTION_STRING" FEATURE_ENABLE_REDDIT_BACKEND = "FEATURE_ENABLE_REDDIT_BACKEND" REDDIT_PULL_TOP = "REDDIT_PULL_TOP"