123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package resource
- import (
- "net/http"
- )
- type Resource struct {
- Path string
- Ctype string
- Bytes []byte
- }
- type resource struct {
- maxurl int
- list map[string]Resource
- }
- func New() *resource {
- r := resource{maxurl: 0}
- r.list = map[string]Resource{}
- return &r
- }
- func (this *resource) Add(path string, ctype string, bytes []byte) {
-
- if _, ok := this.list[path]; ok == true {
- return
- }
-
- this.maxurl = len(path)
- this.list[path] = Resource{
- Path: path,
- Ctype: ctype,
- Bytes: bytes,
- }
- }
- func (this *resource) Response(w http.ResponseWriter, r *http.Request, before func(w http.ResponseWriter, r *http.Request, i *Resource), after func(w http.ResponseWriter, r *http.Request, i *Resource)) bool {
-
- if len(r.URL.Path) <= 1 || len(r.URL.Path)-1 > this.maxurl {
- return false
- }
-
- res, ok := this.list[r.URL.Path[1:]]
- if ok == false {
- return false
- }
-
- if before != nil {
- before(w, r, &res)
- }
-
- w.Header().Set("Content-Type", res.Ctype)
- w.Write(res.Bytes)
-
- if after != nil {
- after(w, r, &res)
- }
- return true
- }
|