How I added Swagger in LetsDisc Repo

in #steemdevs6 years ago

Swagger UI allows the development team to visualize and interact with the API without actually implementing logic in place. Its automatically generated from the specification, which makes it really easy for back end implementation and client side consumption.

I have created a LetsDisc which will be a Clone of StackOverflow on Steem Blockchain. LetsDisc can solve the problem to keep everything on one place. Though you will have an option to Post on Steem, if you do not have the Steem Account, you can use it without posting it. As of now you can Login, Register, Create/Edit Question, Upvote other users questions.

I tried to change the normal JavaScript code to AngularJS, so for that I try to use SwaggerUI for consuming in my frontend. To do it I have done like below,

At first Install the Swashbuckle.AspNetCore NuGet package(https://www.nuget.org/packages/Swashbuckle.AspNetCore/) to your Web project.

Then added the below code in ConfigureServices function of Startup.cs

services.AddSwaggerGen(options =>
{
 options.SwaggerDoc("v1", new Info{Title="LetsDisc API",Version="v1"});
 options.DocInclusionPredicate((docName, description) => true);
});

Then added the below code in Configure function of Startup.cs

// Enable middleware to serve generated Swagger as a JSON endpoint
app.UseSwagger();
// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
app.UseSwaggerUI(options =>
{
  options.SwaggerEndpoint(_appConfiguration["App:ServerRootAddress"] 
     + "/swagger/v1/swagger.json", "LetsDisc API V1");
  options.IndexStream = () => Assembly.GetExecutingAssembly()
    .GetManifestResourceStream
       ("LetsDisc.Web.Host.wwwroot.swagger.ui.index.html");
}); // URL: /swagger

That's it, when you run your application and go to /swagger/index.html you will see all the API's are documented.

swagger.png

Also Posted : http://www.codingdefined.com/2018/09/how-i-added-swagger-in-letsdisc-repo.html

Sort:  

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

Hi @codingdefined!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your UA account score is currently 3.967 which ranks you at #3630 across all Steem accounts.
Your rank has improved 183 places in the last three days (old rank 3813).

In our last Algorithmic Curation Round, consisting of 434 contributions, your post is ranked at #419.

Evaluation of your UA score:
  • You're on the right track, try to gather more followers.
  • Try to show your post to more followers, for example via networking on our discord!
  • Try to work on user engagement: the more people that interact with you via the comments, the higher your UA score!

Feel free to join our @steem-ua Discord server

Coin Marketplace

STEEM 0.26
TRX 0.11
JST 0.033
BTC 64383.21
ETH 3098.60
USDT 1.00
SBD 3.89