Created
May 16, 2014 21:40
-
-
Save naquad/0435d855d8131f31b1c9 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
package main | |
import ( | |
"net/http" | |
"net/url" | |
"encoding/json" | |
"fmt" | |
"os" | |
"log" | |
) | |
type Runtime struct { | |
ConfigPath string | |
Config struct { | |
Listen []string | |
LogFile string | |
Path string | |
EndPoint string | |
} | |
Log *log.Logger | |
HTTP *[]http.Server | |
LogFile *os.File | |
} | |
const ( | |
ResponseOk = iota | |
ResponseInternalError = iota | |
ResponseInvalid = iota | |
ResponseBlocked = iota | |
ResponseBilling = iota | |
) | |
type Response struct { | |
Status int | |
URL string | |
Error string | |
Ping int | |
} | |
func (r *Runtime) readConfig() (err error) { | |
file, err := os.Open(r.ConfigPath) | |
if err != nil { | |
err = fmt.Errorf("Failed to read \"%s\": %s", r.ConfigPath, err.Error()) | |
return | |
} | |
defer file.Close() | |
decoder := json.NewDecoder(file) | |
if err = decoder.Decode(r.Config); err != nil { | |
err = fmt.Errorf("Failed to parse \"%s\": %s", r.ConfigPath, err.Error()) | |
} | |
return | |
} | |
func (r *Runtime) prepare() (err error) { | |
r.LogFile, err = os.OpenFile(r.Config.LogFile, os.O_WRONLY | os.O_APPEND | os.O_CREATE, 0600) | |
if err != nil { return } | |
r.Log = log.New(r.LogFile, "StreamingProxy", log.LstdFlags) | |
return | |
} | |
func NewRuntime(path string) (ret *Runtime, err error) { | |
ret = &Runtime{ConfigPath: path} | |
if err = ret.readConfig(); err != nil { | |
ret = nil | |
} else { | |
err = ret.prepare() | |
} | |
return | |
} | |
func (r *Runtime) makeRequest(action string, ip string, url string, end_point string) (ret *Response) { | |
form := url.Values{} | |
form.Add("action", action) | |
form.Add("ip", ip) | |
form.Add("url", url) | |
form.Add("end_point", end_point) | |
resp, err := http.PostForm(r.Config.EndPoint, form) | |
if err != nil { | |
ret = &Response { | |
Status: ResponseInternalError, | |
Error: "Internal server error", | |
} | |
} | |
return | |
} | |
func (r *Runtime) Connected(ip string, url string, end_point string) *Response { | |
return r.makeRequest("connected", ip, url, end_point) | |
} | |
func (r *Runtime) Disconnected(ip string, url string, end_point string) *Response { | |
return r.makeRequest("disconnected", ip, url, end_point) | |
} | |
func (r *Runtime) KeepAlive(ip string, url string, end_point string) *Response { | |
return r.makeRequest("ping", ip, url, end_point) | |
} | |
func (r *Runtime) Close() { | |
if r.LogFile != nil { | |
r.LogFile.Close() | |
} | |
} | |
func main() { | |
runtime, err := NewRuntime("./config.json") | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
fmt.Println(runtime.Config.EndPoint) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment