N
N
NOONE2018-11-26 23:05:05
go
NOONE, 2018-11-26 23:05:05

How to write web applications on GO?

Hi, I'm learning GO, I'm trying to write an application on GO at the same time, simple work with the twitch API, but I can't figure out how to properly organize the project structure (due to lack of development experience) I've been sitting around for 3 days and don't know how to do it right, now I do it like this:
goTwitch - the root directory of the project
- model - work with the database and the consul
- controller - I push the processing of configs (parsers) here until I figure out what else will be here
- route - these are application routes
- templates - these are application templates
Something it seems to me that this structure is some kind of crap
main file looks like this for now

package main

import (
  "goTwitch/controller"
  "fmt"
  "net/http"
  "time"
  "log"
  "goTwitch/route"
)

func main() {
  Serverconfig := controller.ConfigPars()  //парсим конфиг
  handler := http.NewServeMux()
    handler.HandleFunc("/hello/" , route.Handlers(w http.Request, r *http.Request{})) - Это функция обработчик из файла route
  s := http.Server{  //Параметры сервера 
     Addr: Serverconfig.Port, 
     Handler: handler,
     ReadTimeout: time.Duration(Serverconfig.ReadTimeout) * time.Second,  
     WriteTimeout: time.Duration(Serverconfig.WriteTimeout) * time.Second,
  }
      log.Fatal(s.ListenAndServe()) -//запуск сервера 
  }

route file
package route

import "net/http"

func Handlers(r *http.Request) {
 Каокй-то код обработки
}

config.go - parse yaml with web server configuration
package controller

import (
  "io/ioutil"
  "path/filepath"

  "gopkg.in/yaml.v2"
  "log"
)

type Serverconfig struct {
  Port         string `yaml:"Port"`
  ReadTimeout  int    `yaml:"ReadTimeout"`
  WriteTimeout int    `yaml:"WriteTimeout"`
}

func ConfigPars() *Serverconfig {
  filename, _ := filepath.Abs("./config.yml")
  yamlFile, err := ioutil.ReadFile(filename)
  if err != nil {
    panic(err)
  }
  Serverconfig := &Serverconfig{}
  //err = yaml.Decoder{ &Serverconfig}
  err = yaml.Unmarshal(yamlFile, &Serverconfig)
  if err != nil {
    log.Fatal(err)
  }
  return Serverconfig
}

While it all looks like this, I have already tried many options, while I think I will stop there, but I feel this is also not the right approach.
There is another question regarding the parser of the yaml file, if you remove Port:: 9090 from the yaml file, the config is parsed and does not swear that it does not match the structure in go, what am I doing wrong? it seems that if the structure in go does not match the file, then Anmarshl should give an error?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
stratosmi, 2018-11-27
@stratosmi

revel take, for example, as a basis.
or beego.me

A
abbaboka, 2018-12-04
@abbaboka

but I can't figure out how to properly organize the project structure (due to lack of development experience)

Take a ready-made framework that implies a certain organization of files.
So full frameworks for web applications are not really needed in Go.
But in your case - it is worth taking them to guide you.
Complete, not abbreviated. That is, of the Revel type.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question