Introducing Go and the Mggo Framework

Hello! I want to share my first programming experience in Go.

I started my way into programming as a frontend developer. Then I switched to the backend in Python and, a little for myself, to C # Asp.Net MVC. But recently, a couple of months ago, I met this beautiful language Go.

Why go?


1 - compiled language.
2 is a very unusual language.

Structures, interfaces ... I had to break my brain, relearn. And the first problem, over, is to relearn. Where are the classes? Where is OOP? Where are your favorite patterns?

But breaking my head a little, Go language was revealed to me and I fell in love with it. Well, of course, the first thing I decided to write my first "bike", namely the web framework called MGGO.

What I wanted and what I aspired to


Of course, I immediately wanted to make an MVC framework like Asp.Net. But it turned out to be not so simple, because the Go language is peculiar. But here's what happened:

Controllers


The controller is a structure in which methods can be both external (api) and only internal, as well as those that are responsible for the view.

Immediately on an example, consider a news controller.

import mggo
type News struct{
   ID int
   Title string
   Text string 
}
func NewNews() *NewNews{
   return &NewNews{}
}
func init() {
    // регистрируем контроллер
   mggo.RegisterController("news", NewNews)
}
func(n *News) Read(ctx *mggo.BaseContext) News{
   return News{1, "First News", "Text first News"}
}
func(n *News) IndexView(ctx *mggo.BaseContext, data *mggo.ViewData, path []string){
   data.View = "news/news.html"
   data.Data["News"]
   data.Data["News"] = n.Read()
}


After that, the news is available at the link / news /

Where news is the name of the controller, and the main page is index, for which the IndexView method is responsible.

Now about api. The News controller has a Read method. By default, it is only service. In order to allow api request to this method, you need to add rights to the method in the controller's “init” function.

func init(){
   ...
   mggo.AppendRight("News.Read", mggo.RRightGuest)
}

After that, the Read method is available for api invocation to any user.

fetch("/api/", {
        "method": "POST",
        "headers": {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        "body": JSON.stringify({
            "method": "News.Read",
            "params": {}
        })
}).then(res => res.Json()).then(console.log) 

Result

{result: {ID:1, Title:"First News", Text:"Text first News"}}

A bit about the parameters. All parameters inside the params object will be passed as structure fields.

For example, let's slightly modify the Read function:

func(n *News) Read(ctx *mggo.BaseContext) News{
   if n.ID ==1 {
      return News{1, "First News", "Text first News"}
   } else {
      return News{}
   }
}

And the api call should be with the parameter ID: 1 - params {ID: 1})

What else?


PostgreSQL Go-pg library is connected in the framework . Let's get an example right away. Our news controller has static data. It needs to be changed. To do this, the first thing you need to do is create a News table. Add a couple of lines to the init function.

func init(){
   ...
   mggo.InitCallback(func(){
      mggo.CreateTable([]interface{}{(*News)(nil)})
   })
}

Now, after initialization, we get the news table, with the fields id, title and text.

Change the Read method:

func(n *News) Read(ctx *mggo.BaseContext) News{
   mggo.SQL().Select(n)
   return *n
}

If we call the News.Read method with the ID: 1 parameter, as a result we get the data from the news table with the key 1.

For go-pg features, see the documentation .

But that is not all. Everything cannot be described here. Hopefully I will write and post detailed documentation soon. Here is a brief summary of what Mggo can do.

⁃ Web sockets
⁃ Server and client events
⁃ Caching method results
⁃ Support for JsonRPC calls
⁃ Liba for quickly creating a controller and view

That's all for now. I hope someone will find the article useful. Of course, there are a bunch of other ready-made and cool frameworks. I just wanted to try to do something of my own for my personal experience.

Link to the project

Link to an example and quick start

Also popular now: