mirror of
https://github.com/nkanaev/yarr.git
synced 2025-05-24 00:33:14 +00:00
basic server
This commit is contained in:
parent
241f99fc58
commit
29b860470b
5
main.go
5
main.go
@ -2,7 +2,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/nkanaev/yarr/storage"
|
"github.com/nkanaev/yarr/storage"
|
||||||
"github.com/nkanaev/yarr/worker"
|
//"github.com/nkanaev/yarr/worker"
|
||||||
|
"github.com/nkanaev/yarr/server"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -72,4 +73,6 @@ func main() {
|
|||||||
log.Print(worker.FindFeeds("https://horriblesubs.info/"))
|
log.Print(worker.FindFeeds("https://horriblesubs.info/"))
|
||||||
log.Print(worker.FindFeeds("http://daringfireball.net/"))
|
log.Print(worker.FindFeeds("http://daringfireball.net/"))
|
||||||
*/
|
*/
|
||||||
|
srv := server.New()
|
||||||
|
srv.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
23
server/handlers.go
Normal file
23
server/handlers.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
func Index(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
rw.Write([]byte("index"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Static(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
rw.Write([]byte("static:" + Vars(req)["path"]))
|
||||||
|
}
|
||||||
|
|
||||||
|
func FolderList(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func Folder(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func FeedList(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func Feed(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
}
|
73
server/server.go
Normal file
73
server/server.go
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"regexp"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Route struct {
|
||||||
|
url string
|
||||||
|
urlRegex *regexp.Regexp
|
||||||
|
handler func(http.ResponseWriter, *http.Request)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Handler struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func p(path string, handler func(http.ResponseWriter, *http.Request)) Route {
|
||||||
|
var urlRegexp string
|
||||||
|
urlRegexp = regexp.MustCompile(`[\*\:]\w+`).ReplaceAllStringFunc(path, func(m string) string {
|
||||||
|
if m[0:1] == `*` {
|
||||||
|
return "(?P<" + m[1:] + ">.+)"
|
||||||
|
}
|
||||||
|
return "(?P<" + m[1:] + ">[^/]+)"
|
||||||
|
})
|
||||||
|
urlRegexp = "^" + urlRegexp + "$"
|
||||||
|
return Route{
|
||||||
|
url: path,
|
||||||
|
urlRegex: regexp.MustCompile(urlRegexp),
|
||||||
|
handler: handler,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var routes []Route = []Route{
|
||||||
|
p("/", Index),
|
||||||
|
p("/static/*path", Static),
|
||||||
|
p("/api/folders", FolderList),
|
||||||
|
p("/api/folders/:id", Folder),
|
||||||
|
p("/api/feeds", FeedList),
|
||||||
|
p("/api/feeds/:id", Feed),
|
||||||
|
}
|
||||||
|
|
||||||
|
func Vars(req *http.Request) map[string]string {
|
||||||
|
if rv := req.Context().Value(0); rv != nil {
|
||||||
|
return rv.(map[string]string)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
for _, route := range routes {
|
||||||
|
if route.urlRegex.MatchString(req.URL.Path) {
|
||||||
|
if route.urlRegex.NumSubexp() > 0 {
|
||||||
|
vars := make(map[string]string)
|
||||||
|
matches := route.urlRegex.FindStringSubmatchIndex(req.URL.Path)
|
||||||
|
for i, key := range route.urlRegex.SubexpNames()[1:] {
|
||||||
|
vars[key] = req.URL.Path[matches[i*2+2]:matches[i*2+3]]
|
||||||
|
}
|
||||||
|
ctx := context.WithValue(req.Context(), 0, vars)
|
||||||
|
req = req.WithContext(ctx)
|
||||||
|
}
|
||||||
|
route.handler(rw, req)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rw.WriteHeader(http.StatusNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *http.Server {
|
||||||
|
h := Handler{}
|
||||||
|
s := &http.Server{Addr: "127.0.0.1:8000", Handler: h}
|
||||||
|
return s
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user