A simple page for all your links.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

87 lines
1.5 KiB

  1. package publicbio
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gorilla/mux"
  6. "github.com/gorilla/schema"
  7. "github.com/writeas/web-core/converter"
  8. "io/ioutil"
  9. "log"
  10. "net/http"
  11. "os"
  12. )
  13. const (
  14. serverSoftware = "Public Bio"
  15. softwareURL = "https://publicb.io"
  16. )
  17. var (
  18. // Software version can be set from git env using -ldflags
  19. softwareVer = "0.1.0"
  20. )
  21. type App struct {
  22. router *mux.Router
  23. cfg *Config
  24. singleUser *Profile
  25. }
  26. func (app *App) multiUser() bool {
  27. return app.singleUser == nil
  28. }
  29. type Config struct {
  30. Host string
  31. Port int
  32. static bool
  33. UserFile string
  34. }
  35. func Serve(cfg *Config) {
  36. app := &App{
  37. cfg: cfg,
  38. }
  39. if cfg.UserFile != "" {
  40. f, err := ioutil.ReadFile(cfg.UserFile)
  41. if err != nil {
  42. log.Fatal("File error: %v\n", err)
  43. }
  44. err = json.Unmarshal(f, &app.singleUser)
  45. if err != nil {
  46. log.Fatalf("Unable to read user config: %v", err)
  47. }
  48. fmt.Printf("Results: %v\n", app.singleUser)
  49. } else {
  50. log.Fatal("No user configuration")
  51. }
  52. if app.cfg.static {
  53. if err := renderTemplate(os.Stdout, "profile", app.singleUser); err != nil {
  54. log.Fatal(err)
  55. }
  56. return
  57. }
  58. initRoutes(app)
  59. http.Handle("/", app.router)
  60. log.Printf("Serving on http://localhost:%d", app.cfg.Port)
  61. http.ListenAndServe(fmt.Sprintf(":%d", app.cfg.Port), nil)
  62. }
  63. func initConverter() {
  64. formDecoder := schema.NewDecoder()
  65. formDecoder.RegisterConverter(converter.NullJSONString{}, converter.JSONNullString)
  66. }
  67. // FormatVersion constructs the version string for the application
  68. func FormatVersion() string {
  69. return serverSoftware + " " + softwareVer
  70. }