예제 #1
0
파일: fileop.go 프로젝트: cxwshawn/ornet
func (fo *FileOp) Init(config interface{}) (err error) {
	dlog.Printf("FileOp init...\n")
	fo.FileOpConfig = config.(*FileOpConfig)
	fo.opHandlers = make(map[string]HandlerFunc, 0)
	fo.opReqPool = make(chan *FileRequest, fo.RequestPoolSize)
	for i := 0; i < fo.RequestPoolSize; i++ {
		req := &FileRequest{}
		fo.opReqPool <- req
	}
	fo.client = redis.NewTCPClient(&redis.Options{Addr: fo.RedisAddr,
		DialTimeout: time.Millisecond * 100})
	val, err := fo.client.Ping().Result()
	if err != nil {
		dlog.EPrintln(err.Error())
		return err
	}
	if val != "PONG" {
		dlog.EPrintln("redis returned ping result invalid!")
		return errors.New("redis returned ping result invalid!")
	}

	fo.register("upload", uploadHandler)
	fo.register("download", downloadHandler)
	fo.register("ping", pingHandler)

	dlog.Printf("FileOp init ok, config:(%+v)\n", fo.FileOpConfig)
	return nil
}
예제 #2
0
func (op *Url2NameOp) Init(config interface{}) (err error) {
	dlog.Printf("Url2NameOp init...\n")
	op.Url2NameOpConfig = config.(*Url2NameOpConfig)
	op.opHandlers = make(map[string]HandlerFunc, 0)
	op.opReqPool = make(chan *Url2NameRequest, op.RequestPoolSize)
	for i := 0; i < op.RequestPoolSize; i++ {
		req := &Url2NameRequest{}
		op.opReqPool <- req
	}
	op.entryPool = make(chan *Url2NameEntry, op.EntryPoolSize)
	for i := 0; i < op.RedisWriteCount; i++ {
		go writeToRedis(op)
	}
	err = os.MkdirAll(op.DownPath, 0755)
	if err != nil {
		dlog.EPrintln(err.Error())
		return err
	}
	err = os.MkdirAll(op.StorePath, 0755)
	if err != nil {
		dlog.EPrintln(err.Error())
		return err
	}

	op.register("add", addHandler)
	op.register("rm", removeHandler)
	op.register("help", helpHandler)
	// http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = 100

	dlog.Printf("Url2NameOp init ok, config:(%+v)\n", op.Url2NameOpConfig)
	return nil
}
예제 #3
0
//line /root/myopensrc/ornet/anyd/src/service/sandbox/sandbox.go:93
func (op *SandboxOp) Init(config interface{}) (err error) {
	dlog.Printf("SandboxOp init...\n")
	op.SandboxOpConfig = config.(*SandboxOpConfig)
	op.opReqPool = make(chan *SandBoxRequest, op.RequestPoolSize)
	for i := 0; i < op.RequestPoolSize; i++ {
		req := &SandBoxRequest{}
		op.opReqPool <- req
	}
//line /root/myopensrc/ornet/anyd/src/service/sandbox/sandbox.go:104

//line /root/myopensrc/ornet/anyd/src/service/sandbox/sandbox.go:103
	dlog.Printf("SandboxOp init ok, config:(%+v)\n", op.SandboxOpConfig)
	return nil
}
예제 #4
0
파일: fileop.go 프로젝트: cxwshawn/ornet
func (fo *FileOp) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	dlog.Printf("(%+v)\n", *req)
	pos := strings.LastIndex(req.URL.Path, "/")
	if pos == -1 {
		dlog.EPrintf("invalid request :%s\n", req.RequestURI)
		http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
		return
	}
	action := req.URL.Path[pos+1:]
	foReq := fo.getAvalibleReq()
	foReq.out = w
	foReq.req = req
	foReq.op = fo
	defer func() {
		fo.recycle(foReq)
		req.Body.Close()
	}()

	if handler, ok := fo.opHandlers[action]; ok {
		handler(foReq)
	} else {
		dlog.EPrintln("bad request!")
		http.NotFound(w, req)
	}
}
예제 #5
0
파일: sandbox.go 프로젝트: cxwshawn/ornet
func (op *SandboxOp) Init(config interface{}) (err error) {
	dlog.Printf("SandboxOp init...\n")
	op.SandboxOpConfig = config.(*SandboxOpConfig)
	op.opReqPool = make(chan *SandBoxRequest, op.RequestPoolSize)
	for i := 0; i < op.RequestPoolSize; i++ {
		req := &SandBoxRequest{}
		op.opReqPool <- req
	}
	// http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = 100
	op.luaCtx = C.init_lua()
	luaFilename := C.CString(op.LuaFilename)
	defer C.free(unsafe.Pointer(luaFilename))
	_, err = C.load_lua_file(op.luaCtx, luaFilename)
	if err != nil {
		dlog.EPrintln(err.Error())
		return err
	}
	dlog.Printf("SandboxOp init ok, config:(%+v)\n", op.SandboxOpConfig)
	return nil
}
예제 #6
0
파일: fileop.go 프로젝트: cxwshawn/ornet
func uploadHandler(foReq *FileRequest) {
	if foReq.op.UploadType == ngx_fastcgi {
		fastcgiUploadHandler(foReq)
	} else if foReq.op.UploadType == upload_direct {
		directUploadHandler(foReq)
	} else {
		dlog.Printf("invalid upload type!\n")
		http.Error(foReq.out, http.StatusText(http.StatusInternalServerError),
			http.StatusInternalServerError)
	}
}
예제 #7
0
파일: cmds.go 프로젝트: cxwshawn/ornet
func SafeHandler(handler http.Handler) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		dlog.Printf("%(+v)\n", req)
		defer func() {
			if err, ok := recover().(error); ok {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				dlog.EPrintln("WARN: panic in %v - %v", handler, err)
				dlog.EPrintln(string(debug.Stack()))
			}
		}()
		handler.ServeHTTP(w, req)
	}
}
예제 #8
0
//line /root/myopensrc/ornet/anyd/src/service/sandbox/sandbox.go:64
func (op *SandboxOp) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	dlog.Printf("(%+v)\n", *req)
	pos := strings.LastIndex(req.URL.Path, "/")
	if pos == -1 {
		dlog.EPrintf("invalid request :%s\n", req.RequestURI)
		http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
		return
	}
	action := req.URL.Path[pos+1:]
	unReq := op.getAvalibleReq()
	unReq.out = w
	unReq.req = req
	unReq.op = op
	defer func() {
		op.recycle(unReq)
		req.Body.Close()
	}()
//line /root/myopensrc/ornet/anyd/src/service/sandbox/sandbox.go:88

//line /root/myopensrc/ornet/anyd/src/service/sandbox/sandbox.go:87
}
예제 #9
0
파일: sandbox.go 프로젝트: cxwshawn/ornet
func (op *SandboxOp) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	dlog.Printf("(%+v)\n", *req)
	pos := strings.LastIndex(req.URL.Path, "/")
	if pos == -1 {
		dlog.EPrintf("invalid request :%s\n", req.RequestURI)
		http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
		return
	}
	// action := req.URL.Path[pos+1:]
	unReq := op.getAvalibleReq()
	unReq.w = w
	unReq.req = req
	unReq.op = op
	defer func() {
		op.recycle(unReq)
		req.Body.Close()
	}()
	_, err := C.process_request(op.luaCtx, unsafe.Pointer(unReq))
	if err != nil {
		dlog.EPrintln(err.Error())
		return
	}
}
예제 #10
0
파일: qianke.go 프로젝트: cxwshawn/ornet
func (op *qiankeOp) Init(config interface{}) (err error) {
	dlog.Println("qiankeOp init...")
	op.qiankeOpConfig = config.(*qiankeOpConfig)
	op.dbOp = new(QkDBOp)
	err = op.dbOp.Init(op.MysqlHost, op.MysqlUsername, op.MysqlPasswd, op.MysqlDbName)
	if err != nil {
		dlog.EPrintln(err.Error())
		return err
	}

	op.opReqPool = make(chan *qiankeRequest, op.RequestPoolSize)
	op.opHandlers = make(map[string]HandlerFunc, 0)
	for i := 0; i < op.RequestPoolSize; i++ {
		req := &qiankeRequest{}
		op.opReqPool <- req
	}

	op.register("login", loginHandler)
	op.register("register", registerHandler)

	dlog.Printf("qiankeOp init ok, config:%+v\n", op.qiankeOpConfig)
	return nil
}
예제 #11
0
func (op *Url2NameOp) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	dlog.Printf("(%+v)\n", *req)
	pos := strings.LastIndex(req.URL.Path, "/")
	if pos == -1 {
		dlog.EPrintf("invalid request :%s\n", req.RequestURI)
		http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
		return
	}
	action := req.URL.Path[pos+1:]
	unReq := op.getAvalibleReq()
	unReq.out = w
	unReq.req = req
	unReq.op = op
	defer func() {
		op.recycle(unReq)
		req.Body.Close()
	}()

	if handler, ok := op.opHandlers[action]; ok {
		handler(unReq)
	} else {
		http.NotFound(w, req)
	}
}
예제 #12
0
파일: ngxfm.go 프로젝트: cxwshawn/ornet
func main() {
	if configFileName == nil {
		fmt.Println("without specifiy config file.")
		os.Exit(1)
	}
	config, md, err := LoadFmdConfig(*configFileName)
	if err != nil {
		log.Fatal(err.Error())
	}
	fmConf = &FmdConfig{BasicSvrConfig: BasicSvrConfig{ErrorLog: true, AccessLog: false},
		FastcgiListenAddr: ":11000", HttpListenAddr: ":11001"}
	err = analyzeFmdConf(config, md)
	if err != nil {
		log.Fatal(err.Error())
	}

	err = dlog.InitLog("ngxfmd", fmConf.ErrorLog, fmConf.AccessLog)
	if err != nil {
		log.Fatal(err.Error())
	}

	err = cmds.InitHandlerConf(config, md)
	if err != nil {
		log.Fatal(err.Error())
	}
	cmdServer := &http.Server{Addr: fmConf.HttpListenAddr, Handler: cmds.CmdServerMux}
	err = cmdServer.ListenAndServe()
	if err != nil {
		fmt.Printf("%s", err.Error())
	}
	// var wg sync.WaitGroup
	// wg.Add(1)
	go func() {
		cmdServer := &http.Server{Addr: fmConf.HttpListenAddr, Handler: cmds.CmdServerMux}
		err = cmdServer.ListenAndServe()
		if err != nil {
			fmt.Printf("%s", err.Error())
		}
		// wg.Done()
	}()
	// wg.Add(1)
	go func() {
		ln, err := net.Listen("tcp", fmConf.FastcgiListenAddr)
		if err != nil {
			log.Fatal(err.Error())
		}

		err = fcgi.Serve(ln, cmds.CmdServerMux)
		if err != nil {
			log.Fatal(err.Error())
		}
		// wg.Done()
	}()
	// wg.Wait()
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, os.Kill)

	// Block until a signal is received.
	s := <-c
	cmds.Uninit()
	dlog.Printf("Accept %s signal, quit server...\n", s.String())
}
예제 #13
0
파일: fileop.go 프로젝트: cxwshawn/ornet
func downloadHandler(foReq *FileRequest) {
	dlog.Println("downloadHandler in")

	w := foReq.out
	req := foReq.req
	fo := foReq.op
	values, err := url.ParseQuery(req.URL.RawQuery)
	if err != nil {
		dlog.EPrintln(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if reqUrl, ok := values["link"]; ok {
		if reqUrl[0] == "" {
			dlog.EPrintf("invalid link parameter,from remote %s, url:%s\n",
				req.RemoteAddr, req.URL.Path)
			http.Error(w, "invalid link parameter", http.StatusBadRequest)
			return
		}
		// realUrl, err := url.QueryUnescape(reqUrl[0])
		// if err != nil {
		// 	dlog.EPrintln(err.Error())
		// 	http.Error(w, err.Error(), http.StatusInternalServerError)
		// 	return
		// }
		hmResponse := fo.client.HMGet(Url2NameKey, reqUrl[0])
		result, err := hmResponse.Result()
		if err != nil {
			dlog.EPrintln(err.Error())
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		if len(result) == 0 || result[0] == nil {
			dlog.EPrintf("url %s not found!\n", reqUrl[0])
			http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
			return
		}
		//just update url visit time.
		fo.client.ZAdd(UrlTimeKey, redis.Z{Score: float64(time.Now().Unix()), Member: reqUrl[0]})
		if len(result) == 0 || result[0] == nil {
			dlog.EPrintf("url2name failed, url:%s\n", reqUrl[0])
			http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
			return
		}

		filename := result[0].(string)
		// fmt.Println(filename)
		urlRes, err := url.Parse(reqUrl[0])
		if err != nil {
			dlog.EPrintln(err.Error())
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		realFilename := ""
		if path.Ext(filename) == "" {
			realFilename = fmt.Sprintf("%s%s", filename, path.Ext(urlRes.Path))
		} else {
			realFilename = filename
		}
		w.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", realFilename))
		w.Header().Add("Content-Type", "application/octet-stream")
		fullFilePath := path.Join(foReq.op.StorePath, filename)
		if err = exists(fullFilePath); err != nil {
			fullFilePath = path.Join(foReq.op.StorePath, realFilename)
			if err = exists(fullFilePath); err != nil {
				dlog.EPrintf("file %s not found\n", fullFilePath)
				http.NotFound(w, req)
				return
			}
		}
		dlog.Printf("download file %s\n", fullFilePath)
		http.ServeFile(w, req, fullFilePath)
		//todo:
	} else {
		dlog.EPrintf("request bad parameter\n")
		http.Error(w, "bad parameter", http.StatusBadRequest)
	}
}
예제 #14
0
파일: fileop.go 프로젝트: cxwshawn/ornet
func fastcgiUploadHandler(foReq *FileRequest) {
	req := foReq.req
	w := foReq.out
	// contentType, ret := req.Header["Content-Type"]
	// if ret == false {
	// 	dlog.EPrintf()
	// 	http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
	// 	return
	// }

	// mediaType, params, err := mime.ParseMediaType(contentType[0])
	// if err != nil {
	// 	http.Error(w, err.Error(), http.StatusInternalServerError)
	// 	return
	// }
	// fullFilePath := ""
	// if strings.HasPrefix(mediaType, "multipart/") {
	// reader, err := req.MultipartReader()
	// if err != nil {
	// 	dlog.EPrintln(err.Error())
	// 	http.Error(w, err.Error(), http.StatusBadRequest)
	// 	return
	// }
	// // mr := multipart.NewReader(req.Body, params["boundary"])
	// mf, err := reader.ReadForm(0)
	// if err != nil {
	// 	dlog.EPrintln(err.Error())
	// 	http.Error(w, err.Error(), http.StatusInternalServerError)
	// 	return
	// }
	values, err := url.ParseQuery(req.URL.RawQuery)
	if err != nil {
		dlog.EPrintln(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	filename, ok := values["filename"]
	if !ok {
		dlog.EPrintf("request without specify filename paramter!\n")
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	err = req.ParseMultipartForm(32 * 1024)
	if err != nil {
		dlog.EPrintln(err.Error())
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	var infoList []UploadFileInfo
	dlog.Printf("(%+v)\n", req.Form)
	for k, v := range req.Form {
		if infoList == nil {
			infoList = make([]UploadFileInfo, len(v))
		}
		for i := 0; i < len(v); i++ {
			infoList[i].setInfo(k, v[i])
		}
	}
	dlog.Printf("(%+v)\n", infoList)
	// fmt.Println(infoList)
	for _, fi := range infoList {
		fullFilePath := path.Join(foReq.op.StorePath, filename[0])
		err = os.Rename(fi.UploadPath, fullFilePath)
		dlog.Printf("rename file %s to file %s\n", fi.UploadPath, fullFilePath)
		if err != nil {
			dlog.EPrintf(err.Error())
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}
	// }
	w.WriteHeader(http.StatusOK)
}