Last active
December 29, 2015 05:29
-
-
Save okaq/7622112 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// okaq.com | |
// fugi web server | |
// by [email protected] | |
// on 2013-11-25 | |
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"net/url" | |
) | |
const ( | |
) | |
var ( | |
// list of urls in web app | |
Apps = [3]string{"/","/speed","/war"} | |
Paths = [3]string{"fugi.html", "speed.html", "war.html"} | |
// list of Flight middleware funcs | |
// or func generator based on key? | |
// map url key to cache object instance | |
AppsMap map[string]*Cache | |
// list of header mime types | |
MimeHeaders = [...]string{ | |
"text/plain", | |
"text/html", | |
} | |
) | |
type Cache struct { | |
Data []byte | |
URL *url.URL | |
Path string | |
HeaderIndex int | |
} | |
func NewCache() *Cache { | |
c0 := new(Cache) | |
return c0 | |
} | |
func (c *Cache) ParseURL(s string) { | |
u0, e := url.Parse(s) | |
if e != nil { | |
fmt.Println(e) | |
} | |
c.URL = u0 | |
} | |
func (c *Cache) Init() { | |
b0, e := ioutil.ReadFile(c.Path) | |
if e != nil { | |
fmt.Println(e) | |
} | |
c.Data = make([]byte, len(b0)) | |
copy(c.Data,b0) | |
} | |
func MakeAppsMap() { | |
AppsMap = make(map[string]*Cache) | |
for i,_ := range Apps { | |
c0 := NewCache() | |
c0.Path = Paths[i] | |
c0.ParseURL(Apps[i]) | |
c0.HeaderIndex = 1 | |
c0.Init() | |
AppsMap[Apps[i]] = c0 | |
} | |
} | |
func MakeHandle(s string) http.HandlerFunc { | |
return func(w http.ResponseWriter, r *http.Request) { | |
// call cache.flight() before write? | |
w.Header().Set("Content-Type", "text/html") | |
// test r.URL.Path trailing slash | |
c, ok := AppsMap[r.URL.Path] | |
if ok && c != nil{ | |
/* | |
var s0 = r.URL.Path | |
var s1 = Apps[2] | |
var s4 = len(c.Data) | |
var s3 = fmt.Sprintf("%x , %x, %d", s0, s1, s4) | |
*/ | |
w.Write(c.Data) | |
} else { | |
w.Write([]byte(r.URL.Path)) | |
} | |
} | |
} | |
func MuxItUp() { | |
for i,_ := range Apps { | |
http.HandleFunc(Apps[i], MakeHandle(Apps[i])) | |
} | |
} | |
func StartServer() { | |
return | |
} | |
func main() { | |
// construct cache(appsmap) | |
MakeAppsMap() | |
// mux handlers | |
MuxItUp() | |
// start server | |
StartServer() | |
} | |
func init() { | |
main() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment