Preface
In last part Blog Building From Zero (2) Basic Server: HTTP, HTTPS, and Nginx, we build a https server with nginx. In this part, our goal is to build a full stack blog server.
Design
As mentioned before, there are lots of blog server application. With these apps, we just need to add articles in it. But what we want to do, is to develop a blog server application ourselves.
As for how to design blog server, first question is to chooze monolithic architecture or frontend-backend decoupling architecture. With monolithic architecture, it may be simpler to develop but when adding new feature it may be more complexe. So I chooze to frontend-backend decoupling architecture.
As for how to build frontend and backend, I chooze Nodejs, React, and Typescript for frontend and Golang for backend. Nodejs, React, and Typescript are very popular and with the component it is easy to build a website. And Golang is very efficient to build a backend application.
So the whole architecture will be like:
frontend side
Client —>(send page request)—-> Nodejs
Client <—(receive React page)<— Nodejs
backend side
Client —>(send data request)—-> Nodejs —>(proxy request)—> Golang
Client <—(receive data)<——— Nodejs <—(receive data)<—- Golang
A bit of more explain:
- Nodejs is the server application that receive request from client first
- When client is requst a page, Nodejs will return a html that is build by React
- To render that page, client may need some data. For example, client may need article list. The requset action is defined in the react html.
- When client browse that page, it will send a fetch data request to Nodejs, and Nodejs will proxy that request to real backend developed by Golang.
Git
To build a new application, of course we need git to maintain our code.
To use github (or other repository platform), we need sign up new account and install git in our vm, and set up proper git config to push and pull code with github.
To focus on development part, we will leave how to use github and git to Google and Chatgpt.
The frontend and backend github repository is listed below:
Nodejs and React
First we need to install Node
|
|
And then we will init Node, Typescript, and necessary lib.
|
|
Init React, Typescript, and necessary lib.
|
|
After init, we will develop the server.ts file for Nodejs like this.
Then we need to set up tsconfig.json for Nodejs. To proper import lib, module
and target
should be set to esnext
, and exclude
set to ["blog/*"]
. In case when convert Typescript file into js, it will look into React project.
And then we can convert server.ts to server.js, and run it.
|
|
There are better way to test. I would recommand use VSCode as IDE to connect to remote VM and use port forward to browse it in local browser.
After build Nodejs server, we need to build the website page. And before that, we should design how the page should look like. There are many fancy ways to put on amazing effect on the website. I prefer to make it simple and easy to read. So I designed the main page layout into a header, and two column under header. Left column is article list, and right column is some introdution and feature may be added in the future. And detail page (example) layout is simple header and the detail.
To be more specific, I developed a App component as a Router and it define the header too like below:
|
|
There are two main component in it, ArticalList
and ArticleDetail
. We can look into the detail in this file and the css is in this file.
A tricky part worth mentioning is that how to render markdown and code in it. I use react-markdown
to render markdown and react-syntax-highlighter
to render code in markdown like below.
|
|
Dont forget to set jsx
to react
in tsconfig.json
.
Finish developed React project, run npm run build
to build it.
I run into some problems in it. Because the vm memory is few (2 GB), when build react it always taken up too much memory causing system to crash.
Before I upgraded vm to 8 GB, I use cgroup to control memory usage of node.
|
|
It help me from crashing system, which can lead to failure of build command. So eventully I upgraded vm for better use.
Aftere React is prepared, we can run Nodejs server.
|
|
Once Nodejs server run normally, we can configurate it with nginx.
|
|
With nginx set up, when we browse the blog with https, we should be able to open react page just designed.
Golang
We have developed the frontend, and now it comes to the backend.
The backend will provide article list, detail and image.
About how to organize these data, I have several thoughts, mysql, elasticsearch, or local file. Using database makes it easy to do query and maintain data. But it will be more troublesome to configurate and adding new article. So I decide to use local file pulling from github in the beginning. In this way I can push new article to github, and set a crontab to fetch data from github into local. So the update is nearly real time.
|
|
And then I develop API to fetch list and detail of article with Gin. Gin is a http web framework and very easy to use. The router and handler is developed.
There is a problem of how to show image in article. In markdown there are some three ways to show image.
- Upload image into some place, such as github, or cloud provider, got its url and put it in markdown. Some website may prevent others from read its image, and when putting image in cloud provider, we have to put image (cloud provider) and article (github) in different place, and if the free browsing is used up, we will be charged.
- Convert image into base64 data, and put base64 data in markdown. The biggest problem is base64 data is very very long, and make markdown so ugly.
- Maintain the image url in own server, and put the url in markdown. I chooze this way, because I can put article and image in the same git repository. And the backend will provide article and image together by local file pulled from github.
After decided how to design backend, lets review basic Go usage, download and install go following its guide.
|
|
After development, we can run the golang, prepare some articles and test it.
|
|
It should return article list. If it does, we can test it in blog.
Deploy
If above steps works fine, we should be able to browse the blog. And we can push new article to git to update the blog.
One problem left is that the node and golang application will not start itself if system reboot or some error happen. So we can use set up a service to make sure it.
|
|
Congratulations! A blog server is up and running in the vm.
Summary
In this part We introduced how to build a frontend-backend decoupling architecture blog server with Nodejs, React, Typescript and Golang. And we deploy it in vm. With these set up, we can push new article to git, and it will update blog in nearly realtime.
What is not so perfect is that the server is just deployed in one vm, if that vm is down, then the whole blog is down. So in next part Blog Building From Zero (4) Distrubted Deploy: Docker and Kubernetes, we will introduce how to use docker and kubernetes to deploy blog server in multi vms.