A simple page for all your links.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

78 řádky
1.6 KiB

  1. package publicbio
  2. import (
  3. "encoding/json"
  4. "flag"
  5. "fmt"
  6. "github.com/gorilla/mux"
  7. "github.com/gorilla/schema"
  8. "github.com/writeas/web-core/converter"
  9. "io/ioutil"
  10. "log"
  11. "net/http"
  12. "os"
  13. )
  14. type app struct {
  15. router *mux.Router
  16. cfg *config
  17. singleUser *Profile
  18. }
  19. func (app *app) multiUser() bool {
  20. return app.singleUser == nil
  21. }
  22. type config struct {
  23. host string
  24. port int
  25. static bool
  26. }
  27. func Serve() {
  28. app := &app{
  29. cfg: &config{},
  30. }
  31. flag.IntVar(&app.cfg.port, "p", 8080, "Port to start server on")
  32. flag.StringVar(&app.cfg.host, "h", "https://public.bio", "Site's base URL")
  33. var userFile string
  34. flag.StringVar(&userFile, "u", "", "Configuration file for single-user site")
  35. flag.BoolVar(&app.cfg.static, "s", false, "Generate static page instead of serving the site")
  36. flag.Parse()
  37. if userFile != "" {
  38. f, err := ioutil.ReadFile(userFile)
  39. if err != nil {
  40. log.Fatal("File error: %v\n", err)
  41. }
  42. err = json.Unmarshal(f, &app.singleUser)
  43. if err != nil {
  44. log.Fatalf("Unable to read user config: %v", err)
  45. }
  46. fmt.Printf("Results: %v\n", app.singleUser)
  47. } else {
  48. log.Fatal("No user configuration")
  49. }
  50. if app.cfg.static {
  51. if err := renderTemplate(os.Stdout, "profile", app.singleUser); err != nil {
  52. log.Fatal(err)
  53. }
  54. return
  55. }
  56. initRoutes(app)
  57. http.Handle("/", app.router)
  58. log.Printf("Serving on localhost:%d", app.cfg.port)
  59. http.ListenAndServe(fmt.Sprintf(":%d", app.cfg.port), nil)
  60. }
  61. func initConverter() {
  62. formDecoder := schema.NewDecoder()
  63. formDecoder.RegisterConverter(converter.NullJSONString{}, converter.JSONNullString)
  64. }