I
I
Ivan Lubinsky2015-10-28 11:49:21
go
Ivan Lubinsky, 2015-10-28 11:49:21

Deploying a web application in Go?

Good afternoon.
Please tell me the algorithm for deploying a web application written in Go. Searching the internet for an answer led to Docker and cloud deployment. I also need the application to run on its own server.
I choose between several languages, Go attracts with its relative simplicity.
I apologize if the question is "stupid", please do not poke your nose and at least throw off a link to the material for self-study. But I will be grateful for a more detailed answer, because. not very strong in English.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
evnuh, 2015-10-28
@rascally_rabbit

How it works to understand. I will describe the truly correct path, but a long one: (as suggested by pygame , according to the benchmarks https://gist.github.com/hgfischer/7965620 , the native http server in Go is still faster than nginx with fcgi, because Go has shitty implementation of fcgi, so a normal http proxy_pass will be faster instead of fcgi).
1) The web server is set to normal, and not the one that is in Go itself. That is, we install nginx. We set up all url routes for him, we set up the return of statics.
2) For dynamics, nginx will contact our Go daemon. By fast-cgi protocol. It is for him, because nginx can do it and is generally good (upd: but not in Go). This is a short binary protocol by which nginx passes HTTP request headers from the browser to our Go program and receives back an HTTP response. To do this, we put the simplest wrapper for fast-cgi called spawn-fcgi, it will preconfigure and run our Go program, ready to communicate using the fast-cgi protocol.
3) In case our Go program crashes. Either we already have a system that monitors the demons and raises them in case of a fall (systemd, upstart), then we configure it, or we install one and also configure it.
I give an example of how it's all done for me (the most popular technology stack) on Debian:
1) nginx

server {
  server_name otboi.****;
  listen 80;
  include fastcgi_params;
...
  
  location / {
    fastcgi_pass unix:/var/run/otboinik.sock;
  }

2) systemd config that starts our Go program wrapped in spawn-fcgi
[Unit]
Description=Otboinik

[Service]
Restart=always
Environment=OTBOINIK_BIN=/opt/гыы/otboinik/bin/otboinik
Environment=OTBOINIK_PID=/var/run/otboinik.pid
Environment=OTBOINIK_USER=гыыыы
Environment=OTBOINIK_SOCKET=/var/run/otboinik.sock
Environment=OTBOINIK_SOCKET_USER=гыыыыы
Environment=OTBOINIK_SOCKET_MODE=0666
ExecStart=/usr/bin/spawn-fcgi -s $OTBOINIK_SOCKET -M $OTBOINIK_SOCKET_MODE -n -P $OTBOINIK_PID -u $OTBOINIK_USER -U $OTBOINIK_SOCKET_USER -G $OTBOINIK_SOCKET_USER $OTBOINIK_BIN

[Install]
WantedBy=multi-user.target

3) Go code itself
import (
  "net/http"
  "net/http/fcgi"
)

type Server struct {
}

func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
       // чиста пример
        jsonblob := r.FormValue("events")
  if jsonblob == "" {
    w.WriteHeader(http.StatusNotAcceptable)
    return
  }
}

func main() {
  server := Server{}

  func() {
    fcgi.Serve(nil, server)
  }()

  waitchan := make(chan int, 1)
  <-waitchan
}

K
Kirill, 2015-10-28
@kshvakov

If you just want to run the "binary" install supervisord.d, the config is simple
[program:you_program_name]
command=/opt/bin/you_program
numprocs=1
autostart=true
autorestart=true
startsecs=10
startretries=5
exitcodes=0,2
stopsignal=TERM
stopwaitsecs=10
manage:
sudo supervisorctl status
sudo supervisorctl start/restart you_program_name

O
Oleg Tsilyurik, 2015-10-28
@Olej

I choose between several languages, Go attracts with its relative simplicity.

The Go Programming Language A
Russian-language podcast about Go
There you will find...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question