DEV Community

Mark Davies
Mark Davies

Posted on

My new configurable server

I think I actually had a productive thought today, and I started coding and I think I've come to a place where I can start showing off what I've been working on for the last couple of hours.

What have you been working on?

It's a configurable test server! A little less than exciting, but let me take you through my thinking and why I have created this small tool, which I think is going to help me out a bunch.

Why did you create this thing?

I don't know about you but I spend a lot of my day working on one thing, and that one thing talks to other things and what I've found is that that I have to run both applications even though I'm only interested in the one.

In the past when this has become a bit of a hinderance I have spun up a small web application that sends back fake data. The problem with this? Well I need to spin up a fake data service for everything that my thing calls, so I thought to myself that there must be a better way - and their sort of is, you can setup postman "apis" that allows you to configure a contact-based API.

I've been doing a lot of reading about dotnet "global" tools recently, basically, they're small tools that you can run from your command line. So I thought you know what, I'm going to write a dotnet global tool that allows you to spin up a small dotnetcore server and reads a file. In this file, you'll be able to specify a set of URL's and a set of responses and when a URL gets hit the response gets sent back.

It saves me a bunch of hassle in creating small fake data services.

Let me show you how

If you go to your command shell right now and run this command:
dotnet tool install --global DotNetSimpleServer

You will have my tool installed, cool hey?

So how do you use it!? Very simply! If you run: dotnet-server -fileName:C:\example.txt for example it will load all of your urls and responses from that file. What does the file look like, let me give you a peak:

{
    "sslEnabled" : true,
    "tlsEnabled" : true,
    "interactions": [
        {
            "description": "A GET request to retrieve a thing",
            "request": {
                "method": "get",
                "path": "/things/1234"
            },
            "response": {
                "status": 200,
                "headers": [],
                "body": {
                    "Hello" : "World"
                }
            }
        },
        {
            "description": "A GET request to retrieve a thing",
            "request": {
                "method": "get",
                "path": "/otherThing"
            },
            "response": {
                "status": 200,
                "headers": [],
                "body": {
                    "Hello" : "World2"
                }
            }
        }
    ]
}

Here we are defining two endpoints one /things/1234 which will return the json object {"Hello": "World"} and a /otherThing endpoint that will return an object that looks like: {"Hello" : "World2"}.

I like this tool, I think it's going to save me a bunch of time in the future!

I will admit the code is a bit rough but ready, for example if you don't specify a fileName then it will just throw an exception and I'm pretty sure if you get the contents of the file wrong something weird will happen. But these are bugs that I will be actively working on in the next couple of days.

If you like what you see, please give the tool a go and see what you think!

GitHub logo joro550 / dotnet-server

Dot net global tool that spins up a configurable server for api urls

dotnet-server

GitHub license

A dot net global tool to spin up a quick server, that sets up a url and response contract. For example you can ask the server to respond to a sepcific request on /hello with {"Hello":"world"}, you can setup the server to respond to all of your favourite Http methods like get, post, put, delete.

Why does this exist?

In the modern developer world we are working with interconnected systems service A calls service B calls service C, now when you are working on service A what do you do when it calls service B?

  • Do you spin up a fake service B?
  • Do you create an API collection in postman?

Let me introduce you to a third option, dot net server - a configurable http server that you configure with a text file (written in json) that will only answer requests on the url's that you have…

Oldest comments (0)