Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

80 lignes
1.8KB

  1. package main
  2. import (
  3. "biukop.com/sfm/loan"
  4. "encoding/gob"
  5. "encoding/json"
  6. "fmt"
  7. log "github.com/sirupsen/logrus"
  8. "net/http"
  9. )
  10. type httpEntry func(http.ResponseWriter, *http.Request)
  11. var httpEntryMap = map[string]httpEntry{
  12. apiV1Prefix: apiV1Main,
  13. "/dummy/": dummyHandler,
  14. }
  15. func main() {
  16. err := config.readConfig() //wechat API config
  17. if err != nil {
  18. log.Println(err)
  19. log.Fatalf("unable to read %s, program quit\n", configFile)
  20. return
  21. }
  22. setupRootFileServer()
  23. //always the last one
  24. setupHTTPHandler()
  25. }
  26. func init() {
  27. gob.Register(loginForm{})
  28. customFormatter := new(log.TextFormatter)
  29. customFormatter.TimestampFormat = "2006-01-02 15:04:05"
  30. log.SetFormatter(customFormatter)
  31. log.Info("Change Log Format before FullTimestamp=true")
  32. customFormatter.FullTimestamp = true
  33. }
  34. func setupRootFileServer() {
  35. //root of doc
  36. for idx, node := range config.Static {
  37. log.Printf("setting up static %d with %+v\n", idx, node)
  38. fs := http.FileServer(http.Dir(node.Dir))
  39. http.Handle(node.StaticUrl, http.StripPrefix(node.StripPrefix, fs))
  40. }
  41. }
  42. func setupHTTPHandler() {
  43. for key, val := range httpEntryMap {
  44. http.HandleFunc(key, val)
  45. }
  46. log.Printf("Server started at %s:%s\n", config.Host, config.Port)
  47. //log.Fatal(http.ListenAndServe(config.Host+":"+config.Port, nil))
  48. log.Fatal(http.ListenAndServeTLS(config.Host+":"+config.Port, config.TlsCert, config.TlsKey, nil))
  49. }
  50. func dummyHandler(w http.ResponseWriter, r *http.Request) {
  51. p := loan.People{}
  52. p.FakeNew()
  53. fmt.Fprintf(w, "Hello, there %s, %+v\n", loan.Version, p)
  54. }
  55. func apiSendJson(p interface{}, w http.ResponseWriter) {
  56. b, e := json.Marshal(p)
  57. w.Header().Set("Content-Type", "text/json; charset=utf-8")
  58. if e == nil {
  59. fmt.Fprint(w, string(b))
  60. } else {
  61. apiV1DumpRequest(w, nil, nil)
  62. }
  63. }