Skip to content
Permalink
Browse files

WIP

  • Loading branch information...
violetyk committed Feb 12, 2015
1 parent c73e48d commit df09bbbc886df3397f38e08ff1ddd0163bf7e24a
Showing with 98 additions and 25 deletions.
  1. +2 −0 .gitignore
  2. +8 −19 handler/image_handler.go
  3. +5 −6 { → handler}/worker.go
  4. +83 −0 query.go
@@ -24,3 +24,5 @@ _testmain.go
*.prof

_vendor

sandbox.go
@@ -1,47 +1,36 @@
package handler

import (
"log"
"net/http"
"runtime"

"github.com/violetyk/graid/config"
)

type job struct {
response http.ResponseWriter
request *http.Request
}

type jobs chan *job
type WorkerPool chan *Worker

type ImageHandler struct {
jobs jobs
workerPool WorkerPool
}

func NewImageHandler() *ImageHandler {

config := config.Load()

jobs := make(jobs, config.Server.WorkerPoolSize)
workerPool := make(WorkerPool, config.Server.WorkerPoolSize)
for i := 1; i <= config.Server.WorkerPoolSize; i++ {
go worker(i, jobs)
workerPool <- NewWorker(i)
}

return &ImageHandler{
jobs: jobs,
workerPool: workerPool,
}
}

func (handler *ImageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/favicon.ico" {
return
}
handler.jobs <- &job{response: w, request: r}
}

func worker(id int, jobs jobs) {
for job := range jobs {
log.Println("worker", id, "GR", runtime.NumGoroutine(), "processing job", job.request.URL.String())
}
worker := <-handler.workerPool
worker.Execute(w, r)
handler.workerPool <- worker
}
@@ -1,12 +1,10 @@
package main
package handler

import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"runtime"

"github.com/violetyk/graid/config"
)
@@ -22,14 +20,15 @@ func NewWorker(id int) *Worker {
}

func (worker *Worker) Execute(w http.ResponseWriter, r *http.Request) {

// http://localhost:8080/xx/yy/zz/hogehoge.png:e?hoge=fuga&k=v#f
// http://localhost:8080/hogehoge.png
// http://localhost:8080/path/to/hogehoge.png:w50:w100
// http://localhost:8080/path/to/hogehoge.png:c100,200,10,50
// http://localhost:8080/example.com/hogehoge.png:e
// http://localhost:8080/example.com/hogehoge.png:e:c100,200,10,50
// http://localhost:8080/http://example.com/hogehoge.png
// http://localhost:8080/example.com/hogehoge.png:c100,200,10,50

log.Println(runtime.NumGoroutine())
// log.Println(runtime.NumGoroutine())
u, err := url.Parse(r.URL.String())
if err != nil {
panic(err)
@@ -0,0 +1,83 @@
package main

import (
"net/url"
"regexp"

"github.com/k0kubun/pp"
)

type Query struct {
Raw string
SourceUrl string
IsExternal bool
ResizeOrder resizeOrder
CropOrder cropOrder
}

type resizeOrder struct {
width int
height int
}

type cropOrder struct {
width int
height int
x int
y int
}

func NewQuery() *Query {
return &Query{}
}

func (query *Query) Clear() {
}

func (query *Query) Parse(urlString string) bool {
query.Clear()

query.Raw = urlString

u, err := url.Parse(query.Raw)
if err != nil {
return false
}

// pattern := regexp.MustCompile("^/(http|https)://")
// match := pattern.MatchString(u.Path)
// pattern := regexp.MustCompile(`^/(?P<protocol>http|https)://`)
match := regexp.MustCompile(`(?P<protocol>wef)`).FindStringSubmatch(u.Path)
match := regexp.MustCompile(`(?P<protocol>wef)`).FindStringSubmatch(u.Path)
pp.Print(len(match))

// s := strings.Split(u.Path, ":")

// if match {
// query.IsExternal = true
// query.SourceUrl = (s[0] + ":" + s[1])[1:]
// } else {
// query.IsExternal = false
// query.SourceUrl = config.Load().Origin.Url + s[0]
// }

// return true
}

func main() {

// var s string = "http://localhost:8080/http://hoge.com/path/to/hogehoge.png:w50:w100"
var s string = "http://localhost:8080/path/to/hogehoge.png:w50:w100"

// sum := md5.Sum(([]byte)s)

// query := NewQuery(s)
query := NewQuery()
ok := query.Parse(s)
pp.Print(ok)
if !ok {
pp.Print(query)
}

// pp.Print(len(s))
}

0 comments on commit df09bbb

Please sign in to comment.
You can’t perform that action at this time.