Скелет готов. Можно писать конвертер.

master
Andrey Ivanov 2020-10-23 14:18:22 +03:00 committed by Andrey Ivanov
parent 50ba0b0cca
commit 239b05206a
4 changed files with 21 additions and 17 deletions

View File

@ -21,11 +21,12 @@ func New(conf config.Config) *App {
oslog.Fatal("не удалось прикрутить логгер: ", err.Error())
}
c := cache.NewCache(conf.Cache.Capasity)
return &App{Server: &http.Server{Addr: net.JoinHostPort(conf.Server.Address, conf.Server.Port), Handler: LoggingMiddleware(http.HandlerFunc(Handler), loger)}, Log: loger, Cache: c}
return &App{Server: &http.Server{Addr: net.JoinHostPort(conf.Server.Address, conf.Server.Port)}, Log: loger, Cache: c}
}
func (s *App) Start() error {
s.Log.Infof("Server starting")
s.Handler = loggingMiddleware(handler(&s.Cache), s.Log)
_ = s.ListenAndServe()
s.Log.Infof("Server stoped")
return nil

View File

@ -12,15 +12,15 @@ import (
func buildQuery(u *url.URL) (q models.Query, err error) {
t := strings.Split(u.Path, "/")
q.Width,err=strconv.Atoi(t[1])
q.Width,err=strconv.Atoi(t[2])
if err!=nil {
return models.Query{}, errors.New("width must be an integer")
}
q.Height,err=strconv.Atoi(t[2])
q.Height,err=strconv.Atoi(t[3])
if err!=nil {
return models.Query{}, errors.New("height must be an integer")
}
tn := "http://"+strings.Join(t[3:],"/")
tn := "http://"+strings.Join(t[4:],"/")
q.URL,err=q.URL.Parse(tn)
if err!=nil {
return models.Query{}, errors.New("not valid url")
@ -29,7 +29,6 @@ func buildQuery(u *url.URL) (q models.Query, err error) {
}
func getPic(q models.Query) ([]byte,http.Header,error) {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://" + q.URL.Host + "/" + q.URL.Path, nil)
if err != nil {

View File

@ -1,24 +1,29 @@
package application
import (
"github.com/tiburon-777/OTUS_Project/previewer/cache"
"github.com/tiburon-777/OTUS_Project/previewer/logger"
"net/http"
"time"
)
func Handler(w http.ResponseWriter, r *http.Request) {
q,err := buildQuery(r.URL)
if err!=nil {
writeResponce(w, r.Header,501,[]byte("Can't parse query"))
}
pic, h, err := getPic(q)
if err!=nil {
writeResponce(w, h,501,[]byte("Have problem with cache"))
}
writeResponce(w, r.Header,200,pic)
func handler(c *cache.Cache) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
q,err := buildQuery(r.URL)
if err!=nil {
http.Error(w, "Can't parse query", http.StatusNotFound)
return
}
pic, h, err := getPic(q)
if err!=nil {
http.Error(w, "Have problem with cache", http.StatusInternalServerError)
return
}
writeResponce(w, h,200,pic)
})
}
func LoggingMiddleware(next http.Handler, l logger.Interface) http.HandlerFunc {
func loggingMiddleware(next http.Handler, l logger.Interface) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
defer func() {

View File

@ -1 +0,0 @@
package webserver