Скелет готов. Можно писать конвертер.
This commit is contained in:
parent
50ba0b0cca
commit
239b05206a
@ -21,11 +21,12 @@ func New(conf config.Config) *App {
|
|||||||
oslog.Fatal("не удалось прикрутить логгер: ", err.Error())
|
oslog.Fatal("не удалось прикрутить логгер: ", err.Error())
|
||||||
}
|
}
|
||||||
c := cache.NewCache(conf.Cache.Capasity)
|
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 {
|
func (s *App) Start() error {
|
||||||
s.Log.Infof("Server starting")
|
s.Log.Infof("Server starting")
|
||||||
|
s.Handler = loggingMiddleware(handler(&s.Cache), s.Log)
|
||||||
_ = s.ListenAndServe()
|
_ = s.ListenAndServe()
|
||||||
s.Log.Infof("Server stoped")
|
s.Log.Infof("Server stoped")
|
||||||
return nil
|
return nil
|
||||||
|
@ -12,15 +12,15 @@ import (
|
|||||||
|
|
||||||
func buildQuery(u *url.URL) (q models.Query, err error) {
|
func buildQuery(u *url.URL) (q models.Query, err error) {
|
||||||
t := strings.Split(u.Path, "/")
|
t := strings.Split(u.Path, "/")
|
||||||
q.Width,err=strconv.Atoi(t[1])
|
q.Width,err=strconv.Atoi(t[2])
|
||||||
if err!=nil {
|
if err!=nil {
|
||||||
return models.Query{}, errors.New("width must be an integer")
|
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 {
|
if err!=nil {
|
||||||
return models.Query{}, errors.New("height must be an integer")
|
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)
|
q.URL,err=q.URL.Parse(tn)
|
||||||
if err!=nil {
|
if err!=nil {
|
||||||
return models.Query{}, errors.New("not valid url")
|
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) {
|
func getPic(q models.Query) ([]byte,http.Header,error) {
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
req, err := http.NewRequest("GET", "https://" + q.URL.Host + "/" + q.URL.Path, nil)
|
req, err := http.NewRequest("GET", "https://" + q.URL.Host + "/" + q.URL.Path, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1,24 +1,29 @@
|
|||||||
package application
|
package application
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/tiburon-777/OTUS_Project/previewer/cache"
|
||||||
"github.com/tiburon-777/OTUS_Project/previewer/logger"
|
"github.com/tiburon-777/OTUS_Project/previewer/logger"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Handler(w http.ResponseWriter, r *http.Request) {
|
func handler(c *cache.Cache) http.Handler {
|
||||||
q,err := buildQuery(r.URL)
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if err!=nil {
|
q,err := buildQuery(r.URL)
|
||||||
writeResponce(w, r.Header,501,[]byte("Can't parse query"))
|
if err!=nil {
|
||||||
}
|
http.Error(w, "Can't parse query", http.StatusNotFound)
|
||||||
pic, h, err := getPic(q)
|
return
|
||||||
if err!=nil {
|
}
|
||||||
writeResponce(w, h,501,[]byte("Have problem with cache"))
|
pic, h, err := getPic(q)
|
||||||
}
|
if err!=nil {
|
||||||
writeResponce(w, r.Header,200,pic)
|
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) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
defer func() {
|
defer func() {
|
||||||
|
@ -1 +0,0 @@
|
|||||||
package webserver
|
|
Loading…
x
Reference in New Issue
Block a user