github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/docs/blog/_posts/2020-11-06-building-a-blog-with-micro-part-one.md (about) 1 --- 2 layout: post 3 author: Janos Dobronszki 4 title: Building a Blog with Micro - Part One 5 keywords: tutorials, blog 6 tags: [blog] 7 --- 8 9 This series will cover how to build a blog service using Micro. We'll decompose a monolithic Blog into multiple services. 10 In part one we'll focus on building a Post service. It will be good way to learn how to build nontrivial applications with 11 the [store](https://micro.dev/reference#store) and the [model](https://github.com/micro/dev/tree/master/model). 12 13 The most important takeaway from this post will likely be the the usage of the key-value store for non-trivial usecases 14 (such as querying blog posts by slug and listing them by reverse creation order). 15 16 ## The Basics 17 18 Head to the [Getting Started Guide](/getting-started) if you haven't used Micro before. 19 20 If you have let's use that knowledge! As a reminder, we have to make sure `micro server` is running in an other terminal, 21 and we are connected to it, ie 22 23 24 Running the micro server 25 26 ```sh 27 micro server 28 ``` 29 30 Looking up our local environment 31 32 ```sh 33 $ micro env 34 * local 127.0.0.1:8081 Local running micro server 35 dev proxy.m3o.dev Cloud hosted development environment 36 platform proxy.m3o.com Cloud hosted production environment 37 ``` 38 39 We can see the local environment picked. If not, we can issue `micro env set local` to remedy. 40 41 Now back to the `micro new` command: 42 43 ```sh 44 $ micro new posts 45 $ ls posts 46 Dockerfile Makefile README.md generate.go go.mod handler main.go proto 47 ``` 48 49 Great! The best way to start a service is to define the proto. The generated default should be something similar to this: 50 51 In our post service, we want 3 methods: 52 - `Save` for blog insert and update 53 - `Query` for reading and listing 54 - `Delete` for deletion 55 56 Let's start with the post method. 57 58 <script src="https://gist.github.com/asim/31628c6faf61b392b31bcefc79daa289.js"></script> 59 60 Astute readers might notice that although we have defined a `Post` message type, we still redefine some of the fields as top level fields for the `SaveRequest` message type. 61 The main reason for this is that we don't want our [dynamic commands](https://micro.dev/reference#dynamic-commands). 62 63 Ie. if we would embed a `Post post = 1` inside `SaveRequest`, we would call the posts service the following way: 64 65 ```sh 66 micro posts save --post_title=Title --post_content=Content 67 ``` 68 69 but we don't want to keep repeating `post`, our preferred way is: 70 71 ```sh 72 micro posts save --title=Title --content=Content 73 ``` 74 75 To regenerate the proto, we have to issue the `make proto` command in the project root. 76 77 Now, the `main.go`: 78 79 <script src="https://gist.github.com/asim/99d3bea6fdd234b373473309cab055a1.js"></script> 80 81 After that's done, let's adjust the handler to match our proto! This snippet is a bit longer, so cover it piece by piece: 82 83 <script src="https://gist.github.com/asim/10b96ed42e300959ec96e963ed4cc6e2.js"></script> 84 85 The above piece of code uses the [model package](https://github.com/micro/dev/tree/master/model). It sets up the indexes which will enable us to query the data and also tells model to maintain these indexes. 86 87 - The id index is needed to read by id 88 - The created index is needed so when we list posts the order of the posts will be descending based on the created field 89 - The slug index is needed to we can read posts by slug (ie. `myblog.com/post/awesome-post-url`) 90 91 At this point `micro run .` in project root should deploy our post service. Let's verify with `micro logs posts`: 92 93 ``` 94 $ micro logs posts 95 Starting [service] posts 96 Server [grpc] Listening on [::]:53031 97 Registry [service] Registering node: posts-b36361ae-f2ae-48b0-add5-a8d4797508be 98 ``` 99 100 (The exact output might depend on the actual config format configuraton.) 101 102 ## Saving posts 103 104 Let's make our service do something useful now: save a post. 105 106 <script src="https://gist.github.com/asim/a1870dccc4ad1eb137a602ae9e884af6.js"></script> 107 108 After a `micro update .` in project root, we can start saving posts! 109 110 ``` 111 micro posts save --id=1 --title="Post one" --content="First saved post" 112 micro posts save --id=2 --title="Post two" --content="Second saved post" 113 ``` 114 115 ## Querying posts 116 117 Again, implementation starts with defining the protos: 118 119 <script src="https://gist.github.com/asim/d3dea1832609b32538838529e693f9bf.js"></script> 120 121 A `make proto` issued in the command root should regenerate the Go proto files and we should be ready to define our new handler: 122 123 We want our query handler to enable querying by id, slug and also enable listing of posts: 124 <script src="https://gist.github.com/asim/4cc45b6fceb459332a61bed0f88a99d6.js"></script> 125 126 As mentioned, the existing indexes can be used for querying too with the `ToQuery` method. 127 128 After doing a `micro update .` in the project root, we can now query the posts: 129 130 ``` 131 $ micro posts query 132 { 133 "posts": [ 134 { 135 "id": "2", 136 "title": "Post two", 137 "slug": "post-two", 138 "content": "Second saved post", 139 "created": "1604423363" 140 }, 141 { 142 "id": "1", 143 "title": "Post one", 144 "slug": "post-one", 145 "content": "First saved post", 146 "created": "1604423297" 147 } 148 ] 149 } 150 151 ``` 152 153 Stellar! Now only `Delete` remains to be implemented to have a basic post service. 154 155 ## Deleting posts 156 157 Since we have already defined `Delete` in our proto, we only have to implement the handler. It is rather simple: 158 159 <script src="https://gist.github.com/asim/a3c7875a5fc47439b43740b274ab3ac3.js"></script> 160 161 ## Conclusions 162 163 This brings us to the end of the first post in the blogs tutorial series. 164 There are many more features we will add later, like saving and querying by tags, but this post already taught us enough to digest. 165 We will cover those aspect in later parts of this series. 166 167 The source code for this can be found [here](https://github.com/micro/dev/tree/master/blog/v1-posts). 168 Further versions will be in the same `blog` folder with different versions, ie `v2-posts` and once we have more services, `v2-tags`, `v2-comments`. 169 Folders with the same prefix will be meant to be deployed together, but more on this later. 170