<?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>DEV Community: FRicardi</title>
    <description>The latest articles on DEV Community by FRicardi (@fricardi).</description>
    <link>https://dev.to/fricardi</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%2F761727%2F07c6f87a-815b-4e94-8831-203cd1855ae0.png</url>
      <title>DEV Community: FRicardi</title>
      <link>https://dev.to/fricardi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fricardi"/>
    <language>en</language>
    <item>
      <title>Should I stay or should I Go?</title>
      <dc:creator>FRicardi</dc:creator>
      <pubDate>Tue, 03 Oct 2023 03:00:00 +0000</pubDate>
      <link>https://dev.to/fricardi/should-i-stay-or-should-i-go-45ki</link>
      <guid>https://dev.to/fricardi/should-i-stay-or-should-i-go-45ki</guid>
      <description>&lt;p&gt;One of the thing that has been helping me to keep pushing with my studies is to share them. This is something that makes me stay focused on doing things properly, without skipping steps. So, on this article I will be sharing my thoughts on how it was to learn Go Lang and how I've explored some tools.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I want?
&lt;/h2&gt;

&lt;p&gt;So, first of all, for you to understand what I was trying to accomplish here, let me introduce myself. On the start of my career, I've worked mostly with landing pages creation with pure HTML/CSS/JS, as well as some simple PHP systems. Later on, spent some time on a Node JS with microservices migration project, and a complex Java Spring based tool. Recently, working almost full time on frontend with React.&lt;br&gt;
To make sure that I will not be retained only to learning the tools I'm currently working, I've started exploring some trending techs and documenting them here. On &lt;a href="https://dev.to/fricardi/svelte-my-new-pet-framework-8k0"&gt;this article&lt;/a&gt;, you can find my thoughts on how it was to work with Svelte (spoiler: I loved it!). &lt;br&gt;
GoLang is something that I've always wanted to check because of its pointer usage, as well as error handling techniques and performance. First of all, I want to know how to make a basic API. Later on, I will try to implement the &lt;a href="https://github.com/zanfranceschi/rinha-de-backend-2023-q3" rel="noopener noreferrer"&gt;Rinha de Backend&lt;/a&gt; challenge to dive deeper in some topics.&lt;/p&gt;
&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;To install Go to my system, I've used &lt;a href="https://dev.to/deadwin19/how-to-install-golang-on-wslwsl2-2880"&gt;this great post&lt;/a&gt; and the official documentation. The entire setup did not took more than 5 minutes, which is a great starting point.&lt;/p&gt;

&lt;p&gt;On the official site, Go has a &lt;a href="https://go.dev/tour/welcome/1" rel="noopener noreferrer"&gt;tour&lt;/a&gt; that showcases the functionalities and syntax of the language, using some examples and exercises to make sure that you understood that.&lt;/p&gt;
&lt;h3&gt;
  
  
  Highlights
&lt;/h3&gt;

&lt;p&gt;The first few concepts were nothing to write home about, mostly present on all languages, so the only thing to take away was how to write it in Go. A really nice thing on the functions is that you can return two (or more) values, which allows you to do a lot of fun things, which we'll talk about later. Other small thing is that &lt;code&gt;switch/case&lt;/code&gt; has the &lt;code&gt;break&lt;/code&gt; functionality by default on each &lt;code&gt;case&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;One remarkable concept is that they use the &lt;code&gt;for&lt;/code&gt; loop encapsulates the functionalities of &lt;code&gt;for&lt;/code&gt;, &lt;code&gt;do while&lt;/code&gt;, &lt;code&gt;while&lt;/code&gt; and &lt;code&gt;for each&lt;/code&gt;. It is nice to have only one keyword for it, although I can see that as difficult to grasp for beginners. By the time I've ended my proof of concept, it was totally natural to use the &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;Being able to use scoped variables on the &lt;code&gt;if&lt;/code&gt; clause, combined with functions returning two values makes it work almost as a &lt;code&gt;try/catch&lt;/code&gt; block. It took a while for me to adapt to this mindset.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we are getting to my Achilles heel: pointers. It was an alien concept for me when I've learned C. Not because I've didn't understand it, but because I've never got to learn C on real world examples, so I haven't internalized it. On the docs, it seemed straight forward how go uses it, and the preferences to send a reference to an object and assign to it inside a method. For now, seems enough.&lt;/p&gt;

&lt;p&gt;The first time I've looked to Arrays in Go I thought I was going to drop the goal of learning this language. Honestly, what the f* are slices? Why would I need an extra abstraction for it? Java all over again... &lt;/p&gt;

&lt;p&gt;But fear no more, when the tour start showing how to use slices and how they behave, you understand that there's no need for panicking about it.&lt;/p&gt;

&lt;p&gt;Other powerful feature, which I haven't got the chance to use on my POC, was the &lt;code&gt;defer&lt;/code&gt; clause. It waits for the method to return before executing the code near to it. I can see it saving my life in some real world cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rest API
&lt;/h2&gt;

&lt;p&gt;On Go official documentation, they have a &lt;a href="https://go.dev/doc/tutorial/web-service-gin" rel="noopener noreferrer"&gt;walkthrough&lt;/a&gt; on web services with gin. &lt;/p&gt;

&lt;p&gt;It's nice to see that we can create a struct and add properties to it, such as changing their accessors when it is transformed to JSON for RestAPI returns. Since Go uses PascalCase for package accessors, that is specially helpful to maintain consistency.&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%2F9dc0sqr08rvwv0cz4wjl.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%2F9dc0sqr08rvwv0cz4wjl.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Other than that, there's not much to say as someone who is already familiar with web services. You create a router, assign HTTP verbs with paths and a method to handle it, and raise the server. To return a value, you can use gin context to return an HTTP status and the data that you want to send to the consumer. &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%2Fzv13d2dme1s2ocnsoy7b.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%2Fzv13d2dme1s2ocnsoy7b.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;GetAlbums&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;albums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;albumService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetAlbums&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IndentedJSON&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusOK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;albums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IndentedJSON&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusNotFound&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;H&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"message"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Adding database
&lt;/h3&gt;

&lt;p&gt;First example from the tutorial was with data stored on an array, the next step was to add database access to improve the POC. &lt;br&gt;
The way I've chosen to do it was by creating a global variable on a connector file, and creating a Getter method to access that variable on other places&lt;/p&gt;

&lt;p&gt;I've implemented a first version with the raw database connector, using queries, etc. It was not hard, but, as usual when not using an ORM, it was too verbose.&lt;/p&gt;

&lt;p&gt;For a final version, I've used GORM, which abstracts a lot the database usage. In my opinion, the final code ended up being quite readable. &lt;/p&gt;

&lt;p&gt;connector.go&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;gorm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DB&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;ConnectToDatabase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;
    &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gorm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sqlite&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"database.sqlite"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;gorm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Config&lt;/span&gt;&lt;span class="p"&gt;{})&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Connected!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AutoMigrate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Album&lt;/span&gt;&lt;span class="p"&gt;{})&lt;/span&gt;

    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Migrated successfully!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;GetDB&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;gorm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DB&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;ConnectToDatabase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;albumService.go&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;GetAlbums&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Album&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;albums&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Album&lt;/span&gt;

    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;connector&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetDB&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;albums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Could not find albums"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;albums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Starting this session with a shout out to Go team: what an amazing documentation. Gotta show some love to the people maintaining it. It's totally doable to learn the language only from the official docs, from their tutorials, tours and technical doc itself. &lt;/p&gt;

&lt;p&gt;The language has a lot of quirks of its own, which is really messy at first sight. I feel that, for most of these, seeing it in action was enough to understand why it is there and what was the thought process of doing it that way. &lt;/p&gt;

&lt;p&gt;Error handling and pointers seems to be two of the many things that will make this technology shine and worth choosing on a real use case. &lt;/p&gt;

&lt;p&gt;What I've tried to explore the most out of the official tutorials was to create separate files for each concern, such as controllers, services and database connectors. Go packages and their scopes were not hard, but I've stumbled a lot on the beginning, when I've tried to understand what I could and could not access.&lt;/p&gt;

&lt;p&gt;Given this, I feel that my first goal was accomplished. And, if I see myself on a situation where I need to use Go, I won't be totally lost and know where to look for some answers. On top of that, Go community seem to be quite supportive and growing, which will help on edge cases.&lt;/p&gt;

&lt;p&gt;See you on the next learning endeavors!&lt;/p&gt;

&lt;p&gt;You can find the source code of my POC &lt;a href="https://github.com/FRicardi/learning-go" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>go</category>
      <category>learning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>A new way of saying good morning to my dad</title>
      <dc:creator>FRicardi</dc:creator>
      <pubDate>Thu, 07 Sep 2023 15:43:40 +0000</pubDate>
      <link>https://dev.to/fricardi/a-new-way-of-saying-good-morning-to-my-dad-3mp0</link>
      <guid>https://dev.to/fricardi/a-new-way-of-saying-good-morning-to-my-dad-3mp0</guid>
      <description>&lt;h2&gt;
  
  
  Motivation
&lt;/h2&gt;

&lt;p&gt;My dad loves good morning images. Those with happy little animals, whimsical forests and a happy message to start the day. Every morning he receives a lot of them, and forwards to his friends and family. &lt;/p&gt;

&lt;p&gt;The problem is that, sometimes, he have to recycle images from other days if he does not receive any new images. Because of that, I want to send him fresh images everyday, with a unique scenery! &lt;/p&gt;

&lt;p&gt;Since I am absolutely awful with image editing, what would be better to accomplish that than generating images with Imagine, texts with ChatGPT and overlaying them on Svelte? Let's get to work!&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%2Fxnlcl13wha3spxsr2q46.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%2Fxnlcl13wha3spxsr2q46.png" alt="Good morning image example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the AI tools
&lt;/h2&gt;

&lt;p&gt;I've never worked with AI tools before. So, the first thing I've done for this project was to understand how their API's work. For the first version, I choose not to dive deep with the prompts, focusing on integrating the tools. Since OpenAI gives $5.00 as credits, I will be using their ChatGPT to create the texts. But, I did not use Dall-E to generate the images, because I would not be able to test the image prompts on their website for free. Thus, I've used &lt;a href="https://www.imagine.art/" rel="noopener noreferrer"&gt;Imagine&lt;/a&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Imagine
&lt;/h3&gt;

&lt;p&gt;On Imagine website, we can test their prompts and modifiers. This helps a lot to understand what each option change and how to use it later. By registering on their website, you receive 50 credits monthly for free. &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%2F7lfhtcsk18j7vcoe56rp.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%2F7lfhtcsk18j7vcoe56rp.png" alt="Testing Imagine image generation tool"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Their &lt;a href="https://vyroai.notion.site/Text-Img-2f702a9a89fb45299a17c9f36af33843" rel="noopener noreferrer"&gt;API usage&lt;/a&gt; is quite straightforward. It is basically a POST request, pass your token as a bearer on the header, and the parameters as multipart/form data. Prompt and style are mandatory. For the style, we can use their playground to see which one fits better. &lt;/p&gt;

&lt;p&gt;Using the parameters that I've found on the playground, I was able to quickly test the API and get similar results.&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%2F3a39ppckmqcjsfhh1vps.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%2F3a39ppckmqcjsfhh1vps.png" alt="API test with Insomnia"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  ChatGPT
&lt;/h3&gt;

&lt;p&gt;Also a very good &lt;a href="https://platform.openai.com/docs/api-reference/making-requests" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;! Here we are also able to test the requests on &lt;a href="https://platform.openai.com/playground?mode=chat&amp;amp;model=gpt-3.5-turbo" rel="noopener noreferrer"&gt;their playground&lt;/a&gt;, which allowed me to understand how to craft a good prompt to create the message that will go on the image.&lt;/p&gt;

&lt;p&gt;To generate something, you will need to pick a model and also add some modifiers to set the tone of the message. Similar to my approach with the images, I retained myself to first get the API going with a simple prompt, and study them further on the future.&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%2Ftrog9nnbmqitwvnm3tzh.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%2Ftrog9nnbmqitwvnm3tzh.png" alt="ChatGPT playground"&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%2Fz3rp7agy76pdf2y9mzk2.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%2Fz3rp7agy76pdf2y9mzk2.png" alt="ChatGPT API testing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The project
&lt;/h2&gt;

&lt;p&gt;With the API's already figured out, the next step is to understand what I would build to integrate it. My idea was to create a simple page that allows the user to generate the image with Imagine, and the tool will ask for a motivational phrase to ChatGPT to place over the image. My choice was to create some arrays of options to generate the prompt. &lt;/p&gt;

&lt;h3&gt;
  
  
  Generating the content
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Message
&lt;/h4&gt;

&lt;p&gt;If you want just to generate something, ChatGPT's library makes that as easy as it can get. You send the prompt and the modifiers that you want, and it responds with an array of completions. In this case, it will be only one response anyways. That way, we can just access the message from the first completion of the response. I've reduced a lot the maximum number of tokens, so it would not use &lt;br&gt;
many credits.&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%2Fuiop10ztf2mqkx5dwphu.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%2Fuiop10ztf2mqkx5dwphu.png" alt="Message generation code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since the response is deterministic, if you give the same prompt, you will have the same response. Because of that, I've created a basic random prompt generator, that sets the tone of the message. Positivity, blessings, motivational and friendship are some of them. Still need to improve that list so it does not get repetitive over time.&lt;/p&gt;

&lt;h4&gt;
  
  
  Image
&lt;/h4&gt;

&lt;p&gt;For the image generation, Imagine Art was called via API. With the image returned, got it as a blob and saved it into the static folder.&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%2Fs0rkgzkoesp2p80f5ic7.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%2Fs0rkgzkoesp2p80f5ic7.png" alt="Image generation code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Crafting the final art
&lt;/h3&gt;

&lt;p&gt;To place the text over the image, I've used Jimp. It was quite easy to use it, the only tricky part was that, to print some text with a font that its not their default ones, it requests a font in bitmap format. They provide some good tools that performs that conversion. So, I've downloaded a font from Google fonts and added the generated bitmap to the project.&lt;/p&gt;

&lt;p&gt;Since, with bitmap, the font has only one color, I've used the trick to create text over a transparent image and transform its color to obtain sort of a shadow effect and improve readability.&lt;/p&gt;

&lt;h2&gt;
  
  
  Results
&lt;/h2&gt;

&lt;p&gt;With the landing page created and all the login set up, we are up and running!&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%2Ff3fza5d3z5gvaw213bb6.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%2Ff3fza5d3z5gvaw213bb6.png" alt="Landing page"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first image generated with this tool represents very well the objective, with the phrase "Have a good day full of possibilities".&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%2Fgz2vl10ua424fd4t4vyj.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%2Fgz2vl10ua424fd4t4vyj.png" alt="First image generated with the tool"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Overall, the tools used were really good and had a nice interface. I struggled a bit with the image handling, since I was not familiar with it. I was trying to return the blob from the server side to client side and work with it there, but, ultimately, decided to save it to the static folder and handle it entirely on the server side, with the frontend just consuming the image from the static folder.&lt;/p&gt;

&lt;p&gt;It was really good to find tools such as Jimp, I see it helping me on the future on other situations. Albeit simple, it has a lot of tweaks and hacks that allow to create complex images.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next steps
&lt;/h2&gt;

&lt;p&gt;To further improve this project, the biggest improvement would be on creating the prompts. Currently, the most "Whastapp like" image that I could generate with simple prompts, would be some cute animals on a plant background. So, on the future, it would be nice to feed the model with other good morning messages and receive better results. &lt;/p&gt;

&lt;p&gt;Also, improve it to other circumstances, such as good night, happy birthday, merry Christmas, and other festive occasions. For that, the message and the image should be improved, to understand the context based on the user selection.&lt;/p&gt;

&lt;p&gt;Let me know if you have some suggestion or tips! Drop a heart if you liked this article :D &lt;/p&gt;

&lt;p&gt;Code for this project can be found on: &lt;a href="https://github.com/FRicardi/good-morning-sunshine" rel="noopener noreferrer"&gt;https://github.com/FRicardi/good-morning-sunshine&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>ai</category>
      <category>tutorial</category>
      <category>learning</category>
    </item>
    <item>
      <title>Svelte - My new pet framework</title>
      <dc:creator>FRicardi</dc:creator>
      <pubDate>Sun, 16 Jul 2023 15:18:55 +0000</pubDate>
      <link>https://dev.to/fricardi/svelte-my-new-pet-framework-8k0</link>
      <guid>https://dev.to/fricardi/svelte-my-new-pet-framework-8k0</guid>
      <description>&lt;p&gt;Some time ago, in a college project, I've &lt;a href="https://dev.to/fricardi/svelte-where-to-go-from-here-3c11"&gt;posted here&lt;/a&gt; about my experience with Svelte and some newbie questions about the overall structure of the framework. Recently, I was developing a prototype for my final paper at college and had the opportunity to revisit Svelte with Sveltekit and, hopefully, solve my earlier questions.&lt;/p&gt;

&lt;p&gt;While studying the framework itself and its capabilities, I came across &lt;a href="https://joyofcode.xyz/"&gt;Joy Of Code&lt;/a&gt; blog and YouTube channel, which I strongly recommend for those who are willing to understand more about it. &lt;/p&gt;

&lt;p&gt;Development itself was way smoother than the first journey, since I knew some more about the limitations of the platform. Furthermore, I almost instantaneously loved the structure of Sveltekit, it was so easy to navigate and read components and pages. But the thing I've liked the most about it is how the logic can be well separated, it just makes sense. Well, at least for me lol. &lt;/p&gt;

&lt;p&gt;So, let's get to my remarks on this (lovely) experience:&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Structure
&lt;/h2&gt;

&lt;p&gt;The first thing you have to put your mind into while using Sveltekit is its hierarchy on the file structure. When using layouts, you can nest them, and it will be prioritized from the outermost to the innermost, what gives you a powerful tool to scaffold your application. &lt;/p&gt;

&lt;p&gt;The bad (or different, may I say) part of it is that I saw myself rarely using the file tree to navigate the project, since most of the files would have the &lt;code&gt;+page.svelte&lt;/code&gt; or &lt;code&gt;+page.server.ts&lt;/code&gt; name. Mainly, I've used the &lt;code&gt;ctrl + p&lt;/code&gt; feature from VSCode to search for the wanted file. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oAWg5UAb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ri1a4bmdww7bz5ys5ctm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oAWg5UAb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ri1a4bmdww7bz5ys5ctm.png" alt="Svelte File Structure" width="198" height="558"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Isolating the logic from the server from the client using file was really nice, making it so much easier to understand which code would run server side and which would be client side. Since I've never worked on a full project with SSR in place, it was kinda hard for me to get the grip of it, but this helped me a lot, which leads to the next point.&lt;/p&gt;

&lt;h2&gt;
  
  
  Server side capabilities
&lt;/h2&gt;

&lt;p&gt;I know that SSR is no exclusive for Sveltekit, and surely no new concept overall (Omg hiiiii PHP &amp;lt;3), but this framework made it so readable and extendable that it matches almost exactly my thought process on building a system. &lt;/p&gt;

&lt;p&gt;What I've liked the most on this bit was that the lifecycle of the server was clear, so I could enter my &lt;code&gt;+page.server.ts&lt;/code&gt; file and check what I'm doing on &lt;code&gt;load&lt;/code&gt; and what are the &lt;code&gt;actions&lt;/code&gt; available on that given page. I felt that it was organized and clean, making it easier to reuse logic and also straightforward to write new code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PA1XbxZY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cntks37vh3aqo3vbpum7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PA1XbxZY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cntks37vh3aqo3vbpum7.png" alt="Server code structure" width="557" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Reviewing my old comments
&lt;/h2&gt;

&lt;p&gt;On my &lt;a href="https://dev.to/fricardi/svelte-where-to-go-from-here-3c11"&gt;first post&lt;/a&gt;, I've asked some questions, and now I have an answer for them (or at least more knowledge about it). &lt;/p&gt;

&lt;h3&gt;
  
  
  Components
&lt;/h3&gt;

&lt;p&gt;I'm not really sure why I was struggling here. On my current project, I was able to create components just like this, and accomplish exactly the behavior I was looking for. It looked great! &lt;/p&gt;

&lt;p&gt;Also, I now see that sometimes there's no problem on creating a new component that will be kind of specific if that helps on readability, for example. In this project I was more prone to let my feeling guides me towards the right approach, and that worked great until now. Since I had a time crunch to deliver, that helped on not being so much strict on creating atomic components, preferring what would make the code run better.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4Z3RNvLW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b136g23uzwf5gc07n7r7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4Z3RNvLW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b136g23uzwf5gc07n7r7.png" alt="Form component" width="734" height="322"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Svelte community is, as per my perception, still being crafted. There is a lot of people creating great content for the framework, but it is still years away from React, for example, in terms of finding answers quick and components ready to use. That being said, I see that webcomponents usage is extremely useful, which I would loved to have used more now that I got to look back. &lt;/p&gt;

&lt;h3&gt;
  
  
  Authentication and authorization
&lt;/h3&gt;

&lt;p&gt;My heart and soul goes to Sveltekit on this specific point. Layouts are just so extremely useful and readable to accomplish user authorization. Mostly, I've just created a mechanism to redirect the user if they are not logged in trying to enter a guarded area. Also, login was managed with cookies + &lt;code&gt;locals&lt;/code&gt; object on the framework. Quick to set up and easy to extend. Also, the locals object could easily be used to guard roles with inner layouts/pages.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xsgDC6_w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oshxwak6s7ydlv6d4txg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xsgDC6_w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oshxwak6s7ydlv6d4txg.png" alt="Authorization with layouts" width="477" height="288"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zQJNevWY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/riy9v4d8rfdtexlovv90.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zQJNevWY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/riy9v4d8rfdtexlovv90.png" alt="Saving the user to a hook" width="557" height="542"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Newer final thoughts
&lt;/h2&gt;

&lt;p&gt;As described on the title, Svelte and Sveltekits now are my pet frameworks. Although I have years of experience in React, Vue and Angular, I found that Svelte made me worry less about a complicated file/folder structure, giving so much more time to create a strong logic for my project. &lt;/p&gt;

&lt;p&gt;It surely seems weird to do so (mostly because we are not used to it), but I could write simple backend code on it with confidence, something I couldn't grasp with Next and Nuxt, for example. For sure we have scalability concerns, but for small systems I would go all in on this approach and feel good with it. &lt;/p&gt;

&lt;p&gt;For those who want to take a look on how the project, here is the &lt;a href="https://github.com/tcc-mentoring/frontend"&gt;github link&lt;/a&gt;. It has a lot of unfinished ideas because of time, but it was so nice to finally make an end to end project with this tech. I'm looking forward to contribute to this community and see it being even more promoted on the future.&lt;/p&gt;

&lt;p&gt;I'd love to read about your experience on this framework and what do you think about the future of this tech :D &lt;/p&gt;

</description>
      <category>svelte</category>
      <category>sveltekit</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Svelte - Where to go from here?</title>
      <dc:creator>FRicardi</dc:creator>
      <pubDate>Thu, 25 Nov 2021 04:08:25 +0000</pubDate>
      <link>https://dev.to/fricardi/svelte-where-to-go-from-here-3c11</link>
      <guid>https://dev.to/fricardi/svelte-where-to-go-from-here-3c11</guid>
      <description>&lt;h2&gt;
  
  
  The basics
&lt;/h2&gt;

&lt;p&gt;I was looking forward to learn a little bit about svelte, since i've heard a lot of fuzz on it on the last couple years. There was this college task to create a web project, and my group decided to do it with svelte. &lt;br&gt;
At first, i was really thrilled with how easy it was to create new things and how fast the application was being built.&lt;br&gt;
After finishing the MVP for the college discipline, i was left with some questions, which i would love to receive your inputs on, since i was kinda on a hurry developing this but would love to come back to it and code it properly! :) &lt;/p&gt;
&lt;h2&gt;
  
  
  How to properly extend components?
&lt;/h2&gt;

&lt;p&gt;The first thing that struck me was how to properly extend components and leverage most of the structure. The project i've created contained some forms, and i was used to the pattern that consist on creating components for inputs so it is easier to add form group inputs to a form. On svelte, it seems that i'm not able to create a component like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
    export let type = 'text';
    export let label;
&amp;lt;/script&amp;gt;
&amp;lt;div&amp;gt;
    &amp;lt;span&amp;gt;{label}&amp;lt;/span&amp;gt;
    &amp;lt;input {type} /&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The error/warning i'm getting is that the type cannot be dynamic here. The approach I took, only to finish it quicker, was to create a component for each type i've had to use, which sounded bad. &lt;strong&gt;very bad&lt;/strong&gt;. At this point it was easier to recreate the span/input structure on each input. &lt;/p&gt;

&lt;h2&gt;
  
  
  Authentication store and user
&lt;/h2&gt;

&lt;p&gt;In order to save the bearer token used to prove authentication on the api calls, i've created a store using &lt;code&gt;writable&lt;/code&gt;. To this project, i've created one for the authentication and other one to the user data. The user data one contains just the roles currently, just because i didn't need other informations so far. They looked like this:&lt;br&gt;
&lt;code&gt;auth.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import user from './user';
import { writable } from 'svelte/store'
const { subscribe, set } = writable(localStorage.getItem('token'));

const logout = () =&amp;gt; {
    set(null);
    localStorage.removeItem('token');
    user.resetUserRole();
}

const update = (newToken) =&amp;gt; {
    localStorage.setItem('token', newToken);
    set(newToken);
    user.populateUserData();
}

export default {
    subscribe, logout, update
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;user.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { writable } from 'svelte/store'
import auth from './auth'
import { userConnector } from '../backend-connector';

const { subscribe, set } = writable(localStorage.getItem('role'))

const resetUserRole = () =&amp;gt; {
    set(null);
    localStorage.removeItem('role');
}

const updateUserRole = (newRole) =&amp;gt; {
    localStorage.setItem('role', newRole);
    set(newRole);
}

const populateUserData = () =&amp;gt; {
    userConnector.getUserData().then((response) =&amp;gt; {
        if (response.ok) {
            response.json().then((data) =&amp;gt; {
                updateUserRole(data.roles.map((role) =&amp;gt; role.name));
            });
        } else {
            auth.logout();
        }
    }).catch(() =&amp;gt; {
        auth.logout();
    });
}

export default {
    subscribe, resetUserRole, updateUserRole, populateUserData
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;.then()&lt;/code&gt; structure used instead of await was used just because i was teaching my colleague how a promise works and forgot to change to the classier &lt;code&gt;await&lt;/code&gt; syntax, i know it looked lame hehe&lt;br&gt;
The objective of the &lt;code&gt;populateUserData&lt;/code&gt; method was to update user informations whenever they log in or enter the system, keeping them consistent with the &lt;code&gt;localStorage&lt;/code&gt; so it is kept whenever the user quits the tab and enter again. Although i've liked the result, and using the &lt;code&gt;svelte/store get&lt;/code&gt; method to read the content of any store was quite easy, i would like to know how you would place this structure!&lt;/p&gt;
&lt;h2&gt;
  
  
  Role guards on route
&lt;/h2&gt;

&lt;p&gt;Since there isn't any platform component for that, I've tried to create a structure that represent guards to some route based on their role on the system. It looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
    import { Route } from 'svelte-navigator';
    import auth from '../../store/auth';
    import user from '../../store/user';

    export let path;
    export let componentToRoleMapping;

    $: isAuthenticated = $auth;
    $: role = $user;
&amp;lt;/script&amp;gt;

{#if isAuthenticated &amp;amp;&amp;amp; componentToRoleMapping[role] != null}
    &amp;lt;Route path={path} component={componentToRoleMapping[role]} /&amp;gt;
{:else}
    &amp;lt;Route path={path} component={componentToRoleMapping['ELSE']} /&amp;gt;
{/if}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the &lt;code&gt;componentToRoleMapping&lt;/code&gt; object looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let feedComponentToRoleMapping = {
     'DONOR': DonorFeed,
     'ADMIN': AdminFeed,
     'ELSE': Login
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Is there some way to improve this? it felt quite right but as if something is missing. &lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;Svelte was a nice framework to learn, i feel like there is a lot of room for improvement, but it was one of the easiest ways to code from zero to hero that i've found so far. Almost zero bureaucracy and a lot of shortcuts, i would love to work with it in the future. If you have any inputs on how to improve this code, and/or have some material to learn more about the technology, i would love to see that!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>svelte</category>
      <category>webdev</category>
      <category>html</category>
    </item>
  </channel>
</rss>
