Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.08 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "net/http"
  6.     "time"
  7.     "strings"
  8.     "log"
  9.     "github.com/gorilla/mux"
  10.     "github.com/ttacon/chalk"
  11. )
  12.  
  13. type Server struct {
  14.     name string
  15.     id   int
  16. }
  17.  
  18. type User struct {
  19.     username string
  20.     userid   int
  21.     usercolor string
  22. }
  23.  
  24.  
  25. func HomeHandler(w http.ResponseWriter, r *http.Request) {
  26.    
  27.     fmt.Fprintf(w, "HomeHandler")
  28. }
  29. func RegisterHandler(w http.ResponseWriter, r *http.Request) {
  30.     // Registers a new user
  31.         fmt.Fprintf(w, "RegisterHandler")
  32.         fmt.Println(w, "RegisterHandler")
  33. }
  34. func ServersHandler(w http.ResponseWriter, r *http.Request) {
  35.    
  36.     fmt.Fprintf(w, "ServersHandler")
  37. }
  38. func CreateServersHandler(w http.ResponseWriter, r *http.Request) {
  39.    
  40.     fmt.Fprintf(w, "CreateServersHandler")
  41. }
  42. func ServerInstanceHandler(w http.ResponseWriter, r *http.Request) {
  43.    
  44.     fmt.Fprintf(w, "ServerInstanceHandler")
  45. }
  46. func DeleteServerInstanceHandler(w http.ResponseWriter, r *http.Request) {
  47.    
  48.     fmt.Fprintf(w, "DeleteServerInstanceHandler")
  49. }
  50.  
  51. const GETALL string = "GETALL"
  52. const GETONE string = "GETONE"
  53. const POST string   = "POST"
  54. const PUT string    = "PUT"
  55. const DELETE string = "DELETE"
  56.  
  57. func main() {
  58.  
  59.     r := mux.NewRouter()
  60.  
  61.     // Show some type of landing page
  62.     r.HandleFunc("/", HomeHandler).Methods("GET")
  63.  
  64.     // Register a new user account
  65.     r.HandleFunc("/register", RegisterHandler).Methods("GET")
  66.    
  67.     // Get a list of servers, maybe rank them
  68.     r.HandleFunc("/servers", ServersHandler).Methods("GET")
  69.    
  70.     // Create a new server
  71.     r.HandleFunc("/servers", CreateServersHandler).Methods("PUT")
  72.  
  73.     // Access a specific server
  74.     r.HandleFunc("/servers/{serverInstance}", ServerInstanceHandler).Methods("GET")
  75.  
  76.     // Delete a specific server. Do we want this, really?
  77.     r.HandleFunc("/servers/{serverInstance}/delete", DeleteServerInstanceHandler).Methods("DELETE")
  78.  
  79.     err := r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
  80.         pathTemplate, err := route.GetPathTemplate()
  81.         if err == nil {
  82.             fmt.Println(chalk.Green, "ROUTE:", pathTemplate)
  83.         }
  84.         pathRegexp, err := route.GetPathRegexp()
  85.         if err == nil {
  86.             fmt.Println(chalk.Cyan, "Path regexp:", pathRegexp)
  87.         }
  88.         queriesTemplates, err := route.GetQueriesTemplates()
  89.         if err == nil {
  90.             fmt.Println(chalk.Magenta, "Queries templates:", strings.Join(queriesTemplates, ","))
  91.         }
  92.         queriesRegexps, err := route.GetQueriesRegexp()
  93.         if err == nil {
  94.             fmt.Println(chalk.Blue, "Queries regexps:", strings.Join(queriesRegexps, ","))
  95.         }
  96.         methods, err := route.GetMethods()
  97.         if err == nil {
  98.             fmt.Println(chalk.White, "Methods:", strings.Join(methods, ","))
  99.         }
  100.         fmt.Println()
  101.         return nil
  102.     })
  103.  
  104.     if err != nil {
  105.         fmt.Println(err)
  106.     }
  107.  
  108.     http.Handle("/", r)
  109.  
  110.     srv := &http.Server{
  111.         Handler:      r,
  112.         Addr:         "127.0.0.1:8000",
  113.         WriteTimeout: 15 * time.Second,
  114.         ReadTimeout:  15 * time.Second,
  115.     }
  116.  
  117.     log.Fatal(srv.ListenAndServe())
  118.  
  119. }
  120.  
  121.  
  122. // func colors() {
  123.  
  124. //  // You can just use colors
  125. //  fmt.Println(chalk.Red, "Writing in colors", chalk.Cyan, "is so much fun", chalk.Reset)
  126. //  fmt.Println(chalk.Magenta.Color("You can use colors to color specific phrases"))
  127.  
  128. //  // You can just use text styles
  129. //  fmt.Println(chalk.Bold.TextStyle("We can have bold text"))
  130. //  fmt.Println(chalk.Underline.TextStyle("We can have underlined text"))
  131. //  fmt.Println(chalk.Bold, "But text styles don't work quite like colors :(")
  132.  
  133. //  // Or you can use styles
  134. //  blueOnWhite := chalk.Blue.NewStyle().WithBackground(chalk.White)
  135. //  fmt.Printf("%s%s%s\n", blueOnWhite, "And they also have backgrounds!", chalk.Reset)
  136. //  fmt.Println(
  137. //      blueOnWhite.Style("You can style strings the same way you can color them!"))
  138. //  fmt.Println(
  139. //      blueOnWhite.WithTextStyle(chalk.Bold).
  140. //          Style("You can mix text styles with colors, too!"))
  141.  
  142. //  // You can also easily make styling functions thanks to go's functional side
  143. //  lime := chalk.Green.NewStyle().
  144. //      WithBackground(chalk.Black).
  145. //      WithTextStyle(chalk.Bold).
  146. //      Style
  147. //  fmt.Println(lime("look at this cool lime text!"))
  148. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement