Skip to content

Instantly share code, notes, and snippets.

@e7
e7 / DemoXMLConf.java
Last active December 12, 2017 10:24
apache commons configuration2 read config from both local and network
XMLConfiguration cnf;
{ // remote
cnf = new BasicConfigurationBuilder<>(XMLConfiguration.class)
.configure(new Parameters().xml()).getConfiguration();
InputStream is = new ByteArrayInputStream(
"xxxxxxxxxxxx".getBytes()
);
new FileHandler(cnf).load(is);
}
@e7
e7 / example_regex.txt
Created December 12, 2017 10:31
常用正则
https://jex.im/regulex/
# 合法URL
(https?)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]
@e7
e7 / example_apache_pool2.java
Created December 26, 2017 09:22
apache common pool2用法
// 首先实现PooledObjectFactory接口
public interface PooledObjectFactory<T> {
PooledObject<T> makeObject() throws Exception;
void destroyObject(PooledObject<T> var1) throws Exception;
boolean validateObject(PooledObject<T> var1);
void activateObject(PooledObject<T> var1) throws Exception;
@e7
e7 / golang-orm.go
Created January 4, 2018 07:18
golang的orm示例(mysql)
applist := make([]appinfo, 0)
for rows.Next() {
elmts := reflect.ValueOf(&appinfo{}).Elem()
onerow := make([]interface{}, elmts.NumField())
for i := 0; i < elmts.NumField(); i++ {
onerow[i] = elmts.Field(i).Addr().Interface()
}
rows.Scan(onerow...)
applist = append(applist, elmts.Interface().(appinfo))
}
@e7
e7 / body-parser.go
Created January 8, 2018 09:33
negroni的middleware示例
type bodyParser struct{
unused int;
}
func NewBodyParser() (*bodyParser){
return &bodyParser{}
}
func (m *bodyParser) ServeHTTP(rsp http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
var err error
@e7
e7 / gohfs.go
Created January 9, 2018 10:16
简单的http文件服务器
package main
import (
"net/http"
"github.com/urfave/negroni"
)
func main() {
mux := http.NewServeMux()
@e7
e7 / scoped_thread.cpp
Last active January 11, 2018 14:32
raii确保c++11线程被join
class scoped_thread
{
std::thread t;
public:
explicit scoped_thread(std::thread t_) : t(std::move(t_)) {
if (!t.joinable()) {
throw std::logic_error(“No thread”);
}
}
~scoped_thread() {
@e7
e7 / capture.conf
Last active January 12, 2018 10:09
capture location for nginx
location /capture/ {
internal;
rewrite ^/capture/(https?)/([^/]+)/(\d+)/(.*) /$4 break;
proxy_pass $1://$2:$3;
}
usage like this:
-- url: http://www.hejizhan.com/s/forum.php
ngx.location.capture("/capture/http/www.hejizhan.com/80/s/forum.php"
@e7
e7 / qiniu-token.go
Last active March 8, 2018 02:26
七牛云上传示例
package token
import (
"io/ioutil"
"encoding/json"
"net/http"
"github.com/sirupsen/logrus"
"github.com/qiniu/api.v7/storage"
"fmt"
@e7
e7 / check_type.hpp
Last active May 3, 2018 08:10
check type
#ifndef CHECK_TYPE_HPP___
#define CHECK_TYPE_HPP___
#include <typeinfo> // typeid
#include <sstream> // std::ostringstream, std::string
#include <type_traits> // std::is_array
#include <utility> // std::move
#if defined(__GNUC__)
#include <memory> // std::unique_ptr
#include <cxxabi.h> // abi::__cxa_demangle