Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "net/http"
- "time"
- "strings"
- "log"
- "github.com/gorilla/mux"
- "github.com/ttacon/chalk"
- )
- type Server struct {
- name string
- id int
- }
- type User struct {
- username string
- userid int
- usercolor string
- }
- func HomeHandler(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, "HomeHandler")
- }
- func RegisterHandler(w http.ResponseWriter, r *http.Request) {
- // Registers a new user
- fmt.Fprintf(w, "RegisterHandler")
- fmt.Println(w, "RegisterHandler")
- }
- func ServersHandler(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, "ServersHandler")
- }
- func CreateServersHandler(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, "CreateServersHandler")
- }
- func ServerInstanceHandler(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, "ServerInstanceHandler")
- }
- func DeleteServerInstanceHandler(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, "DeleteServerInstanceHandler")
- }
- const GETALL string = "GETALL"
- const GETONE string = "GETONE"
- const POST string = "POST"
- const PUT string = "PUT"
- const DELETE string = "DELETE"
- func main() {
- r := mux.NewRouter()
- // Show some type of landing page
- r.HandleFunc("/", HomeHandler).Methods("GET")
- // Register a new user account
- r.HandleFunc("/register", RegisterHandler).Methods("GET")
- // Get a list of servers, maybe rank them
- r.HandleFunc("/servers", ServersHandler).Methods("GET")
- // Create a new server
- r.HandleFunc("/servers", CreateServersHandler).Methods("PUT")
- // Access a specific server
- r.HandleFunc("/servers/{serverInstance}", ServerInstanceHandler).Methods("GET")
- // Delete a specific server. Do we want this, really?
- r.HandleFunc("/servers/{serverInstance}/delete", DeleteServerInstanceHandler).Methods("DELETE")
- err := r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
- pathTemplate, err := route.GetPathTemplate()
- if err == nil {
- fmt.Println(chalk.Green, "ROUTE:", pathTemplate)
- }
- pathRegexp, err := route.GetPathRegexp()
- if err == nil {
- fmt.Println(chalk.Cyan, "Path regexp:", pathRegexp)
- }
- queriesTemplates, err := route.GetQueriesTemplates()
- if err == nil {
- fmt.Println(chalk.Magenta, "Queries templates:", strings.Join(queriesTemplates, ","))
- }
- queriesRegexps, err := route.GetQueriesRegexp()
- if err == nil {
- fmt.Println(chalk.Blue, "Queries regexps:", strings.Join(queriesRegexps, ","))
- }
- methods, err := route.GetMethods()
- if err == nil {
- fmt.Println(chalk.White, "Methods:", strings.Join(methods, ","))
- }
- fmt.Println()
- return nil
- })
- if err != nil {
- fmt.Println(err)
- }
- http.Handle("/", r)
- srv := &http.Server{
- Handler: r,
- Addr: "127.0.0.1:8000",
- WriteTimeout: 15 * time.Second,
- ReadTimeout: 15 * time.Second,
- }
- log.Fatal(srv.ListenAndServe())
- }
- // func colors() {
- // // You can just use colors
- // fmt.Println(chalk.Red, "Writing in colors", chalk.Cyan, "is so much fun", chalk.Reset)
- // fmt.Println(chalk.Magenta.Color("You can use colors to color specific phrases"))
- // // You can just use text styles
- // fmt.Println(chalk.Bold.TextStyle("We can have bold text"))
- // fmt.Println(chalk.Underline.TextStyle("We can have underlined text"))
- // fmt.Println(chalk.Bold, "But text styles don't work quite like colors :(")
- // // Or you can use styles
- // blueOnWhite := chalk.Blue.NewStyle().WithBackground(chalk.White)
- // fmt.Printf("%s%s%s\n", blueOnWhite, "And they also have backgrounds!", chalk.Reset)
- // fmt.Println(
- // blueOnWhite.Style("You can style strings the same way you can color them!"))
- // fmt.Println(
- // blueOnWhite.WithTextStyle(chalk.Bold).
- // Style("You can mix text styles with colors, too!"))
- // // You can also easily make styling functions thanks to go's functional side
- // lime := chalk.Green.NewStyle().
- // WithBackground(chalk.Black).
- // WithTextStyle(chalk.Bold).
- // Style
- // fmt.Println(lime("look at this cool lime text!"))
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement