<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Benard Mseti</title>
    <description>The latest articles on Forem by Benard Mseti (@godopetza).</description>
    <link>https://forem.com/godopetza</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1085552%2F5cd77348-00cd-4595-891f-c6073b9deeaa.jpeg</url>
      <title>Forem: Benard Mseti</title>
      <link>https://forem.com/godopetza</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/godopetza"/>
    <language>en</language>
    <item>
      <title>How to work with OpenAI APIs</title>
      <dc:creator>Benard Mseti</dc:creator>
      <pubDate>Sun, 20 Aug 2023 14:02:25 +0000</pubDate>
      <link>https://forem.com/godopetza/how-to-work-with-openai-apis-1478</link>
      <guid>https://forem.com/godopetza/how-to-work-with-openai-apis-1478</guid>
      <description>&lt;p&gt;If you are confused about how OpenAI APIs work, this post will give you a high overview and explain to you how you can utilize the Basic Chat Completion API.&lt;/p&gt;

&lt;p&gt;Just like any other RESTful APIs you have worked with before, all we do is setup a request from our client and send the payload to OpenAI’s server and we will receive a response back from them based on the query we sent.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6UwyTHKO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t637s1yazyyxfl31ymmq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6UwyTHKO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t637s1yazyyxfl31ymmq.jpg" alt="Image description" width="671" height="291"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But what do you put in the client request?&lt;/p&gt;

&lt;p&gt;The client request is composed of the instructions that we want OpenAI to process and then send back a response based off this payload. For OpenAI, the payload will consist of the model we want to use, messages (prompt, assistant and user messages) and temperature.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6BZ6Wq2D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y77s1gg7nph5jieoweog.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6BZ6Wq2D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y77s1gg7nph5jieoweog.jpg" alt="Image description" width="761" height="651"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As seen above, messages will contain three sets of roles, system, assistant, and user. All the messages should be added to this list if you are working with a chatbot that needs to have the memory of previous chats between user and assistant.&lt;/p&gt;

&lt;p&gt;For the temperature, the lower the temperature the more predictable and repetitive the responses returned will be. 0.7 is the recommended temperature but tune it to fit your needs.&lt;/p&gt;

&lt;p&gt;I hope you have learned something new!&lt;/p&gt;

&lt;p&gt;Don’t forget to follow my socials for more of these short tutorials.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/godopetza"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/benard-andrew-5b29069b/"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>openai</category>
      <category>api</category>
    </item>
    <item>
      <title>Set up a Stripe Checkout REST API (+ metadata) using Go and Gin Framework.</title>
      <dc:creator>Benard Mseti</dc:creator>
      <pubDate>Wed, 14 Jun 2023 16:30:23 +0000</pubDate>
      <link>https://forem.com/godopetza/set-up-a-stripe-checkout-rest-api-metadata-using-go-and-gin-framework-pek</link>
      <guid>https://forem.com/godopetza/set-up-a-stripe-checkout-rest-api-metadata-using-go-and-gin-framework-pek</guid>
      <description>&lt;p&gt;Implementing payment APIs for a go backend may be a struggle not only for gin framework developers that are just starting out but also the ones that have been using the framework for a while.&lt;/p&gt;

&lt;p&gt;This article will show how to simply integrate a stripe checkout REST API that will produce links to a stripe checkout screen for your users to perform payments in your apps.&lt;/p&gt;

&lt;p&gt;First install stripe package using this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go get -u github.com/stripe/stripe-go/v74
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now add this controller&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func CreateCheckoutUrl(c *gin.Context) {
    var body struct {
        Amount       int64  `form:"amount"`
        ReceiptEmail string `form:"email"`
                Metadata     string `form:"metadata"`
    }

    if err := c.Bind(&amp;amp;body); err != nil {
        c.JSON(http.StatusBadRequest, gin.H{
            "error": "Failed to read body",
        })

        return
    }

    apiKey := os.Getenv("STRIPE_SECRET_KEY")
    stripe.Key = apiKey

    domain := "https://your.domain.com/"
    params := &amp;amp;stripe.CheckoutSessionParams{
        LineItems: []*stripe.CheckoutSessionLineItemParams{
            {
                PriceData: &amp;amp;stripe.CheckoutSessionLineItemPriceDataParams{
                    Currency: stripe.String("usd"),
                    ProductData: &amp;amp;stripe.CheckoutSessionLineItemPriceDataProductDataParams{
                        Name: stripe. String("Product Name Here"),
                    },

                    TaxBehavior: stripe.String("exclusive"),
                    UnitAmount:  stripe.Int64(body.Amount * 100),
                },
                Quantity: stripe.Int64(1),
            },
        },
        CustomerEmail: &amp;amp;body.ReceiptEmail,
        Mode:          stripe.String(string(stripe.CheckoutSessionModePayment)),
        SuccessURL:    stripe.String(domain + "/"),
        CancelURL:     stripe.String(domain + "/"),
        AutomaticTax:  &amp;amp;stripe.CheckoutSessionAutomaticTaxParams{Enabled: stripe.Bool(true)},
    }

    params.AddMetadata("id", body.Metadata)

    fmt.Println("params - ", params)

    s, err := session.New(params)

    if err != nil {
        log.Printf("Error creating session: %v", err)
        c.JSON(http.StatusInternalServerError, gin.H{
            "error": err.Error(),
        })
        return
    }

    c.JSON(http.StatusOK, gin.H{
        "checkout_url": s.URL,
    })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In your routes add the following endpoint&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// stripe api
    r.POST("/create-checkout-url/", controllers.CreateCheckoutUrl)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Done, You can now deploy your gin backend and test the API by making a post request to the endpoint we just created.&lt;/p&gt;

</description>
      <category>go</category>
      <category>tutorial</category>
      <category>gin</category>
      <category>stripe</category>
    </item>
    <item>
      <title>Simplest way to deploy a Flutter Web App</title>
      <dc:creator>Benard Mseti</dc:creator>
      <pubDate>Mon, 12 Jun 2023 12:57:50 +0000</pubDate>
      <link>https://forem.com/godopetza/simplest-way-to-deploy-a-flutter-web-app-pc6</link>
      <guid>https://forem.com/godopetza/simplest-way-to-deploy-a-flutter-web-app-pc6</guid>
      <description>&lt;p&gt;Flutter is a cross-platform framework which allows us to build and deploy apps for different platforms using a single codebase.&lt;/p&gt;

&lt;p&gt;In this tutorial, you'll learn how to deploy your Flutter webapp using &lt;a href="https://railway.app?referralCode=Szi12H" rel="noopener noreferrer"&gt;Railway&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's dive in!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Open your IDE terminal and run&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

flutter build web --web-renderer html


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will ensure that the webapp uses html rendering on all devices that it is accessed from.&lt;/p&gt;

&lt;p&gt;Let the app build run:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F74jrtfpfqw16tsluoc7w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F74jrtfpfqw16tsluoc7w.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the project folder look for build/web file directory&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5mh30br4iu2hsmm9j7yg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5mh30br4iu2hsmm9j7yg.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Right click on the web directory and open the file -&amp;gt; Use Finder(mac)/File Explorer(Windows).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F93009s92svl33cqr6nal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F93009s92svl33cqr6nal.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The web directory carries our whole compiled Flutter web app.&lt;/p&gt;

&lt;p&gt;Next step is to commit the files to github.&lt;/p&gt;

&lt;p&gt;Open &lt;a href="https://github.com" rel="noopener noreferrer"&gt;github&lt;/a&gt; and create a new repository with your desired name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5dlwftgwcpwrzhot7jyg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5dlwftgwcpwrzhot7jyg.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbvc4fdpr041pwc8ypksp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbvc4fdpr041pwc8ypksp.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can make the repository public/private depending on your requirements.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcdf1lqbqtdl44ok3c2d0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcdf1lqbqtdl44ok3c2d0.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on upload existing files as seen above and then drag and drop the contents from our web directory to the github repository and wait for all files to be uploaded.&lt;/p&gt;

&lt;p&gt;Once upload is done, commit changes and wait for the files to be proceed.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5dnzywfkhijja6kwbvma.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5dnzywfkhijja6kwbvma.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your repository should now look like this 👇🏾&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4zvwcehdxuakhbno638y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4zvwcehdxuakhbno638y.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's set up railway&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next go to &lt;a href="https://railway.app?referralCode=Szi12H" rel="noopener noreferrer"&gt;railway&lt;/a&gt; and follow below steps.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Click on start a new project&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fphq3xgo6zfokhsliaf5l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fphq3xgo6zfokhsliaf5l.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Choose deploy from github repo&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fugy92d0ageb9et2y8gdf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fugy92d0ageb9et2y8gdf.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Login and authorize railway with your github account&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftgz8fvxtddoialf73qfu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftgz8fvxtddoialf73qfu.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choose repository to deploy, ours was 'myappname'&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Funbwdt45ptucxhdvs56y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Funbwdt45ptucxhdvs56y.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Select Deploy Now&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdvuyp51m2vdhucwzj08u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdvuyp51m2vdhucwzj08u.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Wait for build to finish&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fogconxta81jwc5i4422u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fogconxta81jwc5i4422u.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Select Add a Domain&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffbqcwc2xoipal9yq24sn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffbqcwc2xoipal9yq24sn.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Voila! You now have successfully deployed your Flutter Webapp, you can access it through the link provided.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyjs0z4q1vtyxxz5l9z0u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyjs0z4q1vtyxxz5l9z0u.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's the &lt;a href="https://myappname-production.up.railway.app/#/" rel="noopener noreferrer"&gt;example site&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fipqjbanz4okp29ugcjqk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fipqjbanz4okp29ugcjqk.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/godopetza/myappname" rel="noopener noreferrer"&gt;github webapp project&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Connect with me on twitter 😊 if you have any questions.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>flutter</category>
      <category>railway</category>
      <category>deploy</category>
    </item>
    <item>
      <title>Configure CORS for a Go backend</title>
      <dc:creator>Benard Mseti</dc:creator>
      <pubDate>Sun, 11 Jun 2023 08:41:35 +0000</pubDate>
      <link>https://forem.com/godopetza/configure-cors-for-a-go-backend-40m0</link>
      <guid>https://forem.com/godopetza/configure-cors-for-a-go-backend-40m0</guid>
      <description>&lt;p&gt;According to mozilla.org, Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources.&lt;/p&gt;

&lt;p&gt;When writing a backend for your app using golang in the local environment you can perform requests to your APIs without the need to configure CORS, but once you deploy the backend you will require CORS or else you will face errors communicating with the backend.&lt;/p&gt;

&lt;p&gt;This tutorial will help you fix this error in a go backend that uses gin framework.&lt;/p&gt;

&lt;p&gt;We will first download and install the cors middleware using the following line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go get github.com/gin-contrib/cors
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then now import to your main.go file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "github.com/gin-contrib/cors"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Now let's add cors middleware&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Add the following code in the main.go file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func main() {
    r := gin.Default()
    config := cors.DefaultConfig()
    config.AllowAllOrigins = true
    config.AllowMethods = []string{"POST", "GET", "PUT", "OPTIONS"}
    config.AllowHeaders = []string{"Origin", "Content-Type", "Authorization", "Accept", "User-Agent", "Cache-Control", "Pragma"}
    config.ExposeHeaders = []string{"Content-Length"}
    config.AllowCredentials = true
    config.MaxAge = 12 * time.Hour

    r.Use(cors.New(config))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your main.go file should now look like below now;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "time"

    "github.com/gin-contrib/cors"
    "github.com/gin-gonic/gin"
    "github.com/username/appname/controllers"
)

func init() {
    //initialize app
}

func main() {
    r := gin.Default()
    config := cors.DefaultConfig()
    config.AllowAllOrigins = true
    config.AllowMethods = []string{"POST", "GET", "PUT", "OPTIONS"}
    config.AllowHeaders = []string{"Origin", "Content-Type", "Authorization", "Accept", "User-Agent", "Cache-Control", "Pragma"}
    config.ExposeHeaders = []string{"Content-Length"}
    config.AllowCredentials = true
    config.MaxAge = 12 * time.Hour

    r.Use(cors.New(config))

        //Add endpoints here
    r.POST("/ping/", controllers.Pong)

    r.Run()
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can now deploy and run your go backend successfully on a serverless platform like &lt;a href="https://railway.app?referralCode=Szi12H"&gt;railway&lt;/a&gt; easily and CORS error should be fixed now.&lt;/p&gt;

&lt;p&gt;Have any questions?&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
