Added http.Handler interface
This commit is contained in:
parent
97d16a5921
commit
bdcd2f75e6
57
README.md
57
README.md
@ -7,6 +7,7 @@ snowflake is a [Go](https://golang.org/) package that provides
|
|||||||
* Methods to parse existing snowflake IDs.
|
* Methods to parse existing snowflake IDs.
|
||||||
* Methods to convert a snowflake ID into several other data types.
|
* Methods to convert a snowflake ID into several other data types.
|
||||||
* JSON Marshal/Unmarshal functions to easily use snowflake IDs within a JSON API.
|
* JSON Marshal/Unmarshal functions to easily use snowflake IDs within a JSON API.
|
||||||
|
* http.Handler interface to easily serve snowflake IDs over HTTP.
|
||||||
|
|
||||||
**For help with this package or general Go discussion, please join the [Discord
|
**For help with this package or general Go discussion, please join the [Discord
|
||||||
Gophers](https://discord.gg/0f1SbxBZjYq9jLBk) chat server.**
|
Gophers](https://discord.gg/0f1SbxBZjYq9jLBk) chat server.**
|
||||||
@ -39,40 +40,46 @@ cannot guarantee unique IDs across all nodes.
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/bwmarrin/snowflake"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/snowflake"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// Create a new Node with a Node number of 1
|
// Create a new Node with a Node number of 1
|
||||||
node, err := snowflake.NewNode(1)
|
node, err := snowflake.NewNode(1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate a snowflake ID.
|
// Generate a snowflake ID.
|
||||||
id, err := node.Generate()
|
id, err := node.Generate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print out the ID in a few different ways.
|
// Print out the ID in a few different ways.
|
||||||
fmt.Printf("Int64 ID: %d\n", id)
|
fmt.Printf("Int64 ID: %d\n", id)
|
||||||
fmt.Printf("String ID: %s\n", id)
|
fmt.Printf("String ID: %s\n", id)
|
||||||
fmt.Printf("Base2 ID: %s\n", id.Base2())
|
fmt.Printf("Base2 ID: %s\n", id.Base2())
|
||||||
fmt.Printf("Base64 ID: %s\n", id.Base64())
|
fmt.Printf("Base64 ID: %s\n", id.Base64())
|
||||||
|
|
||||||
// Print out the ID's timestamp
|
// Print out the ID's timestamp
|
||||||
fmt.Printf("ID Time : %d\n", id.Time())
|
fmt.Printf("ID Time : %d\n", id.Time())
|
||||||
|
|
||||||
// Print out the ID's node number
|
// Print out the ID's node number
|
||||||
fmt.Printf("ID Node : %d\n", id.Node())
|
fmt.Printf("ID Node : %d\n", id.Node())
|
||||||
|
|
||||||
|
// Print out the ID's sequence number
|
||||||
|
fmt.Printf("ID Step : %d\n", id.Step())
|
||||||
|
|
||||||
|
// Start HTTP Server for this Node on port 8080
|
||||||
|
http.ListenAndServe(":8080", node)
|
||||||
|
|
||||||
// Print out the ID's sequence number
|
|
||||||
fmt.Printf("ID Step : %d\n", id.Step())
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
10
snowflake.go
10
snowflake.go
@ -4,6 +4,7 @@ package snowflake
|
|||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -85,6 +86,15 @@ func (n *Node) Generate() (ID, error) {
|
|||||||
), nil
|
), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *Node) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
id, err := n.Generate()
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Internal Error", 500)
|
||||||
|
}
|
||||||
|
fmt.Fprint(w, id)
|
||||||
|
}
|
||||||
|
|
||||||
// Int64 returns an int64 of the snowflake ID
|
// Int64 returns an int64 of the snowflake ID
|
||||||
func (f ID) Int64() int64 {
|
func (f ID) Int64() int64 {
|
||||||
return int64(f)
|
return int64(f)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user