package main import ( "biukop.com/sfm/loan" "encoding/gob" "encoding/json" "fmt" log "github.com/sirupsen/logrus" "net/http" ) type httpEntry func(http.ResponseWriter, *http.Request) var httpEntryMap = map[string]httpEntry{ apiV1Prefix: apiV1Main, apiV1WebSocket: apiV1WebSocketHandler, "/dummy/": dummyHandler, } func main() { err := config.readConfig() //wechat API config if err != nil { log.Println(err) log.Fatalf("unable to read %s, program quit\n", configFile) return } loan.SetDSN(config.DSN) setupRootFileServer() //always the last one setupHTTPHandler() } func init() { gob.Register(loginForm{}) customFormatter := new(log.TextFormatter) customFormatter.TimestampFormat = "2006-01-02 15:04:05" log.SetFormatter(customFormatter) log.Info("Change Log Format before FullTimestamp=true") customFormatter.FullTimestamp = true } func setupRootFileServer() { //root of doc for idx, node := range config.Static { log.Printf("setting up static %d with %+v\n", idx, node) fs := http.FileServer(http.Dir(node.Dir)) http.Handle(node.StaticUrl, http.StripPrefix(node.StripPrefix, fs)) } } func setupHTTPHandler() { for key, val := range httpEntryMap { http.HandleFunc(key, val) } log.Printf("Server started at %s:%s\n", config.Host, config.Port) //log.Fatal(http.ListenAndServe(config.Host+":"+config.Port, nil)) log.Fatal(http.ListenAndServeTLS(config.Host+":"+config.Port, config.TlsCert, config.TlsKey, nil)) } func dummyHandler(w http.ResponseWriter, r *http.Request) { p := loan.People{} p.FakeNew() fmt.Fprintf(w, "Hello, there %s, %+v\n", loan.Version, p) } func apiSendJson(p interface{}, w http.ResponseWriter) { b, e := json.Marshal(p) w.Header().Set("Content-Type", "text/json; charset=utf-8") if e == nil { fmt.Fprint(w, string(b)) } else { apiV1DumpRequest(w, nil, nil) } }