advancing

Former-commit-id: 0ea003b2683a6174d3d304e4bd32dd01540b87df
This commit is contained in:
bel
2019-06-22 13:48:27 -06:00
parent c0c88502c4
commit 84d5906d9b
8 changed files with 62 additions and 79 deletions

View File

@@ -1,17 +1,12 @@
package server
import (
"bytes"
"errors"
"fmt"
"io"
"local/router"
"local/rssmon3/config"
"local/storage"
"log"
"net/http"
"regexp"
"strings"
)
func (s *Server) Routes() error {
@@ -47,46 +42,11 @@ func (s *Server) error(w http.ResponseWriter, r *http.Request, err error) {
}
func (s *Server) tag(w http.ResponseWriter, r *http.Request) {
foo := s.notFound
switch r.Method {
case "GET":
foo = s.getFeedByTag
if r.Method != "GET" {
s.notFound(w, r)
return
}
foo(w, r)
}
func (s *Server) getFeedByTag(w http.ResponseWriter, r *http.Request) {
tag := regexp.MustCompile("^.*\\/").ReplaceAllString(r.URL.Path, "")
log.Println(tag)
s.error(w, r, errors.New("not impl"))
}
func (s *Server) auctions(w http.ResponseWriter, r *http.Request) {
foo := http.NotFound
r.ParseForm()
switch strings.ToLower(r.Method) {
case "get":
foo = s.get
}
foo(w, r)
}
func (s *Server) get(w http.ResponseWriter, r *http.Request) {
db := config.Values().DB
if len(strings.Split(r.URL.Path, "/")) < 3 {
s.notFound(w, r)
return
}
key := strings.Split(r.URL.Path, "/")[2]
log.Println("GET", key)
value, err := db.Get(key)
if err == storage.ErrNotFound {
s.notFound(w, r)
return
}
if err != nil {
s.error(w, r, err)
return
}
io.Copy(w, bytes.NewBuffer(value))
}