Answer the question
In order to leave comments, you need to log in
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()) -//запуск сервера
}
package route
import "net/http"
func Handlers(r *http.Request) {
Каокй-то код обработки
}
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
}
Answer the question
In order to leave comments, you need to log in
but I can't figure out how to properly organize the project structure (due to lack of development experience)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question