-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[server] Add blueprints, also make this v2.0.0
- Loading branch information
Showing
12 changed files
with
580 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Blueprints | ||
|
||
[`Blueprint`][nserver.server.Blueprint]s provide a way for you to compose your application. They support most of the same functionality as a `NameServer`. | ||
|
||
Use cases: | ||
|
||
- Split up your application across different blueprints for maintainability / composability. | ||
- Reuse a blueprint registered under different rules. | ||
- Allow custom packages to define their own rules that you can add to your own server. | ||
|
||
Blueprints require `nserver>=2.0` | ||
|
||
## Using Blueprints | ||
|
||
```python | ||
from nserver import Blueprint, NameServer, ZoneRule, ALL_CTYPES, A | ||
|
||
# First Blueprint | ||
mysite = Blueprint("mysite") | ||
|
||
@mysite.rule("nicholashairs.com", ["A"]) | ||
@mysite.rule("www.nicholashairs.com", ["A"]) | ||
def nicholashairs_website(query: Query) -> A: | ||
return A(query.name, "159.65.13.73") | ||
|
||
@mysite.rule(ZoneRule, "", ALL_CTYPES) | ||
def nicholashairs_catchall(query: Query) -> None: | ||
# Return empty response for all other queries | ||
return None | ||
|
||
# Second Blueprint | ||
en_blueprint = Blueprint("english-speaking-blueprint") | ||
|
||
@en_blueprint.rule("hello.{base_domain}", ["A"]) | ||
def en_hello(query: Query) -> A: | ||
return A(query.name, "1.1.1.1") | ||
|
||
# Register to NameServer | ||
server = NameServer("server") | ||
server.register_blueprint(mysite, ZoneRule, "nicholashairs.com", ALL_CTYPES) | ||
server.register_blueprint(en_blueprint, ZoneRule, "au", ALL_CTYPES) | ||
server.register_blueprint(en_blueprint, ZoneRule, "nz", ALL_CTYPES) | ||
server.register_blueprint(en_blueprint, ZoneRule, "uk", ALL_CTYPES) | ||
``` | ||
|
||
### Middleware, Hooks, and Error Handling | ||
|
||
Blueprints maintain their own `QueryMiddleware` stack which will run before any rule function is run. Included in this stack is the `HookMiddleware` and `ExceptionHandlerMiddleware`. | ||
|
||
## Key differences with `NameServer` | ||
|
||
- Does not use settings (`Setting`). | ||
- Does not have a `Transport`. | ||
- Does not have a `RawRecordMiddleware` stack. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" | |
|
||
[project] | ||
name = "nserver" | ||
version = "1.1.0.dev1" | ||
version = "2.0.0" | ||
description = "DNS Name Server Framework" | ||
authors = [ | ||
{name = "Nicholas Hairs", email = "[email protected]"}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from .models import Query, Response | ||
from .rules import StaticRule, ZoneRule, RegexRule, WildcardStringRule | ||
from .rules import ALL_QTYPES, StaticRule, ZoneRule, RegexRule, WildcardStringRule | ||
from .records import A, AAAA, NS, CNAME, PTR, SOA, MX, TXT, CAA | ||
from .server import NameServer | ||
from .server import NameServer, Blueprint | ||
from .settings import Settings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.